2016-01-04 49 views

回答

17

我發現此解決方案

{ component: HomeComponent, path: "", pathMatch: "full" }, 
{ component: LoginComponent, path: "/login" }, 
{ component: NotFoundComponent, path: "**" } 
+0

感謝ü給了這樣的inforamtion,我創建了使用** @ routerconfig()**中的子組件的子視圖,我能夠用** ** routerLink導航。例如:Parent:'#/ sales'和Childs:'#sales/asia'&'#sales/eu',點擊鏈接;孩子的意見是完美的工作。但我沒能initally加載'#銷售/ asia',我已經使用缺省路由器選項仍然它不工作。如何在加載'#sales'視圖時加載'#/ sales/asia' initailly?提前致謝。 –

10

重定向默認路由(/),請參閱Route api docs。只需設置默認路由的useAsDefault參數true

舉例來說,如果你有一個路線定義如下:

@RouteConfig([ {path: '/home', component: HomeCmp, name: 'Home', useAsDefault: true} ]) 

/路線將被重新路由到/home

正如OP提到在他的回答中,將所有未定義路線重定向到某一路線,請使用

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

你試過嗎? –

+0

是的。當我到達一臺電腦時,我會嘗試搭建一個有效的示例。如果遇到問題,可能是服務器未配置爲將未知路由路由到您的index.html文件。嘗試使用'HashLocationStrategy'或查看如何設置您的服務器/ dev服務器以將未知路由路由到您的主html文件。 (儘管這應該只在直接在地址欄中設置路徑或刷新頁面時出現)。嘗試在使用useAsDefault時將'routerLink'設置爲未知路由,並且它應該正確重新路由。 –

+0

這不適合我。 –

3

路由器更改爲新版本,現在回退的路由以這種方式用戶。 參考:Angular 2 route

{ path: '', redirectTo: '/member/dashboard', terminal: true }, 
{ 
    path: 'member', 
    component: MemberMainComponent, 
    canActivate: [AuthGuard], 
    children: [ 
    { 
     path: 'dashboard', 
     component: DashboardComponent 
    } 
    ] 
}, 
{ path: '**', component: DashboardComponent }