如何解决错误组件不是任何NgModule的一部分或未正确导入角度6中?

时间:2019-01-04 02:56:34

标签: javascript angular angular5 angular6 lazy-loading

这是我得到的错误:-

<b>Error: Component Component is not part of any NgModule or the module has not been imported into your module.
Error: Component Component is not part of any NgModule or the module has not been imported into your module.</b>

Click here to see the full error image

我希望我的安装模块被延迟加载,但是出现错误,例如组件不是模块的一部分或模块未导入到您的模块中

//This is my app.module.ts
import { BrowserModule, Title } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { LeaveService } from './EW_Leave/leave.service';
import { UtilsModule } from './EW_Utils/utils.module';
import { JobReferenceComponent } from '../app/EW_Utils/job-reference/job-reference.component'
import { CommonModule } from '@angular/common';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { EWDashboardComponent } from './EW_Dashboard/ew-dashboard.component';
import { EWLoginComponent } from './EW_Login/ew-login.component';


@NgModule({
  imports: [
    StorageServiceModule,
    CommonModule,
    BrowserModule,
    FormsModule, ReactiveFormsModule,
    AppRoutingModule,
    UtilsModule, LeaveModule, 
  ],
  declarations: [
    AppComponent, JobReferenceComponent, EWDashboardComponent, EWLoginComponent
  ],
  exports: [],
  providers: [LeaveService, Title],
  bootstrap: [AppComponent]
})
export class AppModule {




}

//这是我的app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { EWDashboardComponent } from './EW_Dashboard/ew-dashboard.component';
import { EWLoginComponent } from './EW_Login/ew-login.component';
import { SetupDashboardComponent } from './EW_Setup/setup-dashboard/setup-dashboard.component';
import {CompaniesSetupComponent} from './EW_Setup/companies-setup/companies-setup.component';
const appRoutes: Routes = [
  { path: 'login', component: EWLoginComponent },
  { path: 'dashboard', component: EWDashboardComponent },
  { path: '', redirectTo: 'login', pathMatch: 'full' },
  { path: 'setup',

   loadChildren: './EW_Setup/setup.module#CustomersModule'

  },



]
@NgModule({
  imports: [RouterModule.forRoot(appRoutes)],
  exports: [RouterModule],
  declarations: []
})

export class AppRoutingModule {


}

下面是我要延迟加载的设置模块

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CompaniesSetupComponent } from './companies-setup/companies-setup.component';
import { SetupDashboardComponent } from './setup-dashboard/setup-dashboard.component';
import { BusinessUnitSetupComponent } from './business-unit-setup/business-unit-setup.component';

const appRoutes: Routes = [

  { path: '', component: SetupDashboardComponent},
]
@NgModule({
  imports: [RouterModule.forChild(appRoutes)],
  exports: [],
  declarations: []
})

export class SetupRoutingModule {


}

//这是我的SETUP.MODULE.TS文件

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UtilsModule } from '../EW_Utils/utils.module';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { SetupDashboardComponent } from './setup-dashboard/setup-dashboard.component';
@NgModule({
  imports: [
    CommonModule,UtilsModule,FormsModule,ReactiveFormsModule,NgxSpinnerModule,SharedModule,SetupRoutingModule,Ng2SearchPipeModule,GrowlerModule,TextareaAutosizeModule
  ],
  declarations: [ SetupDashboardComponent, SidebarMenuComponent, CompaniesSetupComponent, BusinessUnitSetupComponent],
  providers:[CompaniesSetupService,GrowlerService,CommonApiService],
  entryComponents:[]
})
export class SetupModule { }

2 个答案:

答案 0 :(得分:0)

从AppModule声明的组件不能由延迟加载的模块访问(这是为了保护延迟加载的模块的封装不受外部组件依赖性的影响)。

要从延迟加载的模块访问EWDashboardComponent,您需要执行以下操作之一:

  
      
  1. 从延迟加载的模块中声明组件
  2.   
  3. 在小部件模块中声明该组件,然后由您的延迟加载的模块导入(确保该组件也是   export-表示在导出数组中也是如此)
  4.   

如果EWDashboardComponent是共享组件,我建议使用选项2,因为它遵循Angular关于模块类型的指导:https://angular.io/guide/module-types(请参见小部件模块)。

答案 1 :(得分:0)

我有一个具有以下结构的项目:

  • gridster 模块包含 @app/modules/gridster/angular-grid/angulargridComponent

  • analytics 模块包含 @app/modules/analytics/components/analytics-one/AnalyticsOneComponent

注意:我的gridster模块已经通过导入使用了analytics模块。

由于路径和父子嵌套的不同,尝试访问angulargridComponent模块中的analytics时遇到了上述问题。

以前,我的分析路由为:

{ path: 'gross-profit', component: angulargridComponent}

我改为使用它,而不是导入使用:

{ path: 'gross-profit', loadChildren: '@app/modules/gridster/gridster.module#angulargridModule' }

angulargridModulegridster模块内部的模块类名称,它仅包含一个组件,即angulargridComponent