【发布时间】:2016-06-24 04:12:37
【问题描述】:
我正在使用Angular2-cli 创建两个应用程序:MyFirstApp 和MySecondApp。我希望MySecondApp 导入MyFirstApp,这样我就可以重用指令/服务/组件。
当我导入MyFirstApp 时,出现以下编译错误: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,使其看起来像:
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)一起玩,没有任何运气。我总是有一个Cannot find module 'MyFirstApp'。
有什么建议吗?
编辑:
通过调整system-config.ts(第 1 到 14 行)如下:
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/... 不起作用。不过,这似乎不是 正确 的方法......难道不应该这样做 import { MyFirstAppServiceService } from 'my-first-app/my-first-app-service.service' 吗?
请注意,生成的vendors/my-first-app/ 目录不包含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
谢谢。
【问题讨论】:
标签: angular angular2-services angular2-cli