angular2 主模块与子模块之间如何共享组件?

当前有个 header.component.ts 公共头部组件,我在 app.module.ts 注册后,无法其他模块中使用。
其他的模块也已经在app.module.ts 注册。

app.module.ts

@NgModule({
    declarations : [ HeaderComponent ],
    imports : [ otherModule ]     // 其他子模块
})

other.module.ts

@NgModule({
    ...........
 })
export class otherModule {}

other.component.ts

<div class="other-component">
    <header-component></header-component>   
 </div>

报错 :

'header-component' is not a known element:
If 'header-component' is an Angular component, then verify that it is part of this module.
阅读 4.1k
2 个回答

子模块是不能导入父模块里面的组件的,你想要共享header组件的话,就在每个要用的模块里面导入header组件。
共享模块也是父模块可以使用子模块里面exports出来的东西,你把需要共享的东西加入到一个模块里面,然后在其他模块导入

  1. 在其他模块导入
  2. 或者全局导入
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题