0
如何從承諾中返回值並將其包含在全局變量中?退出角度承諾時的返回值4
我試圖讓這個形成,但無濟於事。全局變量未定義
import { Injectable } from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, CanLoad, Route, Router, RouterStateSnapshot} from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Auth } from '../providers/auth';
@Injectable()
export class AuthGuard implements CanActivate, CanLoad {
_check: boolean;
constructor(private auth: Auth, private router: Router) {
console.log(this.check());
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check();
}
canLoad(Route: Route): Observable<boolean> | boolean {
return this.check();
}
check() {
this.auth.check().then((user) => {
if (!user) {
this._check = null;
}else {
this._check = true;
}
});
console.log(this._check) <---- undefined
return this._check;
}
}
你好。我需要check()函數來返回true或false。謝謝 –
你可以在塊中返回this._check。我會稍後更新我的答案。 – nightElf91
@ user2716095我已更新我的答案。如果它幫助您找到解決方案,請選擇它作爲答案。 – nightElf91