简体   繁体   English

Angular 2-后备路线不起作用

[英]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. 我有一个与此Angular 2 Webpack Starter一起构建的Angular2项目,但是我无法获得后备路由来正常工作。 In my app.routes.ts I have: 在我的app.routes.ts我有:

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. 上面not found路径可以正常工作,但后备路由( ** )不能正常工作。 Instead of showing the NotFoundModule it does not load a module at all and I get no errors. 除了显示NotFoundModule它根本不会加载模块,并且我没有收到任何错误。 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. 我不想重定向,因为我不想通过重定向将URL更改为/notfound 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
  },
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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