2017-08-17 53 views
0

在我的路由器配置中,我有一個DashboardComponent在路徑'dashboard'下配置,我想在沒有指定路徑時自動將用戶重定向到此路由(空路徑,所以只是/)。主要路線角度無效配置(空路徑)

這是我的代碼:

const appRoutes: Routes = [ 
    { path: '', redirectTo: 'dashboard'}, 
    { path: 'dashboard', component: DashboardComponent }, 
    /* other routes... */ 
]; 

未處理的承諾拒絕:航​​線的無效配置 '{路徑: 「」,redirectTo: 「儀表板」}':請提供 'pathMatch'。 「pathMatch」的默認 值爲「前綴」,但往往意圖是使用 「全」

回答

1

的問題是缺少空航線的pathMatch屬性,默認爲prefix

然而,在這種情況下,pathMatch值應該被設置爲full

const appRoutes: Routes = [ 
    { path: '', redirectTo: 'dashboard', pathMatch: 'full'}, 
    { path: 'dashboard', component: DashboardComponent }, 
    /* other routes... */ 
]; 

其原因如下:在一個

技術上,pathMatch = '滿' 的結果剩餘的 路徑命中,URL中匹配不匹配的部分匹配''。在此示例中, 重定向位於頂層路由中,因此剩餘的URL和整個URL都是相同的。

另一個可能的pathMatch值是'prefix',它告訴路由器 匹配重定向路由,當其餘的URL以 重定向路由的前綴路徑開頭時。

不要這樣做。如果pathMatch值是'prefix',則每個URL 都將匹配''。

欲瞭解更多詳情,請參閱官方文檔:https://angular.io/guide/router#redirecting-routes

+0

你應該紀念你的答案被接受。 –

+0

@AndreiMatracaru Stackoverflow允許我在2天后才能接受我自己的答案 – ShinDarth