2016-05-20 101 views
1

如果当前路由存在,或者如果它结束为404错误,是否可以使用Angular2的新路由器检测? 所以我可以将其设置回默认路由“/”。Angular 2新的路由器 - 检测404

我什么都没找到,我搜索了一个小时,但发现只有废弃的路由器的东西。

+0

是否所有醒目的'{路径: '/ **',redirectTo:“家庭']}'不再工作? – rinukkusu

回答

4

您可以在末尾添加这条路线的路线的这意味着角2 RC1路由器会去这个MyDefaultComponent如果没有其他定义路由的请求的URL匹配:

{ path: '**', component: MyDefaultComponent } 
1

您可以使用通配符做到这一点,无论是重定向到任何其他路线或有专门的404组件:

@RouteConfig([ 
    { path: '/', name: 'Welcome', component: WelcomeComponent, useAsDefault: true }, 
    { path: '/products', name: 'Products', component: ProductListComponent }, 
    { path: '/product/:id', name: 'ProductDetail', component: ProductDetailComponent }, 

    // Redirect option: 
    // { path: '/**', redirectTo:['Welcome'] }, 

    // Not found component option: 
    // {path: '/**', component: NotFoundComponent}, 

    // Both together: 
    { path: '/not-found', name: 'NotFound', component: NotFoundComponent}, 
    { path: '/**', redirectTo:['NotFound'] }, 
]) 

注意,在折角的版本,我现在使用,2.0.0-beta.15,如果你把刚刚path: '/*'它不起作用,see here

1

我有同样的问题。在角2的唯一的方式来做到按最新文档是

  1. 重定向到pagenotfound当路线不匹配任何图案

{ 路径:“**”, redirectTo: 'pagenotfound' }

  • 导航到组件,而不URL变化
  • { 路径: '**', 组件:SomeComponent }