Multiple named router-outlet angular 2

前端 未结 1 1888
清酒与你
清酒与你 2020-11-29 03:06

I want to display different views by child routes. For Example I want my template to hold multiple router-outlet

Version: @angular/router\": \"3.0.0-alpha.7\"<

相关标签:
1条回答
  • 2020-11-29 03:52

    Current version multiple-named router-outlet (for angular2 RC.6^) looks like this:

    Router configuration

    const appRoutes: Routes = [{   
        path: 'home',
          component: HomeComponent,
          children: [
            { path: '', component: LayoutComponent },
            { path: 'page1', component: Page1Component, outlet: 'route1' },
            { path: 'page2', component: Page2Component, outlet: 'route2' },
            { path: 'page3', component: Page3Component, outlet: 'route3' }
          ]
        }, {
          path: 'articles',
          component: ArticlesComponent,
          children: [
            { path: '', component: LayoutComponent },
            { path: 'article1', component: Article1Component, outlet: 'route1' },
            { path: 'article2', component: Article2Component, outlet: 'route2' }
          ]
        },  { 
          path: '', 
          redirectTo: '/home',
          pathMatch: 'full'
       }
    ];
    

    Template within Home component:

    <router-outlet></router-outlet>
    <router-outlet name="route1"></router-outlet>
    <router-outlet name="route2"></router-outlet>
    <router-outlet name="route3"></router-outlet>
    

    And a navigation example from root component:

    constructor(router: Router) {
      router.navigateByUrl('/home/(route1:page1//route2:page2//route3:page3)');
    } 
    

    Alternative way:

    <a [routerLink]="['/home', { outlets: {'route1':['page1'],'route2': ['page2'] }}]"></a>
    

    Here's live Plunker Example

    See also

    • Named Router Outlets in Angular 2
    0 讨论(0)
提交回复
热议问题