Angular9 嵌套路由

2020-07-06 16:58 更新

隨著你的應(yīng)用變得越來(lái)越復(fù)雜,你可能要?jiǎng)?chuàng)建一些根組件之外的相對(duì)路由。這些嵌套路由類型稱為子路由。這意味著你要為你的應(yīng)用添加第二 <router-outlet>,因?yàn)樗?AppComponent 之外的另一個(gè) <router-outlet>。

在這個(gè)例子中,還有兩個(gè)子組件,child-achild-b。這里的 FirstComponent 有它自己的 <nav>AppComponent 之外的第二 <router-outlet>。

  1. //In the template
  2. <h2>First Component</h2>
  3. <nav>
  4. <ul>
  5. <li><a routerLink="child-a">Child A</a></li>
  6. <li><a routerLink="child-b">Child B</a></li>
  7. </ul>
  8. </nav>
  9. <router-outlet></router-outlet>

子路由和其它路由一樣,同時(shí)需要 pathcomponent。唯一的區(qū)別是你要把子路由放在父路由的 children 數(shù)組中。

  1. //AppRoutingModule (excerpt)
  2. const routes: Routes = [
  3. { path: 'first-component',
  4. component: FirstComponent, // this is the component with the <router-outlet> in the template
  5. children: [
  6. {
  7. path: 'child-a', // child route path
  8. component: ChildAComponent // child route component that the router renders
  9. },
  10. {
  11. path: 'child-b',
  12. component: ChildBComponent // another child route component that the router renders
  13. }
  14. ] },
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)