I'm trying to create a lazy load in my angular4 project, follow all the steps according to the documentation and nothing.
Here is my code below:
StudentModule:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StudentComponent } from './student.component';
import { StudentNotFoundComponent } from './student-not-found/student-not-found.component';
import { StudentFormComponent } from './student-form/student-form.component';
import { StudentDetailComponent } from './student-detail/student-detail.component';
import { StudentService } from './student.service';
@NgModule({
imports: [
CommonModule
],
declarations: [
StudentComponent,
StudentFormComponent,
StudentDetailComponent,
StudentNotFoundComponent
],
providers: [
StudentService
]
})
export class StudentModule { }
StudentRoutingModule:
import { ModuleWithProviders } from '@angular/core';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { StudentNotFoundComponent } from './student-not-found/student-not-found.component';
import { StudentFormComponent } from 'app/student/student-form/student-form.component';
import { StudentDetailComponent } from './student-detail/student-detail.component';
import { StudentComponent } from './student.component';
const student : Routes = [
{path : '', component : StudentComponent, children: [
{path : 'new', component : StudentFormComponent},
{path : ':id', component : StudentDetailComponent},
{path : ':id/edit', component : StudentFormComponent},
{path : 'student-not-found', component : StudentNotFoundComponent}
]}
];
@NgModule({
imports : [RouterModule.forChild(student)],
exports : [RouterModule]
})
export class SchoolClassRoutingModule { }
AppRoutingModule:
import { ModuleWithProviders } from '@angular/core';
import { RouterModule, Routes, LoadChildren } from '@angular/router';
import { NgModule } from '@angular/core';
import { HomeComponent } from './home/home.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { AuthGuard } from './guard/auth.guard';
const APP_ROUTE: Routes = [
{
path: 'student',
component: StudentComponent,
loadChildren: 'app/student/student.module#StudentModule',
canLoad: [AuthGuard]
},
{ path: 'login', component: LoginComponent},
{ path: 'home', component: HomeComponent},
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent}
];
@NgModule({
imports : [RouterModule.forRoot(APP_ROUTE)],
exports : [RouterModule]
})
export class AppRoutingModule { }
And AppModule:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { ModuleWithProviders } from '@angular/core';
import { MaterializeModule } from 'angular2-materialize';
import { AppComponent } from './app.component';
import { LoggedConfig } from './config/logged.config';
import { TokenConfig } from './config/token.config'
import { AuthGuard } from './guard/auth.guard';
import { AuthenticationService } from './authentication/authentication.service';
import { LoginComponent } from './login/login.component';
import { AdministratorComponent } from './administrator/administrator.component';
import { HomeComponent } from './home/home.component';
import { NavbarComponent } from './navbar/navbar.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { AppRoutingModule } from 'app/app.routing.module';
@NgModule({
declarations: [
AppComponent,
LoginComponent,
AdministratorComponent,
HomeComponent,
NavbarComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
MaterializeModule,
AppRoutingModule
],
providers: [
AuthGuard,
AuthenticationService,
LoggedConfig,
TokenConfig
],
bootstrap: [AppComponent]
})
export class AppModule { }
Console error:
I have read the documentation, I have already seen several examples in github and all do what I am doing. Help!!!
component: StudentComponentfrompath: 'student', seems unnecessary as you are already adding it in default child path. Also since the student module is loaded lazily component will not be avilable to be used in AppModule unless you import them.