I run into troubles when using multiple router-outlet.
I would like to use 2 router-outlet at the same time. Here a hierarchical view of the components display on which outlet and which url:
- outlet 1: app-list => url: /app
- outlet 2: app-detail (optional) => url: /app/1
- outlet 1: srv-list => url: /srv
- outlet 2: srv-detail (optional) => url: /srv/1
For example, srv-detail should not be displayed when the app-list is.
This is my attempt (I've reduced my code the most I could). There is no error in the browser console. :( And I'm confused when manipulating named router-outlets.
app-list-component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-list',
template: '<h2>AppList</h2>',
styleUrls: []
})
export class AppListComponent {}
app-detail-component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-detail',
template: '<h2>App Detail</h2>',
styleUrls: []
})
export class AppDetailComponent {}
srv-list-component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'srv-list',
template: '<h2>Srv list</h2>',
styleUrls: []
})
export class SrvListComponent {}
srv-detail-component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'srv-detail',
template: '<h2>Srv detail</h2>',
styleUrls: []
})
export class SrvDetailComponent {}
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template:
'<div><h1>Title 1</h1><router-outlet name="menu"></router-outlet></div>' +
'<div><h1>Title 2</h1><router-outlet name="content"></router-outlet></div>',
styleUrls: []
})
export class AppComponent {
/* In future, by subscribing to the router, we will be able to hide the content
outlet if there is only one '/' in the route (eg. /app and not /app/3) */
}
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { AppListComponent } from './app-list.component';
import { AppDetailComponent } from './app-detail.component';
import { SrvListComponent } from './srv-list.component';
import { SrvDetailComponent } from './srv-detail.component';
const routes : Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'app'
},
{
path: 'app',
component: AppComponent,
children: [
{
path: '',
component: AppListComponent,
outlet: 'menu'
},
{
path: ':id',
component: AppDetailComponent,
outlet: 'content'
}
]
},
{
path: 'srv',
component: AppComponent,
children: [
{
path: '',
component: SrvListComponent,
outlet: 'menu'
},
{
path: ':id',
component: SrvDetailComponent,
outlet: 'content'
}
]
}
]
@NgModule({
declarations: [
AppComponent,
AppListComponent,
AppDetailComponent,
SrvListComponent,
SrvDetailComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(routes)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Do you guys have some ideas? Thanks.
Edit : The plunker.