【发布时间】:2021-03-15 12:42:13
【问题描述】:
我们有一个带有 2 个选项卡的基本应用程序,每个选项卡都有自己的命名插座
<mat-tab-group>
<mat-tab label="Tab1">
<router-outlet name="tab1"></router-outlet>
</mat-tab>
<mat-tab label="Tab2">
<router-outlet name="tab2"></router-outlet>
</mat-tab>
</mat-tab-group>
2 个命名的 outlet 之一只是渲染一个组件,这很好。对于第二个,我们希望能够在不同视图之间进行路由,因此我们创建了自己的模块并延迟加载它。
应用路由看起来像
const routes: Routes = [{
path: '',
children: [{
path: '',
redirectTo: '/home',
pathMatch: 'full'
}, {
path: 'login',
loadChildren: () => import('./login/login.module').then(m => m.LoginModule)
},
{
path: 'home',
loadChildren: () => import('./home/home.module').then(m => m.HomeModule)
}]
}]
首页的路由看起来像
const routes: Routes = [{
path: '',
component: HomeComponent,
children: [{
path: '',
component: Tab1Component,
outlet: 'tab1'
}, {
path: '',
outlet: 'tab2',
loadChildren: () => import('./tab2/tab2.module').then(m => m.Tab2Module)
}]
}];
Tab2Module 实现如下
const routes: Routes = [{
path: '',
component: Tab2Child1Component
}, {
path: 'tab2-child',
outlet: 'tab2',
component: Tab2Child2Component
}]
Tab2Child1Component 渲染良好,但我们无法导航到 Tab2Child2Component。
我正在尝试以下方法:
this.router.navigate([{ outlets: { tab2: 'tab2-child' } }], { relativeTo: this.route });
我可以在跟踪中看到路由器确实在尝试路由到 /home/(tab2:tab2-child),但最后它只是重定向回 /home 并出现错误:
Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home'
Error: Cannot match any routes. URL Segment: 'home'
Stackblitz 展示错误: https://stackblitz.com/edit/angular-ivy-pv5jsi
【问题讨论】:
-
嘿,我不确定我是否理解正确,您希望它路由到:
https://angular-ivy-w8jqek.stackblitz.io/home;tab2=tab2-child吗?