I am trying to do multi-level menu routing.
The main menu component
@Component({
template: `
<h2>Main Menu</h2>
<ul>
<li><a [routerLink]="['/mainItem1']">Main Item 1</a></li>
<li><a [routerLink]="['/mainItem2']">Main Item 2</a></li>
more .......
</ul>
<router-outlet></router-outlet>
`,
directives: [RouterOutlet],
})
@Routes([
{path: '/mainItem1', component: MainItem1Component },
{path: '/mainItem2', component: MainItem2Component },
more .......
])
export class MainMenuComponent { }
One of the sub menu component
@Component({
template: `
<h2>Main Item 1</h2>
<ul>
<li><a [routerLink]="['main1sub1']">Main1 sub1 </a></li>
<li><a [routerLink]="['main1sub2']">Main1 sub2 </a></li>
many more .......
</ul>
<router-outlet></router-outlet>
<button type="button" (click)="goback()">Go back</button>
`,
directives: [RouterOutlet],
})
@Routes([
{path: 'main1sub1', component: Main1Sub1Component },
{path: 'main1sub2', component: Main1Sub2Component },
many more .......
])
export class MainItem1Component { }
When main menu component displays and I click on Main Item 1
the MainItem1Component
displays it self below the MainMenuComponent
.
I would expect when MainItem1Component
displays the MainMenuComponent
should go away. If the user want to go back to main menu, he/she can hit the Go back
button.
How can I achieve the desired behavior without defining all routes in the top level?
Just to clarify:
My question is about at which level to define the child @routes
and put <router-outlet>
so that when the use click on a parent menu item the child menu will show up while the parent menu should disappear.
I cannot define child @Routes
without providing <router-outlet>
at the same level. But then <router-outlet>
at different level "conflicts", meaning they holds their own content and wouldn't go away.
If I remove the <router-outlet>
at deeper level, @Routes
defined at that level won't work.