在角2角1,否则路线等效 [英] Equivalent of Angular 1 otherwise route in Angular 2
问题描述
我用角2路由我的应用程序和它的作品pretty很好,但我不知道如何界定,否则路线。所以,如果没有,如果当前URL不对应任何支持的路线将要显示的路线。
下面是我当前的配置的例子:
@RouteConfig([
{路径:'/',名称:'家',组成:StoryComponent,useAsDefault:真正},
{路径:'/故事,名称:'故事',成分:StoryComponent},
{路径:'/订阅,名称:'认购',成分:SubscriptionComponent}
])
有在角2不',否则'路线呢。但可以使用通配符参数来实现相同的功能,像这样:
@RouteConfig([
{路径:'/',redirectTo:['家']},
{路径:'/家,名称:'家',组成:HomeComponent},
//所有其他路线,终于在最后的加
{路径:'/ *路径,成分:NOTFOUND}
这只会加载NOTFOUND组件和URL将同您导航到什么。如果你希望所有不匹配的路由重定向到一个404的网址,你可以这样做:
//在路由定义的结束
{路径:'/ 404',名称:'404',成分:PageNotFoundComponent},
{路径:'/ *路径,redirectTo:['404']}
I'm using Angular 2 routing for my application and it works pretty well but I have no idea how to define the "otherwise" route. So a route that will be displayed if none if the current URL does not correspond to any "supported" route.
Here is an example of my current configuration:
@RouteConfig([
{ path: '/', name: 'Home', component: StoryComponent, useAsDefault: true },
{ path: '/story', name: 'Story', component: StoryComponent },
{ path: '/subscription', name: 'Subscription', component: SubscriptionComponent}
])
There is no 'otherwise' route in angular 2 yet. But the same functionality can be achieved using wildcard parameter, like so:
@RouteConfig([
{ path: '/', redirectTo: ['Home'] },
{ path: '/home', name: 'Home', component: HomeComponent },
// all other routes and finally at the last add
{path: '/*path', component: NotFound}
This will only load the 'NotFound' component and the url will be same as what you navigate to. In case you want all not matching routes to redirect to a '404' url, you can do something like:
//at the end of the route definitions
{ path: '/404', name: '404', component: PageNotFoundComponent },
{ path: '/*path', redirectTo:['404'] }
这篇关于在角2角1,否则路线等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!