我之前在AngularJS中使用了ui-router,然后在Angular 2+中使用了ui-router。我想转移到@ angular / router只是因为它似乎更受支持并且更多的库建立在它上面(ui-router-ng2仍在测试中!)
我的问题是我使用的是具有命名路由出口的主模板,因为主模板具有可重用的静态页眉和页脚。 (还有一个侧栏,我为了简洁而拿出来)
我的问题是,一旦我进入我的' / mainDashboard'路由器我无法从该路由导航到下一个嵌套/子路由' / otherDashboard'同时尝试在主模板'内容'
中使用指定的路由器插座我应该如何在下面的代码的第十三行导航到我的路线。 还有plunker https://plnkr.co/edit/RErIiby535x0toaA02W4?p=preview
这是我的代码
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 { }
答案 0 :(得分:5)
在玩了你的plunker代码后,我发现了这个问题。由于otherDashboard
是mainDashboard
的孩子,因此otherDashboard
内mainDashboard
的路由器插座应该是otherDashboard
。添加完成后,我可以使用router.navigateByUrl
导航至HomeComponent
,因此您的@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))');
}
}
将如下所示:
HomeComponent
如果您想将OtherDashboardComponent
替换为MainDashboardComponent
本身的otherDashboard
,则mainDashboard
和HomeComponent
中的Contact
应该是同一父母的子女prefetch_related
{1}}