Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

Multiple named router-outlet angular 2

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"

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

In my router i want to specify the name of router.

As i saw in a question, the solution for this is to specify AuxRoute, but AuxRoute does not exist in this version.

{path: '/',        component: HomeComponent, as: 'Home'},
new AuxRoute({path: '/route1', component: Route1Component, as: 'Route1'}),
new AuxRoute({path: '/route2', component: Route2Component, as: 'Route2'})

While on angular 2 official website i found that it is possible to have multiple router, but i can't find any resource.

https://angular.io/docs/ts/latest/guide/router.html

A template may hold exactly one unnamed <router-outlet>. The router supports multiple named outlets, a feature we'll cover in future.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...