404 pages and Lazy Loading in Angular2

只愿长相守 提交于 2019-12-04 12:04:31

URL matching wont work with wildcard lazy loaded modules. I wonder what you will add in the routing inside your Lazy loaded modules.

Having said that you may try to redirect to a different route for wildcard routes like below,

{ path: 'notfound', loadChildren: 'app/notfound/notfound.module#NotFoundModule'}
{ path: '**', redirectTo: '/notfound' }

Update

you may give an empty path in Module routing so that it loads default component,

const notfoundRoutes: Routes = [
  { path: '',  component: NotFoundComponent }
];

This makes sure that when you route to a path which is not configured it goes and loads NotFound module lazily.

See this Plunker!!

Hope this helps!!

Ok figured it out, what I did was the following.

imported the NotFound module in AppModule

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    CoreModule.forRoot(),
    NotFoundModule,
    routing
  ],
  declarations: [
    AppComponent
  ],
  providers: [  ],
  bootstrap: [AppComponent]
})

inside the NotFound module I declared a route (not a lazy one)

const routes: Routes =  [
  { 
    path: 'notfound', 
    component: NotFoundComponent
  }
]

in the AppModule routings I added

export const routes: Routes = [
  { path: '', redirectTo: 'dashboard', pathMatch: 'full'},
  { path: 'dashboard', loadChildren: 'app/dashboard/dashboard.module#DashboardModule'},
  { path: 'buckets', loadChildren: 'app/buckets/buckets.module#BucketsModule'},
  { path: '**', redirectTo: '/notfound' } 
];

This way the NotFound module is directly imported in the app, so it does exist in the application context.

Everytime you enter a unexistent route you get routed to /notfound ;)

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