有一個路由配置如下:
const routes: Routes = [
{
path:'home',
component:HomeComponent
},
{
path:'news',
component:NewsComponent
},
{
path:'',
redirectTo:'home',
pathMatch: 'full'//pathMatch:prefix和full,prefix表示以path開頭,如path:'a' 實際訪問的路徑爲'a/b',就可以匹配到
//full:path完全匹配
},
{
path:'**',//上面匹配不上的路徑,都匹配到這
redirectTo:'home'
}
];
路由地址爲:
1 "localhost:4200/home",匹配到Home組件
2 "localhost:4200/news",匹配到News組件
3 "localhost:4200/",匹配到Home組件
4 "localhost:4200/user",由於沒有配置user對應的組件,則匹配到**,最後轉發到"localhost:4200/home",顯示爲Home組件