angular – 在2个模块之间共享分量
我正在尝试在2个模块(父级和子级)中包含一个Component,但在此过程中会遇到各种错误
app.module.ts @NgModule({ declarations: [SharedComponent],exports: [SharedComponent]... }) child.module.ts @NgModule({ imports: [SharedComponent],//Unexpected directive imported by module }) app.html <div class="container"> <shared-selector></shared-selector> <child-selector></child-selector> </div> child.html <div> content <shared-selector></shared-selector> </div> 我正在Async中加载ChildModule loadChildren: 'app/child.module#ChildModule', 当没有在ChildModule中导入或声明时,我收到错误: template parse error: shared-selector is not a known element ******更新******* 在创建FeatureModule时,为了工作,SharedModule应该导出Components …更新的代码… SharedModule @NgModule({ imports: [ CommonModule ],declarations: [ SharedComponent ],exports: [ SharedComponent ] }) export class SharedModule {} app.module.ts @NgModule({ imports: [ChildModule,SharedModule],... }) child.module.ts @NgModule({ imports: [SharedModule],//Unexpected directive imported by module })
更新
导入仅适用于模块,而不适用于组件。 原版的 一个组件只能添加一个@NgModule()的声明 解决方法为组件创建一个新模块并将新模块添加到导入:[…]其他两个模块(您要在其中使用它)。 另见https://github.com/angular/angular/issues/11481#issuecomment-246186173
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |