Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2:页面刷新时未定义路由数据_Angular_Angular2 Routing - Fatal编程技术网

Angular 角度2:页面刷新时未定义路由数据

Angular 角度2:页面刷新时未定义路由数据,angular,angular2-routing,Angular,Angular2 Routing,刷新页面时,通过route Data属性传递的数据未定义。我使用的是路由保护,基于模块的privilegeId,canActivate返回可观察的布尔值 home.routing.ts export const HomeRoutes: Routes = [ { path: '', component: HomeComponent, canActivate: [AuthGuard], children: [

刷新页面时,通过route Data属性传递的数据未定义。我使用的是路由保护,基于模块的privilegeId,canActivate返回可观察的布尔值

home.routing.ts

export const HomeRoutes: Routes = [
    {
        path: '',
        component: HomeComponent,
        canActivate: [AuthGuard],
        children: [
            { path: '', component: DashboardComponent },
            {
                path: 'groups',
                loadChildren: 'app/modules/groups/groups.module#GroupsModule',
                data: { privilegeId: 5 }
            }
        ]
    },
];
export const HomeRouting = RouterModule.forChild(HomeRoutes);
canActivate(routeSnapshot: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
// Issue: privilegeId is undefined on page reload.
let privilegeId = routeSnapshot.data["privilegeId"] as number;

// Calls API with privilege id and returns boolean.
}
auth guard.service.ts

export const HomeRoutes: Routes = [
    {
        path: '',
        component: HomeComponent,
        canActivate: [AuthGuard],
        children: [
            { path: '', component: DashboardComponent },
            {
                path: 'groups',
                loadChildren: 'app/modules/groups/groups.module#GroupsModule',
                data: { privilegeId: 5 }
            }
        ]
    },
];
export const HomeRouting = RouterModule.forChild(HomeRoutes);
canActivate(routeSnapshot: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
// Issue: privilegeId is undefined on page reload.
let privilegeId = routeSnapshot.data["privilegeId"] as number;

// Calls API with privilege id and returns boolean.
}
canActivate(routeSnapshot:ActivatedRouteSnapshot,state:RouterStateSnapshot):可观察的布尔值{
//问题:页面重新加载时未定义privilegeId。
让privilegeId=routeSnapshot.data[“privilegeId”]作为编号;
//使用特权id调用API并返回布尔值。
}

刷新后,不知何故您无法访问父级防护中的路由数据

首先,保护您的孩子的最佳方法是使用
canActivateChild
而不是
canActivate

奇怪的是,仅在刷新之后,
ActivatedRouteSnapshot
才不在当前路由的范围内,这意味着当前路由没有路由数据

幸运的是,完整的路由包含在
routerstatesnashot
根属性(树)中

现在,您可以沿着这棵树走下去(每个树只包含一个子树),查看其中一个子树是否有任何数据:

searchData(状态:RouterStateSnapshot,数据键:string):string | null{
if(state.root.data&&state.root.data[dataKey])
返回state.root.data[dataKey];
let child:ActivatedRouteSnapshot | null;
child=state.root.firstChild;
while(child!=null){
if(child.data&&child.data[dataKey]){
返回child.data[dataKey];
}
child=child.firstChild;
}
返回null;
}
此方法在
RouterStateSnapshot
中搜索具有指定键的数据,并返回第一次出现(如果有)或null