Is it possible to have multiple router-outlet in the same template?
If yes then how to configure the routes?
I am using angular2 beta.
Angular Router supports multiple outlets in the same application. A component has one associated primary route and can have auxiliary routes.
Using named outlets and secondary routes, you can target multiple outlets in the same RouterLink directive.
There is no limit in angular but there are some best practices of using multiple place one in app.
yes you can, but you need to use aux routing. you will need to give a name to your router-outlet:
<router-outlet name="auxPathName"></router-outlet>
and setup your route config:
@RouteConfig([ {path:'/', name: 'RetularPath', component: OneComponent, useAsDefault: true}, {aux:'/auxRoute', name: 'AuxPath', component: SecondComponent} ])
Check out this example, and also this video.
Update for RC.5 Aux routes has changed a bit: in your router outlet use a name:
<router-outlet name="aux">
In your router config:
{path: '/auxRouter', component: secondComponentComponent, outlet: 'aux'}
You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows.
Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc.
Assuming on load you are bootstraping appComponent.
app.component.html
<div class="layout"> <div class="page-header"> //first outlet to load required component <router-outlet name='child1'></router-outlet> </div> <div class="content"> //second outlet to load required component <router-outlet name='child2'></router-outlet> </div> </div>
Add following to your router.
{ path: 'home', // you can keep it empty if you do not want /home component: 'appComponent', children: [ { path: '', component: childOneComponent, outlet: 'child1' }, { path: '', component: childTwoComponent, outlet: 'child2' } ] }
Now when /home is loaded appComponent will get load with allocated template, then angular router will check the route and load the children component in specified router outlet on the basis of name.
Like above you can configure your router to have multiple router-outlet in same route.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With