简体   繁体   中英

Angular 2 - Fallback route not working

I have an Angular2 project built with this Angular 2 Webpack Starter but I cannot get the fallback route to work correctly. In my app.routes.ts I have:

import { Routes } from '@angular/router';
import { HomeComponent } from './home';
import { DataResolver } from './app.resolver';

export const ROUTES: Routes = [
  {
     path: '',
     component: HomeComponent
  },
  {
     path: 'getstarted', loadChildren: './getstarted#GetStartedModule'
  },

  ...
  {
     path: 'notfound', loadChildren: './notfound#NotFoundModule'
  },
  {
     path: '**', loadChildren: './notfound#NotFoundModule'
  },
];

The not found path above works correctly but the fallback route ( ** ) does not work correctly. Instead of showing the NotFoundModule it does not load a module at all and I get no errors. However, when I do this it redirects correctly:

  ...
  {
     path: 'notfound', loadChildren: './notfound#NotFoundModule'
  },
  {
    path: '**', redirectTo:'/notfound', pathMatch: 'full'
  },
];

I do not want to redirect though because I do not want to change the url to /notfound by redirecting. How can I make my top version work or what else can I do to make this work?

So, I just tried it and it seems that you cannot use lazy routes to set your fallback page. This should work :

export const ROUTES: Routes = [
  {
     path: '',
     component: HomeComponent
  },
  {
     path: 'getstarted', loadChildren: './getstarted#GetStartedModule'
  },

  ...
  {
     path: 'notfound', loadChildren: './notfound#NotFoundModule'
  },
  {
     path: '**', component : NotFoundComponent
  },
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM