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

问题描述 投票:1回答:1

我正在处理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 { }

现在,当我在User-Routing模块中使用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 }},
angular lazy-loading ng-modules
1个回答
1
投票

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

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

@NgModule({
  imports: [GroupModule, RouterModule.forChild(Routes)],
  exports: [RouterModule]
})
export class UserRoutingModule { }
© www.soinside.com 2019 - 2024. All rights reserved.