2017-08-30 59 views
0

這是我的錯誤:角度重新路由到首頁不起作用?

core.es5.js:1020 ERROR Error: Uncaught (in promise): 
Error: Cannot match any routes. URL Segment: 'home' 

這是從view.html代碼:

<div class="container"> 
    This is the main app; 
    <a routerLink="second">Click to go to second</a> 
    <a routerLink="third">Click to go to third</a> 
    <a routerLink="app">Go to Home</a> 
    <router-outlet></router-outlet> 
</div> 

這是我的目標,其中包括路徑到家庭和其他路徑的陣列。到家的路徑是正確的,因此我不明白爲什麼每次點擊它時彈出錯誤。

const appRoutes: Routes=[ 
    {path:'second', component:SecondComponent}, 
    {path:'third', component:ThirdComponent}, 
    {path:'', redirectTo:'./app', pathMatch:'full'}, 
] 

當我點擊第三和第二個組件url時,一切正常。

注意:當我點擊家庭組件時,我想隱藏第二個和第三個組件。

+0

您已經使用'routerLink =「home」',但沒有爲它創建路由。這樣做並檢查。 – abhig10

+0

我改變了它,但事情保持不變。我只想顯示其他組件正在「擦除」的主頁。我怎樣才能做到這一點? – masterach

+0

任何錯誤更改?如果沒有,請將'Go to Home'更改爲'Go to Home'並檢查。 – abhig10

回答

0

這解決了我的問題,我家的路徑是空的,爲此,一旦我點擊主頁標籤主頁不重複自己。

const APP_ROUTES: Routes=[ 
    {path:'first', component:FirstComponent}, 
    {path:'second', component:SecondComponent}, 
    {path:'third', component:ThirdComponent}, 
    {path:'', redirectTo:'', pathMatch:'full'}, 
]; 
0

您需要定義家用路線:

const appRoutes: Routes=[ 
    {path:'home', component:HomeComponent}, 
    {path:'second', component:SecondComponent}, 
    {path:'third', component:ThirdComponent}, 
    {path:'', redirectTo:'/home', pathMatch:'full'}, 
    {path:'**', redirectTo:'/home', pathMatch:'full'} 
]; 
+0

這,agian,只是重複我的主頁,當我按在我的家鏈接 – masterach

+1

這是實現這一目標的正確方法。如果它不能按預期工作,則問題不在於路由而是在視圖/組件實現中。無論如何,你的問題中的代碼片段沒有提供足夠的信息來幫助你 –