Angular2 lazy loading a route issue

老子叫甜甜 提交于 2019-12-04 19:20:31
Bruno João

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!