【发布时间】:2020-01-25 03:52:54
【问题描述】:
我正在尝试在一个可观察对象内的 promise(gameid) 中返回真或假,如果用户登录或未登录,该可观察对象返回我:
这里是我的 authguard:
//Angular
import { Injectable } from '@angular/core';
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
//Rxjs
import { Observable } from 'rxjs';
import { map, take, mergeMap } from 'rxjs/operators';
//Services
import { LoginService } from '../services/login.service';
@Injectable()
export class AuthGuard implements CanActivate{
constructor(
public router: Router,
public loginService: LoginService
){}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.loginService.getAuthenticated().pipe(map(user => {
if(user && user.hasOwnProperty('uid') && user.uid != "" ) {
this.loginService.getGameIdUser().then( game => {
if(game.hasOwnProperty["gameid"] && game.gameid != "") {
return true; // <-- this return is reached but doesnt effect my route
} else {
this.router.navigate(['selgame']);
return false;
}
})
} else {
this.router.navigate(['login']);
return false;
}
}))
}
}
似乎返回 true 不会影响 authguard.. 我的错误在哪里?
【问题讨论】:
-
这是因为当你运行promise时,代码会继续并完成你的canActivate(),promise会稍后运行。
标签: angular typescript ionic-framework