Angular2:使用 angular-cli 创建的应用程序之间的依赖关系 [英] Angular2: Dependencies Between Apps created with angular-cli
问题描述
我正在使用 Angular2-cli 创建两个应用程序:MyFirstApp代码> &
MySecondApp
.我希望 MySecondApp
导入 MyFirstApp
以便我可以重用指令/服务/组件.当我导入 MyFirstApp
时,出现以下编译错误:Cannot find module 'MyFirstApp'
.
I am using the Angular2-cli to create two applications: MyFirstApp
& MySecondApp
. I whish MySecondApp
to import MyFirstApp
so I can reuse directive/service/component.
When I import MyFirstApp
, I have the following compilation error: Cannot find module 'MyFirstApp'
.
以下是如何复制:
$ npm -v
3.9.3
$ node -v
v6.2.1
$ npm list | grep 'angular'
├── @angular/common@2.0.0-rc.1
├── @angular/compiler@2.0.0-rc.1
├── @angular/core@2.0.0-rc.1
├── @angular/http@2.0.0-rc.1
├── @angular/platform-browser@2.0.0-rc.1
├── @angular/platform-browser-dynamic@2.0.0-rc.1
├── @angular/router@2.0.0-rc.1
├─┬ angular-cli@1.0.0-beta.5
$ ng new MyFirstApp
$ cd MyFirstApp
$ ng g service MyFirstAppService
$ ng build
$ cd ..
$ ng new MySecondApp
$ cd MySecondApp
$ npm install ../MyFirstApp --save #Also tried w/ ng install ../MyFirstApp and github link
$ vim src/app/my-second-app.component.ts
修改 src/app/my-second-app.component.ts
使其看起来像:
Modify src/app/my-second-app.component.ts
so it looks like:
import { Component } from '@angular/core';
import { MyFirstAppService } from 'MyFirstApp';
@Component({
moduleId: module.id,
selector: 'MySecondApp',
templateUrl: 'mysecondapp.component.html',
styleUrls: ['mysecondapp.component.css']
})
export class MySecondAppAppComponent {
title = 'MySecondApp works!';
}
我尝试了许多版本的 import { MyFirstAppService } from 'MyFirstApp';
(即 import { MyFirstAppServiceService } from './../../node_modules/my-first-app/src/app/my-first-app-service.service';
&co) 并使用 system-config.ts
(使用 this 和 this) 没有任何运气.我总是有一个找不到模块'MyFirstApp'
.
I tried many version of import { MyFirstAppService } from 'MyFirstApp';
(i.e. import { MyFirstAppServiceService } from './../../node_modules/my-first-app/src/app/my-first-app-service.service';
& co) and played with system-config.ts
(using this and this) without any luck. I always have a Cannot find module 'MyFirstApp'
.
有什么建议吗?
编辑:通过如下调整 system-config.ts
(第 1 到 14 行):
Edit:
By tweaking system-config.ts
(lines 1 to 14) as follows:
const map: any = {
'my-first-app': 'vendor/my-first-app/app/index.js'
};
/** User packages configuration. */
const packages: any = {
'my-first-app': {
format: 'cjs'
}
};
我可以import { MyFirstAppServiceService } from 'my-first-app/src/app/my-first-app-service.service';
..../dist/...
不起作用.似乎不是正确的方式来做到这一点......难道不应该从'my-first-app/my-first-做import { MyFirstAppServiceService}app-service.service'
?
I can do import { MyFirstAppServiceService } from 'my-first-app/src/app/my-first-app-service.service';
. .../dist/...
doesn't work. Doesn't seems to be the right way to do it though... Should'nt it be possible to do import { MyFirstAppServiceService } from 'my-first-app/my-first-app-service.service'
?
请注意,生成的 vendors/my-first-app/
目录不包含 MyFirstApp
的代码
Note that the resulting vendors/my-first-app/
directory doesn't contain the code of MyFirstApp
$ # While in MySecondApp
$ tree dist/vendor/my-first-app
dist/vendor/my-first-app
├── angular-cli-build.js
└── config
├── environment.js
├── karma.conf.js
├── karma-test-shim.js
└── protractor.conf.js
谢谢.
推荐答案
Angular2-cli 项目的开发者希望支持 library/addons 1.0.0 版本的特色项目.
Developers of the Angular2-cli project want to support library/addons flavoured project for the 1.0.0 release.
现在,我们可以做些什么来使它工作:
For now, what we can do to make it works:
- 在库 package.json 中添加一个 main
- npm install yourlib --save 在你的其他应用程序中
配置您的第二个应用程序,使其根据您的需要打包您的库.吞咽示例:
- Add a main in the library package.json
- npm install yourlib --save in your other apps
Configure your second app so it packages your lib according to your needs. An example with gulp:
var copyScripts = require('ionic-gulp-scripts-copy');
var copyScripts = require('ionic-gulp-scripts-copy');
gulp.task('assets', function(){
return copyScripts({src:'node_modules/your-lib/dist/**/*', dest:'our-dest'});
});
这篇关于Angular2:使用 angular-cli 创建的应用程序之间的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!