Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular auth-guard中可观测的承诺的混合_Angular_Typescript_Ionic Framework - Fatal编程技术网

Angular auth-guard中可观测的承诺的混合

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 {

如果用户登录或未登录,我试图在可观察对象内的承诺(gameid)内返回真或假:

我的卫士:

//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);