0
我在angular2项目中工作。但是,我们的身份验证代码中存在一个问题。如何在angular2中使用callback或observable?
我觉得脚本不等待可调用函数的结果。然后结果变量控制台在“undefined”中。
所以,请帮助我。
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);
});
}
}
回调返回true,不working..please解释更 –
请解释越多,你尝试过什么,或者你尝试完成。我不知道你期望得到什么回报。 –
“getMenuOgj”是递归函数,如果条件为真,那么我的第二个if返回true或false –