6

I am looking for equivalent of $urlRouterProvider.otherwise in Angular 2.

More details:

I have a router definition like this:

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

Both routes are working perfectly, for instance when I hit / or /m/sth/sth/0 I got what I want.

But when I hit a random url like /kdfsgdshdjf nothing happens. Page is rendered and <router-outlet> is empty since no route is matched.

What I want to do is to redirect all non matching routes to a default one. I need something like $urlRouterProvider.otherwise("/");.

Does anyone knows how to do that?

4

2 回答 2

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}

这只会加载“未找到”组件,并且 URL 将与您导航到的相同。如果您希望所有不匹配的路由都重定向到“404”网址,您可以执行以下操作:

//at the end of the route definitions
{ path: '/404', name: '404', component: PageNotFoundComponent },
{ path: '/*path', redirectTo:['404'] }
于 2016-01-25T09:38:48.477 回答
1

此功能尚未实现:

https://github.com/angular/angular/issues/2965

于 2016-01-20T17:41:15.860 回答