ng-packagr 打包的库:只有缩小的组件名称有效
ng-packagr packaged lib: only minified component names work
我制作了一个 angular (7.2) 库,与 ng-packagr 捆绑在一起,发布在 npm 上。现在我想在另一个项目中使用它。我可以导入模块,但不能导入包含的组件:
我捆绑的库项目中的模块看起来像
import { ConfirmDialogComponent } from './dialogs/confirm/confirm-dialog.component';
@NgModule({
imports: [...],
entryComponents: [
ConfirmDialogComponent
],
declarations: [
ConfirmDialogComponent
],
exports: [
ConfirmDialogComponent
]
})
export class MyComponents { }
在目标项目中,我使用 npm 安装发布的包,并在 `app.modules.ts
中导入模块
从'@angular/platform-browser'导入{BrowserModule};
从'@angular/core'导入{NgModule};
import { AppComponent } from './app.component';
import { MyComponents } from 'my-components';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
MyComponents
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
到目前为止没有错误,但是当我在 app.component.ts
中使用它时
import { Component } from '@angular/core';
import { ConfirmDialogComponent } from 'my-components';
该语句导致
的编译错误
ERROR in src/app/app.component.ts(2,10): error TS2305: Module '"../../node_modules/my-components/my-components"' has no exported member 'MessageDialogComponent'.
如果我看node_modules/my-components/my-components.d.ts
/**
* Generated bundle index. Do not edit.
*/
export * from './my-components.module';
export { ConfirmDialogComponent as ɵa } from './dialogs/confirm/confirm-dialog.component';
和这个app.component.ts
到
import { Component } from '@angular/core';
import { ɵa } from 'my-components';
有效!
那么为什么缩小的 (?) 别名有效但组件的真实名称无效?
--
编辑:如果我从声明文件中删除 as ɵa,我可以按预期使用组件的真实姓名。这里出了什么问题?
解决方案是使用桶文件作为 ng-packagr public_api.ts
的入口文件,而不是 (angular) 模块文件。
export * from './my-components.module'; // don't forget to export the module itself
export * from './dialogs/confirm/confirm-dialog.component';
我制作了一个 angular (7.2) 库,与 ng-packagr 捆绑在一起,发布在 npm 上。现在我想在另一个项目中使用它。我可以导入模块,但不能导入包含的组件:
我捆绑的库项目中的模块看起来像
import { ConfirmDialogComponent } from './dialogs/confirm/confirm-dialog.component';
@NgModule({
imports: [...],
entryComponents: [
ConfirmDialogComponent
],
declarations: [
ConfirmDialogComponent
],
exports: [
ConfirmDialogComponent
]
})
export class MyComponents { }
在目标项目中,我使用 npm 安装发布的包,并在 `app.modules.ts
中导入模块从'@angular/platform-browser'导入{BrowserModule}; 从'@angular/core'导入{NgModule};
import { AppComponent } from './app.component';
import { MyComponents } from 'my-components';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
MyComponents
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
到目前为止没有错误,但是当我在 app.component.ts
import { Component } from '@angular/core';
import { ConfirmDialogComponent } from 'my-components';
该语句导致
的编译错误ERROR in src/app/app.component.ts(2,10): error TS2305: Module '"../../node_modules/my-components/my-components"' has no exported member 'MessageDialogComponent'.
如果我看node_modules/my-components/my-components.d.ts
/**
* Generated bundle index. Do not edit.
*/
export * from './my-components.module';
export { ConfirmDialogComponent as ɵa } from './dialogs/confirm/confirm-dialog.component';
和这个app.component.ts
到
import { Component } from '@angular/core';
import { ɵa } from 'my-components';
有效!
那么为什么缩小的 (?) 别名有效但组件的真实名称无效?
--
编辑:如果我从声明文件中删除 as ɵa,我可以按预期使用组件的真实姓名。这里出了什么问题?
解决方案是使用桶文件作为 ng-packagr public_api.ts
的入口文件,而不是 (angular) 模块文件。
export * from './my-components.module'; // don't forget to export the module itself
export * from './dialogs/confirm/confirm-dialog.component';