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 –