尝试在警卫队上定义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;
}
})
}
}
答案 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;
}))
}
}