Angular2后备路线

Vla*_*vic 19 angular2-routing angular

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

Vla*_*vic 26

我找到了这个解决方案

{ component: HomeComponent, path: "", pathMatch: "full" },
{ component: LoginComponent, path: "/login" },
{ component: NotFoundComponent, path: "**" }
Run Code Online (Sandbox Code Playgroud)


Ion*_*ica 11

有关重定向默认路由(/)的信息,请参阅Routeapi文档.只需将默认路由的useAsDefault参数设置为true

例如,如果您的路由定义如下:

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

/路线将被重新路由至/home

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

@RouteConfig([ /*...,*/ {path: '/**', redirectTo: ['Home']} ])
Run Code Online (Sandbox Code Playgroud)


Vee*_*rra 6

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

{ path: '', redirectTo: '/member/dashboard', terminal: true },
{
  path: 'member',
  component: MemberMainComponent,
  canActivate: [AuthGuard],
  children: [
    {
      path: 'dashboard',
      component: DashboardComponent
    }
  ]
},
{ path: '**', component: DashboardComponent }
Run Code Online (Sandbox Code Playgroud)