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 })
导入仅适用于模块,而不适用于组件。
我怀疑如果app.module导出共享组件会有效。将其设置为SharedModule或MyFeatureModule,并将此模块添加到要使用模块导出的元素的导入中。
原版的
一个组件只能添加一个@NgModule()的声明
解决方法为组件创建一个新模块并将新模块添加到导入:[…]其他两个模块(您要在其中使用它)。
另见https://github.com/angular/angular/issues/11481#issuecomment-246186173
When you make a component part of a module you impart on it a set of rules when it is compiled. Having a component without belonging to a NgModule is meaningless as the compiler can’t compile it. Having a component be part of more then one module is also weird as you are saying that depending which module you chose the rules for compiling are different. And when you dynamically load such a component it would be ambiguous which set of compilation rules you wanted.
The idea of removing that each component belongs to exactly one module is a no-go for the reasons stated above.