我有一个路由 UserComponent
(它被加载为"parent"路由: path: ''
)组件只包含一个 <router-outlet>
指令 . 从那里我想要导航到 StartPageComponent
(子根: path: ''
)如果 sartPage
属性设置为true,或者导航到 IdeasComponent
(子根: path: 'list'
)如果不是这种情况 . startPage
属性包含在 instanceData
对象中,该对象作为已解析数据传递给"parent"路由 . 虽然我'm doing the following: I'm检查 StartPageGuard
(实现 CanDeactivateChild
接口)中的 startPage
属性并尝试访问 route.data['instanceData'].startPage
(路由类型为ActivatedRouteSnapshot) . 当打印出 route
本身时,我可以't access it even if I'在控制台中看到 InstanceData
对象 . 打印_13811时,我只得到一个空对象 . 所以,如果有人能够解释我在那里发生了什么,也许我可以根据 startPage
属性的值阻止导航到根路径,这将是很好的 . 有我的代码: StartPageGuard
:
import { ActivatedRouteSnapshot, CanActivateChild, RouterStateSnapshot, Router, ActivatedRoute } from '@angular/router';
import { Injectable } from '@angular/core';
@Injectable()
export class StartPageGuard implements CanActivateChild {
constructor(private router: Router, private activeRoute: ActivatedRoute) {}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const showStartPage = route.data['instanceData'].startPage;
if (showStartPage) {
return true;
} else {
this.router.navigate(['/list'], {relativeTo: this.activeRoute});
}
}
}
appRoutingModule
的 routes: Routes
变量:
{
path: '',
component: UserComponent,
canActivateChild: [StartPageGuard],
resolve: {
instanceData: InstanceResolve
},
children: [
{
path: '',
component: StartPageComponent,
},
{
path: 'list',
component: IdeasComponent,
}
并且 UserComponent
仅包含 router-outlet
指令 .