1

Angular2 lazy loading a route issue.

I'm using Angular2, typscript, html5 and systemjs.

I'm trying to get lazy loading working for one of my basic routes. This is the blog I'm following but I can't seem to get it working: http://blog.angular-university.io/angular2-ngmodule/

This is the console error I get:

Uncaught (in promise): Error: Cannot match any routes. URL Segment: '500'

My page is the 500 page. Below I have added my files at there current state.

Module for 500 page:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Component500 } from './500.component';
import { ModuleRouting500 } from './500.routes';

@NgModule({
  imports: [RouterModule, ModuleRouting500],
  declarations: [Component500],
  exports: [Component500],
  providers: []
})
export default class Module500 { }

Route for 500 page:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Component500 } from './index';

const Routes500: Routes[] = [
  {
    path: '',
    loadChildren: Component500
  }
];

@NgModule({
  imports: [RouterModule.forChild(Routes500)],
  exports: [RouterModule],
  providers: []
})

export class ModuleRouting500 { }

This is my core app routes page: (I don't add the route 500 here)

import { Routes } from '@angular/router';
import { HomeRoutes } from './components/home/index';

export const routes: Routes = [
...HomeRoutes,

{ path: '500', loadChildren: 'app/components/500/500.module#Module500' } ];

This is my core app module page: (I don't add the module 500 here)

import { NgModule } from '@angular/core';
import { routes } from './app.routes';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { FormsModule} from '@angular/forms';
import { APP_BASE_HREF, CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';

import { HomeModule } from './components/home/home.module';
import { AuthService } from './services/authService/authService';
import { Environment } from './models/environment/environment';

@NgModule({
  imports: [BrowserModule, FormsModule, CommonModule, HttpModule, RouterModule.forRoot(routes), 
  HomeModule
  ],
  declarations: [AppComponent],
  providers: [{
    provide: APP_BASE_HREF,
    useValue: '<%= APP_BASE %>'},
    AuthService,
    Environment
  ],
  bootstrap: [AppComponent]
})

export class AppModule { }

This is the 500 page index.ts file:

export * from './500.component';
export * from './500.routes';

This is a screen grab of my folder structure:

enter image description here

This is a screen grab of my console error I currently get:

enter image description here

Full folder structure:

enter image description here

1 Answer 1

1

You have to add your lazy route in the App route by doing this:

App routes

export const routes: Routes = [
    ...,
    { path: '500', loadChildren: 'app/components/500/500.module#Module500' },
];

You have to change your 500.routes.ts to a module like:

500.routes.ts:

import { NgModule, ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { Component500 } from './index';

const routes: Routes = [
  {
    path: '', 
    component: Component500,
  }
];

@NgModule({
  imports: [RouterModule.forChild(Routes500)],
  exports: [RouterModule],
  providers: []
})

export const routing: ModuleWithProviders = RouterModule.forChild(routes);

Then, in 500.module.ts you have to load the 500.routes.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Component500 } from './500.component';
import { routing  } from './500.routes';

@NgModule({
  imports: [RouterModule, routing],
  declarations: [Component500]
})
export class Module500 { }

Now, every module knows the routes, being it lazy or not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.