我有一个用 angular2 typescript 编写的代码,这是一个使用服务和组件导出的模块
@NgModule({
declarations:[
...DECLARATIONS
],
imports: [
CommonModule,
ChartModule
],
exports:[
...DECLARATIONS,
CommonModule,
ChartModule
],
providers:[
AlertErrorHandleService
, AuthenticationModelService
, AuthConnectionBackend
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class MyModule {
static forRoot():ModuleWithProviders {
return {
ngModule: StmsModule,
providers: [AlertErrorHandleService
, AuthenticationModelService
, AuthConnectionBackend
]
};
}
}
我想在不同的 angular2 应用程序中使用这个 MyModule,所以我用这个 tsconfig 和脚本发布了它
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"removeComments": true,
"module": "es2015",
"target": "es5",
"moduleResolution": "node",
"sourceMap": true,
"declaration": true,
"noImplicitAny": false,
"experimentalDecorators": true,
"lib": ["dom", "es2015"],
"outDir": "dist",
"types": [
"node"
]
},
"exclude": [
"node_modules",
"dist",
"scripts"
],
"angularCompilerOptions": {
"genDir": "aot"
}
}
包.json
"name": "my-lib",
"version": "0.2.1",
"description": "Shared lib angular2",
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"ngc": "ngc",
"lint": "tslint src/**/*.ts",
"copyPackage":"node ./scripts/copy-package",
"build": "rimraf -rf aot dist && npm run ngc && npm run copyPackage",
"publishPackage": "npm run build && cd dist && npm publish"
},
"keywords": [
"angular",
"angular2"
],
"author": "Haddar Macdasi",
"license": "MIT",
"dependencies": {
"@angular/core": "2.1.1",
"@angular/common": "2.1.1",
"@angular/compiler": "2.1.1",
"@angular/compiler-cli": "2.1.1",
"@angular/http":"2.1.1",
"@angular/platform-browser": "2.1.1",
"rxjs": "5.0.0-beta.12",
"angular2-highcharts": "0.4.1",
"highcharts": "5.0.2"
},
"devDependencies": {
"@types/chai": "^3.4.34",
"@types/node": "^6.0.41",
"typescript": "^2.0.3"
}
在使用来自angular2 种子的种子创建的其他 angular2 应用程序中,我已经安装了该软件包,尝试使用它一切正常,因为我不在 AOT 中运行该应用程序,但在 AoT 中它有错误
错误:模块“AboutModule”导入的意外值“MyModule”
我有几个问题:
- 我应该如何发布包/ tsconfig "module" = es2015 / systemjs / commonjs 内部有什么不同
- 模块发布类型是否与如何在 angular2 中使用包有关 - 即,如果我使用 systemjs 发布,我可以在 angular2 AoT 应用程序中使用这个包吗?
- 是否有任何最佳实践如何发布 angular2 共享模块并在 diff 应用程序中使用它?