I previously used ui-router in AngularJS then Angular 2+. I would like to move to the @angular/router simply because it seems to be more supported and more libraries built on it (ui-router-ng2 is still in beta!!)
My problem is that I am using a main template that has named route outlets because the main template has a static header and footer that is reused. (there is also a sidebar which i took out for brevity)
My problem is that once I get into my '/mainDashboard' router I am having trouble navigating from that route into the next nested/child route '/otherDashboard' whilst trying to use the named router outlet in the main template 'content'
How should I be navigating to my route in the thirteenth line of my code below. There is also a plunker https://plnkr.co/edit/RErIiby535x0toaA02W4?p=preview
Here is my code
import { NgModule, Component } from '@angular/core';
import { RouterModule, Router } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
@Component({
selector: 'app-home',
template: '<button (click)="go()">Go To Other Dashboard</button>'
})
export class HomeComponent {
constructor( public router: Router ) {}
go(){
//THIS ERRORS
this.router.navigate(['../dashboard',{outlets: {'content': ['../dashboard/mainDashboard/otherDashboard']}}]);
}
}
@Component({
selector: 'app-main-dashboard',
template: `
<header>My Static Header</header>
<div id="main-container" class="main-background">
<router-outlet name="content"></router-outlet>
</div>
<footer>My Static Footer</footer>
`
})
export class MainDashboardComponent {
constructor() {}
}
@Component({selector: 'app-login',template: '<button (click)="login()">Login</button>'})
export class LoginComponent {
constructor(public router: Router) { };
login(){
this.router.navigate(['/dashboard',{outlets: {content: ['mainDashboard']}}]);
}
}
@Component({selector: 'app-other-dashboard', template: '<h1>Hello Other Dashboard</h1>'})
export class OtherDashboardComponent{
constructor() { }
}
export const routes: Array<any> = [
{path: '',component: LoginComponent },
{path: 'dashboard', component: MainDashboardComponent,
children: [
{path: 'mainDashboard',component: HomeComponent,outlet: 'content',
children: [{path: 'otherDashboard',component: OtherDashboardComponent, outlet: 'content'}]
}
]
}
]
@Component({ selector: 'app-root', template: '<router-outlet></router-outlet>'})
export class AppComponent {}
@NgModule({
declarations: [ AppComponent, LoginComponent, MainDashboardComponent, HomeComponent, OtherDashboardComponent],
imports: [ BrowserModule, RouterModule.forRoot(routes) ],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }
After playing with your plunker code I found out the issue. As your otherDashboard
is child of mainDashboard
it is expected to have the router outlet for otherDashboard
inside mainDashboard
. Once that is added I was able to navigate to otherDashboard
using router.navigateByUrl
So your HomeComponent
will look like:
@Component({
selector: 'app-home',
template: '<button (click)="go()">Go To Other Dashboard</button><router-outlet name="content"></router-outlet>'
})
export class HomeComponent {
constructor( public router: Router ) {}
go(){
//Error gone
this.router.navigateByUrl('/dashboard/(content:mainDashboard/(content:otherDashboard))');
}
}
If you want to replace the HomeComponent
with OtherDashboardComponent
in MainDashboardComponent
itself then in both otherDashboard
and mainDashboard
should be children of same parent that is HomeComponent
Working code: https://plnkr.co/edit/jYOMNhbE3lX5UYmjS7qy?p=preview
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