我在angular2项目中工作。但是,我们的身份验证代码中存在一个问题。
我认为脚本不是等待可调用函数的结果。然后在“未定义”中生成变量控制台。
所以,请帮助我。
1)auth-guard.service.ts
@Injectable()
export class RoutePermissionGuard implements CanActivate{
private menuType:any;
constructor(
private router: Router,
private _shared:SharedService
) { }
// return only true or false
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):boolean {
let url: string = state.url;
let slug = route.data['slug'];
let type = route.data['type'];
if(slug !== ""){
this._shared.menuList.subscribe(menus =>{
let result = this.getMenuOgj(menus,slug,type);
console.log("result: "+ result); // Get undefine varibale
});
}
return true;
}
// This is recursive function and my first if condition is true then my second if is return true or false
getMenuOgj(menus, menuSlug, type):boolean { // return only true or false
menus.forEach((menu) =>{
if(menu.menu_slug === menuSlug){
if(type==="view"){
this.menuType = menu.view;
}else if(type==="edit"){
this.menuType = menu.edit;
}
if (this.menuType===1 || this.menuType==='1' || this.menuType===true) {
return true;
}else{
return false;
}
}
this.getMenuOgj(menu.submenu, menuSlug, type);
});
}
}
答案 0 :(得分:0)
您
getMenuOgj(menus, menuSlug, type) {
不会返回任何内容。
return true;
仅从传递给menues.forEach(...)
您需要在getMenuOgj()