Angular2 后备路线

Angular2 fallback route

如果我转到不存在的路由,如何配置 Angular2 路由器将我重定向到默认路由(或任何其他路由)?

关于重定向默认路由 (/),请参阅 Route api docs。只需将默认路由的 useAsDefault 参数设置为 true

例如,如果您定义了如下路线:

@RouteConfig([ {path: '/home', component: HomeCmp, name: 'Home', useAsDefault: true} ])

/ 路线将是 re-routed 到 /home

正如 OP 在他的回答中提到的,要将所有未定义的路由重定向到特定路由,请使用

@RouteConfig([ /*...,*/ {path: '/**', redirectTo: ['Home']} ])

我找到了这个解决方案

{ component: HomeComponent, path: "", pathMatch: "full" },
{ component: LoginComponent, path: "/login" },
{ component: NotFoundComponent, path: "**" }

路由器已更改为新版本,现在以这种方式使用回退路由。 参考:Angular 2 route

{ path: '', redirectTo: '/member/dashboard', terminal: true },
{
  path: 'member',
  component: MemberMainComponent,
  canActivate: [AuthGuard],
  children: [
    {
      path: 'dashboard',
      component: DashboardComponent
    }
  ]
},
{ path: '**', component: DashboardComponent }