【发布时间】:2021-05-26 17:12:23
【问题描述】:
我正在构建一个 Angular 10 库以在私有 NPM 存储库上发布。按照 Angular.io 指南,我使用 Angular CLI 使用了 ng new my-workspace --create-application=false 和 ng generate library my-library。
我需要为其中一个组件导入 RouterModule,所以我这样做:
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { MyLibraryComponent } from './my-library.component';
@NgModule({
declarations: [MyLibraryComponent],
imports: [
RouterModule.forChild(null)
],
exports: [MyLibraryComponent]
})
export class MyLibraryModule { }
当我尝试构建库时出现此错误:
ERROR: Error during template compile of 'MyLibraryModule'
Function calls are not supported in decorators but 'RouterModule' was called.
An unhandled exception occurred: Error during template compile of 'MyLibraryModule'
Function calls are not supported in decorators but 'RouterModule' was called.
See "/tmp/ng-bGhmPt/angular-errors.log" for further details.
我尝试在 const 变量中定义 RouterModule.forChild() 以避免装饰器中的函数调用,但它会产生相同的错误:
import { ModuleWithProviders, NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { MyLibraryComponent } from './my-library.component';
const routerModule: ModuleWithProviders<RouterModule> = RouterModule.forChild(null);
@NgModule({
declarations: [MyLibraryComponent],
imports: [routerModule],
exports: [MyLibraryComponent],
})
export class MyLibraryModule {}
这个错误似乎与 Ivy 有关,因为构建我正在使用的库 ng build --prod 并且 Ivy 被禁用用于生产。如果我启用 Ivy,请从 tsconfig.lib.prod.json 中删除此部分
"angularCompilerOptions": {
"enableIvy": false
}
然后它构建成功,但由于以下错误,我无法将其发布到 NPM:
ERROR: Trying to publish a package that has been compiled by Ivy. This is not allowed.
Please delete and rebuild the package, without compiling with Ivy, before attempting to publish.
我应该如何在没有 Ivy 的情况下将 RouterModule (forChild) 导入要构建的库中?
【问题讨论】: