我有一个由 Nrwl Nx 工具创建的 monorepo。monorepo 已经包含一个 Angular Web 应用程序,现在我想将我的 Ionic 应用程序移动到现有的 monorepo。
工作空间的结构是,
apps/
-> AngularWebApp
-> IonicMobileApp/
-> e2e
-> node_modules
-> src/
->app/
-> app.module.ts
-> app.component.ts
-> app.component.html
-> ..
-> home/
-> home.page.html
-> home.module.ts
-> home.page.ts
-> ionic.config.json
-> package.json
-> tsconfig.json
-> tsconfig.app.json
-> angular.json
libs/
-> my-lib
package.json
ionic.config.json
package.json
tsconfig.json
tsconfig.app.json
angular.json
我还想将我的离子组件重用到我的 Angular 项目中。为了实现组件的重用,到目前为止我看到的最好的方法是创建一个共享库。
要创建一个库,我使用以下命令,
ng 生成库 my-lib --prefix=lib
ng generate library my-lib --prefix=lib
? In which directory should the library be generated?
? What framework should this library use? Angular [ https://angular.io/ ]
? Which stylesheet format would you like to use? SCSS [ http://sass-lang.com ]
? Which tags would you like to add to the library? (used for linting)
? Which Unit Test Runner would you like to use for the library? Jest [https://jestjs.io/]
CREATE libs/my-lib/README.md (986 bytes)
CREATE libs/my-lib/tsconfig.lib.json (705 bytes)
CREATE libs/my-lib/tslint.json (187 bytes)
CREATE libs/my-lib/src/index.ts (37 bytes)
CREATE libs/my-lib/src/lib/my-lib.module.ts (160 bytes)
CREATE libs/my-lib/src/lib/my-lib.module.spec.ts (344 bytes)
CREATE libs/my-lib/tsconfig.json (123 bytes)
CREATE libs/my-lib/tsconfig.spec.json (245 bytes)
CREATE libs/my-lib/jest.config.js (261 bytes)
CREATE libs/my-lib/src/test-setup.ts (30 bytes)
UPDATE angular.json (58704 bytes)
UPDATE nx.json (1732 bytes)
UPDATE tsconfig.json (1649 bytes)
UPDATE package.json (5294 bytes)
创建库 my-lib 后,我在 my-lib 中创建了一个组件。
my-lib.component.ts 看起来像,
@Component({
selector: 'app-my-lib',
templateUrl: './my-lib.component.html',
styleUrls: ['./my-lib.component.scss']
})
export class MyLibComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
在 angular.json 中,
"my-lib": {
"root": "libs/my-lib",
"sourceRoot": "libs/my-lib/src",
"projectType": "library",
"prefix": "lib",
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"libs/my-lib/tsconfig.lib.json",
"libs/my-lib/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**"]
}
},
"test": {
"builder": "@nrwl/builders:jest",
"options": {
"jestConfig": "libs/my-lib/jest.config.js",
"tsConfig": "libs/my-lib/tsconfig.spec.json",
"setupFile": "libs/my-lib/src/test-setup.ts"
}
}
},
"schematics": {
"@nrwl/schematics:component": {
"styleext": "scss"
}
}
}
}
并在项目中添加了以下路径tsconfig.json
,
path: {"@workspace/my-lib": ["libs/my-lib/src/index.ts"]}
我将此路径复制到我的移动应用程序的 tsconfig.json,
path: {"@workspace/my-lib": ["../../libs/my-lib/src/index.ts"]}
my-lib.module.ts 看起来像,
@NgModule({
imports: [CommonModule],
declarations: [MyLibComponent],
exports: [MyLibComponent]
})
export class MyLibModule {}
生成库后,我尝试在 Angular 和 ionic 应用程序中使用它。
在我的移动应用程序的 home.module.ts 页面中,
import { MyLibModule } from '@workspace/my-lib'; //specified in apps/IonicMobileApp/tsconfig.json
@NgNodule({imports: [MyLibModule]})
ionic serve
执行命令时一切正常。
尝试使用 --prod 构建 ionic 应用程序时,出现以下错误,
ERROR in Unexpected value 'MyLibModule' in '../libs/my-lib/src/lib/my-lib.module.ts'imported by the module 'AppModule' in ../apps/mobile-app/src/app/app.module.ts'. Please add a @NgModule annotation.
Unexpected value 'MyLibModule' in ../libs/my-lib/src/lib/my-lib.module.ts' imported by the module 'HomePageModule' in ../apps/mobile-app/src/app/home/home.module.ts'. Please add a @NgModule annotation.
'app-my-lib' is not a known element:
1. If 'app-my-lib' is an Angular component, then verify that it is part of this module.
2. If 'app-my-lib' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<ion-content>
<div class="ion-padding">
[ERROR ->]<app-my-lib></app-my-lib>
The world is yo")
[ERROR] An error occurred while running subprocess ng.
ng run app:build:production exited with exit code 1.
Re-running this command with the --verbose flag may provide more information.
经过一番研究,我觉得这与一些AOT编译器问题有关?但我真的不明白如何解决这个问题。
我已经尝试使用 , 运行我的应用程序ionic build --prod --aot=false
,但它仍然失败并出现相同的错误。
这里的任何人都可以帮助摆脱这个错误吗?提前致谢。:)