2

I am implementing lazy loading Below is the same problem solution and I am implementing same but again getting the error. Lazy loading error on stackoverflow question I exported the components from project module and imported project module in app.module.ts

Below is my main app.module.ts file

App.module.ts

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { DashComponent } from './dash/dash.component';
    import { FourzerofourComponent } from './fourzerofour/fourzerofour.component';
import { ProjectModule } from './project/project.module';

    @NgModule({
      declarations: [
        AppComponent,
        DashComponent,
        FourzerofourComponent,


      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        ProjectModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }

Below is my another module file which is project.module.ts. I am setting up lazy loaidng for that module

Project.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ProjectRoutingModule } from './project-routing.module';
import { ProjectComponent } from './project.component';
import { ProjectListComponent } from './project-list/project-list.component';
import { ProjectDetailsComponent } from './project-details/project-details.component';

@NgModule({
  declarations: [ProjectComponent, ProjectListComponent, ProjectDetailsComponent],
  imports: [
    CommonModule,
    ProjectRoutingModule
  ],
  exports: [ProjectComponent, ProjectListComponent, ProjectDetailsComponent]
})
export class ProjectModule { }

Below is my app-routing module where I am loading project module in it App-module.routing.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DashComponent } from './dash/dash.component';
import { FourzerofourComponent } from './fourzerofour/fourzerofour.component';



const appRoutes: Routes = [
 {
   path:'dash',
   component:DashComponent
 },
 {
  path:'projects',
  loadChildren:'./project/project.module#ProjectModule'
},

 {
   path: '',
   redirectTo: '/dash',
   pathMatch: 'full'
 }, 
 {
   path:"**",
   component:FourzerofourComponent
 }






];  




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

 }

The error which i am getting is

core.js:15723 ERROR Error: Uncaught (in promise): Error: Component ProjectComponent is not part of any NgModule or the module has not been imported into your module.
Error: Component ProjectComponent is not part of any NgModule or the module has not been imported into your module.
    at JitCompiler.push../node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._createCompiledHostTemplate (compiler.js:26121)
    at compiler.js:26097
    at Array.forEach (<anonymous>)

project-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {ProjectComponent} from './project/project.component';
import {ProjectListComponent} from './project-list/project-list.component';
import {ProjectDetailsComponent} from './project-details/project-details.component';


const projectRoutes: Routes = [

{ 
  path: '',
  component:ProjectComponent,
  children:[
    {
      path:'',
      component:ProjectListComponent
    }, {
      path: ':id',
      component:ProjectDetailsComponent
    }
  ]
}

];

@NgModule({
  imports: [RouterModule.forChild(projectRoutes)],
  exports: [RouterModule]
})
export class ProjectRoutingModule { }
1
  • you don't need to import ProjectModule in your AppModule since your a lazy loading it. Commented Mar 7, 2019 at 0:29

1 Answer 1

2

Since you are lazy loading your ProjectModule there is no need to import this module into AppModule which is probably causing the conflict.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { DashComponent } from './dash/dash.component';
import { FourzerofourComponent } from './fourzerofour/fourzerofour.component';

@NgModule({
  declarations: [
     AppComponent,
     DashComponent,
     FourzerofourComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.