2016-01-20 62 views
6

非匹配的路由我在角2重定向角2

更多细节寻找的$urlRouterProvider.otherwise相当于:

我有一个路由器的定义是这样的:

@RouteConfig([ 
    { path: "/", component: MultiPost, name: "Home", useAsDefault: true }, 
    { path: "/m/:collectionName/:categoryName/:page", component: MultiPost, name: "MultiPost" } 
]) 

两条路线都完美运行,例如当我点击//m/sth/sth/0时,我得到了我想要的。

但是当我点击一个像/kdfsgdshdjf这样的随机链接时,没有任何反应。由于没有路线匹配,页面呈现并且<router-outlet>为空。

我想要做的是将所有不匹配的路由重定向到默认路由。我需要类似$urlRouterProvider.otherwise("/");

有谁知道该怎么做?

回答

1
+0

坏消息对我来说,但谢谢你让我知道。 –

+1

是的,我也期待着这个,你仍然可以重定向到后端服务器。 – Langley

+0

它现在被实现,可以像这样使用: const appRoutes:Routes = [ {path:'**',component:PageNotFoundComponent} ]; –

6

有一个在角2不 '否则' 路线呢。但是,相同的功能可以使用通配符参数来实现,就像这样:

@RouteConfig([ 
    { path: '/', redirectTo: ['Home'] }, 
    { path: '/home', name: 'Home', component: HomeComponent }, 
    // all other routes and finally at the last add 
    {path: '/*path', component: NotFound} 

这只会加载“NOTFOUND”组件和URL将是一样的,你浏览一下。如果你想所有不匹配的路线重定向到'404'的网址,你可以做这样的事情:

//at the end of the route definitions 
{ path: '/404', name: '404', component: PageNotFoundComponent }, 
{ path: '/*path', redirectTo:['404'] } 
+0

@Langley答案是正确的,虽然这应该被接受,因为这也提供了解决方案。 –