Angular8 AngularFireAuth:Guard canActivate()的authState.map错误:类型“ Observable <User>”上不存在属性“ map”

时间:2019-11-14 10:15:58

标签: firebase angular8 angular-router-guards

尝试在警卫队上定义canActive()时,Angular8 AngularFireAuth出现错误

我得到一个错误:

  

“可观察”类型上不存在属性“地图”。ts(2339)

import { Injectable } from "@angular/core";
import { CanActivate, Router } from '@angular/router';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private router: Router, private afAuth: AngularFireAuth) { }

  canActivate(): Observable<boolean> {
    this.afAuth.authState.map(auth => {
      if(!auth) {
        this.router.navigate(['/login']);
        return false;
      } else {
        this.router.navigate(['/']);
        return true;
      }
    })
  }
}

1 个答案:

答案 0 :(得分:0)

import { Injectable } from "@angular/core";
import { CanActivate, Router } from '@angular/router';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private router: Router, private afAuth: AngularFireAuth) { }

  canActivate(): Observable<boolean> {
    return this.afAuth.authState.pipe(map((user)=>{
      if(user) {
        return true;
      }
      this.router.navigate(['/login'])
      return false;
    }))
  }
}