【问题标题】:I cannot use component from one module into another module component我无法将一个模块中的组件用于另一个模块组件
【发布时间】:2020-09-14 13:40:26
【问题描述】:

我有两个模块,MapModule 和 LeftListModule。在 MapModule 里面我有 MapComponent,在 LeftListModule 里面有 LeftListContainerComponent。

我想要的是用LeftListContainerComponent变成MapComponent,如下

<p>map works!</p>
<app-left-list-container></app-left-list-container>

为此,我以这种方式配置了 LeftListModule

@NgModule({
  declarations: [LeftListContainerComponent],
  imports: [
    CommonModule
  ],
  exports:[LeftListContainerComponent]
})
export class LeftListModule { }

还有像这样的 MapModule

@NgModule({
  declarations: [MapComponent, LeftListContainerComponent],
  imports: [
    CommonModule
  ]
})
export class MapModule { }

我在网上查了好几个结果,但我不明白问题出在哪里。

我收到这条消息:

map/map.component.html:2:1 中的错误 - 错误 NG8001:“app-left-list-container”不是已知元素: 1. 如果 'app-left-list-container' 是一个 Angular 组件,那么验证它是这个模块的一部分。 2. 如果“app-left-list-container”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。

2 <app-left-list-container></app-left-list-container>
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  map/map.component.ts:5:16
    5   templateUrl: './map.component.html',
                     ~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component MapComponent.

【问题讨论】:

    标签: angular


    【解决方案1】:

    您需要将LeftListModule 导入您的MapModule

    【讨论】:

    • @NgModule({ declarations: [MapComponent, LeftListContainerComponent], imports: [CommonModule, LeftListModule ] }) export class MapModule { }我正在这样做,但我仍然有问题
    • 你不应该在MapModule中声明LeftListContainerComponent
    【解决方案2】:

    问题在于,在您的 LeftListModule 中您确实导出了组件,但在 MapModule 中您没有导入从中导出组件的模块。您需要在第二个模块的导入中添加 LeftListModule

    在共享模块中重新定位共享组件并在两个导入中添加此共享模块

    【讨论】:

    • 我有使用共享模块的耻辱问题
    猜你喜欢
    • 1970-01-01
    • 2019-04-14
    • 2019-10-28
    • 1970-01-01
    • 2018-10-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多