简体   繁体   English

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

[英]Angular components declared in shared module not recognised in feature modules after importing shared module in feature's module

I have imported the shared module into my appModule and used one of its components without issues but doesn't work when I imported my shared module into in my feature module.我已将共享模块导入我的 appModule 并使用其组件之一没有问题,但当我将共享模块导入我的功能模块时不起作用。

Shared Module:共享模块:

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

})
export class SharedModule { }

Feature Module:功能模块:

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

In my feature module component am just using the TableComponent selector:在我的功能模块组件中,我只是使用 TableComponent 选择器:

<app-table></app-table>

and I get this error:我收到这个错误:

  1. If 'app-table' is an Angular component, then verify that it is part of this module.如果 'app-table' 是一个 Angular 组件,那么验证它是否是这个模块的一部分。
  2. If 'app-table' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.如果“app-table”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以抑制此消息。

The 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);
  }

}

This is my project structure:这是我的项目结构:

在此处输入图片说明

In case any one in the future having the same issue, even though the error I was getting was referring to the shared component ('app-table') exported in my SharedModule but the issue was that I forgot to declare the Component in which I am referencing the shared component ('app-table') in its own Module.万一将来有人遇到同样的问题,即使我得到的错误是指在我的 SharedModule 中导出的共享组件('app-table') ,但问题是我忘记声明我在其中的组件我在它自己的模块中引用共享组件('app-table') I think the error Angular should have given is that the component in which I am using my shared component is not registered in any Module and it would have been more clearer and straight forward.我认为 Angular 应该给出的错误是我使用共享组件的组件没有在任何模块中注册,它会更清晰和直接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM