在功能模块中导入共享模块后,在功能模块中无法识别在共享模块中声明的角组件

时间:2020-03-20 22:31:48

标签: angular

我已将共享模块导入到我的appModule中,并使用其组件之一没有问题,但是当我将共享模块导入到我的功能模块中时,该模块不起作用。

共享模块:

@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    CarouselModule
  ],
  declarations: [
    CarouselComponent,
    SpinnerComponent,
    OverviewComponent,
    TableComponent
  ],
  exports: [
    CommonModule,
    CarouselComponent,
    SpinnerComponent,
    OverviewComponent,
    TableComponent
  ],

})
export class SharedModule { }

功能模块:

@NgModule({
  declarations: [
    IncidentsContainerComponent,
    NewIncidentComponent
  ],
  imports: [
    ReactiveFormsModule,
    IncidentsRoutes, // feature routing
    BsDatepickerModule.forRoot(),
    TimepickerModule.forRoot(),
    NgSelectModule,
    SharedModule
  ],
  providers: [
    IncidentsService
  ]
})
export class IncidentsModule { }

在我的功能模块组件中,仅使用TableComponent选择器:

<app-table></app-table>

我收到此错误:

  1. 如果“ app-table”是Angular组件,请验证它是否属于此模块。
  2. 如果“ app-table”是Web组件,则将“ CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“ @ NgModule.schemas” 禁止显示此消息。

TableComponent:

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.css']
})
export class TableComponent implements OnInit {

  @Input() elements: Array<any>;
  @Input() headers: Array<string>;

  constructor() { }

  ngOnInit() {
    console.log('...from table ', this.headers);
    console.log('...from table ', this.elements);
  }

}

这是我的项目结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果将来有人遇到相同的问题,即使我遇到的错误是指在SharedModule中导出的共享组件('app-table'),但问题是我忘记声明要在其自己的模块中引用共享组件('app-table')的组件。我认为Angular应该给出的错误是,我使用共享组件的组件未在任何模块中注册,并且会更加清晰直接。