Using Auxiliary Routes

Angular supports the concept of auxiliary routes, which allow you to set up and navigate multiple independent routes in a single app. Each component has one primary route and zero or more auxiliary outlets. Auxiliary outlets must have unique name within a component.

To define the auxiliary route we must first add a named router outlet where contents for the auxiliary route are to be rendered.

Here's an example:

  1. import {Component} from '@angular/core';
  2. @Component({
  3. selector: 'app',
  4. template: `
  5. <nav>
  6. <a [routerLink]="['/component-one']">Component One</a>
  7. <a [routerLink]="['/component-two']">Component Two</a>
  8. <a [routerLink]="[{ outlets: { 'sidebar': ['component-aux'] } }]">Component Aux</a>
  9. </nav>
  10. <div style="color: green; margin-top: 1rem;">Outlet:</div>
  11. <div style="border: 2px solid green; padding: 1rem;">
  12. <router-outlet></router-outlet>
  13. </div>
  14. <div style="color: green; margin-top: 1rem;">Sidebar Outlet:</div>
  15. <div style="border: 2px solid blue; padding: 1rem;">
  16. <router-outlet name="sidebar"></router-outlet>
  17. </div>
  18. `
  19. })
  20. export class AppComponent {
  21. }

Next we must define the link to the auxiliary route for the application to navigate and render the contents.

  1. <a [routerLink]="[{ outlets: { 'sidebar': ['component-aux'] } }]">
  2. Component Aux
  3. </a>

View Example

Each auxiliary route is an independent route which can have:

  • its own child routes
  • its own auxiliary routes
  • its own route-params
  • its own history stack

原文: https://angular-2-training-book.rangle.io/handout/routing/aux-routes.html