首页 > 解决方案 > 嵌套/子组件不是任何 NgModule 的一部分

问题描述

我正在开发 Angular 6 应用程序。我有父组件'Consultation',它在提供者的consultation.module 中被引用,然后我有嵌套组件内部咨询组件和测试组件,它们的引用也在提供者的consultation.module 中,然后是在应用程序中添加的consultation.module 引用。模块。结构如下;

我收到错误 SurveyComponent is not part of any NgModule 即使由于在 component.module 中被引用,不确定我在这里缺少什么???

错误

Error: Component SurveyComponent is not part of any NgModule or the module has not been imported into your module.

在此处输入图像描述

咨询模块

@NgModule({
imports:[
    CommonModule
],
declarations: [ 
    ConsultationComponent,
    SurveyComponent,
    TestingComponent
 ]
})

export class ConsultationModule{
}

App.Module // 根级

@NgModule({
imports: [
  ConsultationModule
],
export class AppModule { }

应用程序路由

export const routes: Routes = [

{
path: 'consultation',
component: ConsultationComponent,
data: {
  title: 'Consultation' 
}     
},
 {
 path: 'survey',
 component: SurveyComponent,
 data: {
   title: 'Survey' 
 }     
 }
];

标签: typescriptangular6

解决方案


the problem you are facing is in here

 {
    path: 'survey',
    component: SurveyComponent,
    data: {
    title: 'Survey' 
 } 

as you cannot set a path to a component which is nested into another module you need to push that component to the declarations array in app module or you set it into Consultation Module Routing file and would be better to make it lazy loaded


推荐阅读