angular路由path:' ',與path:'**'的區別

有一個路由配置如下:

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組件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章