Angular auth-guard中可观测的承诺的混合
如果用户登录或未登录,我试图在可观察对象内的承诺(gameid)内返回真或假: 我的卫士:Angular auth-guard中可观测的承诺的混合,angular,typescript,ionic-framework,Angular,Typescript,Ionic Framework,如果用户登录或未登录,我试图在可观察对象内的承诺(gameid)内返回真或假: 我的卫士: //Angular import { Injectable } from '@angular/core'; import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; //Rxjs import { Observable } from 'rxjs'; import {
//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;
}
}))
}
}
//角度
从“@angular/core”导入{Injectable};
从'@angular/Router'导入{CanActivate,Router,ActivatedRouteSnapshot,RouterStateSnashot};
//Rxjs
从“rxjs”导入{Observable};
从“rxjs/operators”导入{map,take,mergeMap};
//服务
从“../services/login.service”导入{LoginService};
@可注射()
导出类AuthGuard实现了CanActivate{
建造师(
公共路由器:路由器,
公共登录服务:登录服务
){}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值{
返回此.loginService.getAuthenticated().pipe(映射(用户=>{
if(user&&user.hasOwnProperty('uid')&&user.uid!=“”){
this.loginService.getGameIdUser()。然后(游戏=>{
if(game.hasOwnProperty[“gameid”]&&game.gameid!=“”){
return true;//试试这样的方法。你会把你的承诺变成可观察的
@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(concatMap((user) => {
if (user && user.hasOwnProperty("uid") && user.uid != "") {
return from(this.loginService.getGameIdUser()).pipe( // <-- Turn your promise to an observable
map((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 of(false);
}
}));
}
}
@Injectable()
导出类AuthGuard实现了CanActivate{
建造师(
公共路由器:路由器,
公共登录服务:登录服务
) { }
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):可观察的|承诺|布尔值{
返回此.loginService.getAuthenticated().pipe(concatMap((用户)=>{
if(user&&user.hasOwnProperty(“uid”)&&user.uid!=“”){
从(this.loginService.getGameIdUser()).pipe(/)返回{
if(game.hasOwnProperty.gameid&&game.gameid!=“”){
return true;//这是因为当您运行promise时,代码将继续并完成您的canActivate()谢谢你的回答,但是现在它说Observable类型不能与Observable匹配。我更新了我的答案。因为我们使用concatMap,我必须在else部分返回一个Observable,而不是false。嗯,对不起,还是一样的…我试着自己修复它,但我想我还是不知道理解它:(啊,好吧,缺失的部分是我正在使用rxjs6,我必须将其更改为returnobserveof(false);