Angular2 - 路由器不接收路由/归属路径

时间:2017-03-28 17:39:51

标签: javascript angular typescript angular2-routing

我使用此Angular2 Webpack Starter和Angular2应用。在将模块(app.routes.ts)添加到NotFoundModule之后,我刚刚向我的app.module.ts文件添加了一个后备路由,除了现在我的主路径('')没有注册外,一切都很好再加上NotFoundComponent加载。代码如下:

import { Routes } from '@angular/router';
import { HomeComponent } from './home';
import { NotFoundComponent } from './notfound/notfound.component';

import { DataResolver } from './app.resolver';

export const ROUTES: Routes = [
  {
     path: '', component: HomeComponent },
  {
     path: 'getstarted', loadChildren: './getstarted#GetStartedModule'
  },
  {
     path: 'open-account', loadChildren: './open-account/#OpenAccountModule'
  },
  ...
  ...
  ...
  {
    path: '**', component: NotFoundComponent
  },
];

如何解决此问题,以便我的主页路由再次正常运行,NotFoundComponent将无法加载到位?

1 个答案:

答案 0 :(得分:1)

当您的路径使用''作为路径并且没有孩子时,您需要为该路线指定pathMatch: 'full'

export const ROUTES: Routes = [
  {
     path: '', pathMatch: 'full', component: HomeComponent },
  {
     path: 'getstarted', loadChildren: './getstarted#GetStartedModule'
  },
  {
     path: 'open-account', loadChildren: './open-account/#OpenAccountModule'
  },
  ...
  ...
  ...
  {
    path: '**', component: NotFoundComponent
  },
];

请参阅https://angular.io/docs/js/latest/api/router/index/Routes-type-alias.html#!#matching-strategy了解原因。