Javascript 相当于角1,否则为角2中的路线

Javascript 相当于角1,否则为角2中的路线,javascript,angularjs,typescript,angular,Javascript,Angularjs,Typescript,Angular,我在应用程序中使用Angular 2路由,它工作得很好,但我不知道如何定义“否则”路由。因此,如果当前URL与任何“受支持”的路由不对应,则将显示一个路由(如果没有) 以下是我当前配置的一个示例: @RouteConfig([ { path: '/', name: 'Home', component: StoryComponent, useAsDefault: true }, { path: '/story', name: 'Story', component: StoryCom

我在应用程序中使用Angular 2路由,它工作得很好,但我不知道如何定义“否则”路由。因此,如果当前URL与任何“受支持”的路由不对应,则将显示一个路由(如果没有)

以下是我当前配置的一个示例:

@RouteConfig([
    { path: '/', name: 'Home', component: StoryComponent, useAsDefault: true },
    { path: '/story', name: 'Story', component: StoryComponent },
    { path: '/subscription', name: 'Subscription', component: SubscriptionComponent}
])

在angular 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”url,可以执行以下操作:

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

试试这个而不是别的。它对我有效,不确定,但似乎正在进行中

@RouteConfig([
  { path: '/**', redirectTo: ['MycmpnameCmp'] }, 
   ...
  }
])

这在angular 2中尚未实现。目前最好的解决方案是使用@Gary show这样的解决方案

{路径:'**',组件:PageNotFoundComponent}

如角度导轨布线部分()所示。

尝试此操作

RouterModule.forRoot([
    { path: 'login', component: LoginComponent },
    { path: 'edit-event', component: EventComponent },
    { path: 'participants', component: ParticipantsComponent },
    { path: 'notification', component: NotificationComponent },
    { path: '', component: WelcomeComponent }, //default
    { path: '**', component: WelcomeComponent } //page not found route
], { useHash: true })
useHash参数用于使用哈希url样式 这对我很有效:

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
    // all other routes
  { path: '**', redirectTo: '/home' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

该功能尚未实现,请检查,这不是您使用“useAsDefault”的原因吗?如果您重定向到子例程,则重定向不起作用。我建议只使用
NotFound
组件并注入
路由器
,这样就可以在构造函数内部重定向。