组件不是任何NgModule的一部分,或者模块尚未导入模块(延迟加载)

时间:2019-04-10 11:11:41

标签: angular lazy-loading ng-modules

我正在Angular7应用程序上工作,并遇到一个奇怪的错误,我的组件'x'不属于任何NgModule,或者该模块尚未导入到您的模块中。

组模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { GroupRoutingModule } from './group-routing.module';
import { GroupComponent } from './group/group.component';
import { GroupListComponent } from './group-list/group-list.component';

@NgModule({
  declarations: [

   GroupComponent,
   GroupListComponent 

  ],
  imports: [

     CommonModule,
     FormsModule,
     GroupRoutingModule

  ],
  exports: [
    GroupComponent,
    GroupListComponent 
  ]
})
export class GroupModule { }

用户模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CorrCommonModule } from '../../../common/common.module';
import { FormsModule } from '@angular/forms';
import { UserComponent } from './user/user.component';
import { UserRoutingModule } from './user-routing.module';


@NgModule({
  declarations: [
    UserComponent    
  ],
  entryComponents: [
  ],
  imports: [
     CommonModule,
     FormsModule,
     UserRoutingModule
  ],
  exports: [
    UserComponent        
  ]
})
export class UserModule { }

然后将这两个模块都添加到安全模块中,该模块是延迟加载的模块。

安全模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule} from '@angular/forms';
import { SecurityRoutingModule } from './security-routing.module';
import { UserModule } from './components/user/user.module';
import { GroupModule } from './components/group/group.module';


@NgModule({
  declarations: [
  ],
    entryComponents: [
    ],
    exports: [
    ],
  imports: [
    CommonModule,
    FormsModule,
    SecurityRoutingModule,
    GroupModule,
    UserModule
  ],
  providers: []
})
export class SecurityModule {}

用户路由模块:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UserComponent } from './user/user.component';
import { GroupListComponent } from '../group/group-list/group-list.component';

const Routes: Routes = [
  {
    path: 'user',
    component: UserComponent
    },
     {
       path: 'group',
       component: GroupListComponent
     }
];

@NgModule({
  imports: [RouterModule.forChild(Routes)],
  exports: [RouterModule]
})
export class UserRoutingModule { }

现在,当我在用户路由模块中使用GroupListComponent时,我遇到的问题是 Component不属于任何NgModule或模块尚未导入到您的模块中

编辑

此模块也使用类似的方法,但是没有这种问题。

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CountryComponent } from './country/country.component';
import { CurrencyListComponent } from '../currency/currency-list/currency-list.component';
import { SetupLogComponent } from '../log/setup-log/setup-log.component';
import { RegionListComponent } from '../region/region-list/region-list.component';
import { CityPlaceListComponent } from '../city-place/city-place-list/city-place-list.component';




const Routes: Routes = [
  {
    path: 'country',
    component: CountryComponent
  },
  {
     path: '', pathMatch: 'full',
     component: CountryDetailComponent
     },
     {
     path: 'region',
     component: RegionListComponent
     },
     {
     path: 'currency',
     component: CurrencyListComponent
     },
     {
     path: 'cityplace',
     component: CityPlaceListComponent
     }];




@NgModule({
  imports: [RouterModule.forChild(Routes)],
  exports: [RouterModule],
  declarations: []
})
export class CountryRoutingModule { }

编辑2: 应用程式程式码的一部分

{path: 'security',  loadChildren: './modules/security/security.module#SecurityModule', data: { preload: true }},
{path: 'setup', loadChildren: './modules/setup/setup.module#SetupModule', data: { preload: true }},

1 个答案:

答案 0 :(得分:1)

  

现在,当我在User-Routing模块中使用GroupListComponent时,我遇到的问题是Component不属于任何NgModule或该模块尚未导入到您的模块中

您的UserRoutingModule不会导入GroupModule版本,因此您无法使用它。

@NgModule({
  imports: [GroupModule, RouterModule.forChild(Routes)],
  exports: [RouterModule]
})
export class UserRoutingModule { }