在我的Angular-Firebase在线购物项目中,我使用calculateDeliveryTime
检查用户是否已登录以访问AuthGuard
和其他链接,如下面的代码所示。在下面的代码中,未为./check-out
导入map
导致错误。使用最新版本的所有内容。
auth.service.ts
Observable.User
login.component.ts
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import * as firebase from 'firebase';
@Injectable({
providedIn: 'root'
})
export class AuthService {
user$: Observable<firebase.User>;
constructor(private afAuth: AngularFireAuth) {
this.user$=afAuth.authState;
}
login(){
this.afAuth.auth.signInWithRedirect(new firebase.auth.GoogleAuthProvider())
}
logout(){
this.afAuth.auth.signOut()
}
}
bs-navbar.component.ts
import { AuthService } from './../auth.service'
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent{
constructor(private auth: AuthService) { }
ngOnInit() {
}
login(){
this.auth.login();
}
}
bs-navbar.component.html
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';
@Component({
selector: 'bs-navbar',
templateUrl: './bs-navbar.component.html',
styleUrls: ['./bs-navbar.component.css']
})
export class BsNavbarComponent{
constructor(public auth: AuthService) {
}
logout(){
this.auth.logout();
}
}
auth.guard.service.ts
<nav class="navbar navbar-expand-md navbar-light bg-light fixed-top">
<a class="navbar-brand" routerLink="/">O</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" routerLink="/shopping-cart">Shopping cart</a>
</li>
<ng-template #anonymousUser>
<li class="nav-item">
<a class="nav-link" routerLink="/login">Login</a>
</li>
</ng-template>
<li ngbDropdown *ngIf="auth.user$ | async as user; else anonymousUser" class="nav-item dropdown">
<a ngbDropdownToggle class="nav-link dropdown-toggle" id="dropdown01" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{user.displayName}}
</a>
<div ngbDropdownMenu class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" routerLink="/my/orders">My Orders</a>
<a class="dropdown-item" routerLink="/admin/orders">Manage Orders</a>
<a class="dropdown-item" routerLink="/admin/products">Manage Prodcuts</a>
<a class="dropdown-item" (click)="logout()">Log Out</a>
</div>
</li>
</ul>
</div>
</nav>
在import { Injectable } from '@angular/core';
import { AuthService } from './auth.service';
import { Router } from '@angular/router';
import { CanActivate } from '@angular/router';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate{
constructor(private auth:AuthService, private router: Router) {
}
canActivate(): boolean{
return this.auth.user$.map(user=>{
if(user) return true;
this.router.navigate(['./login']);
return false;
});
}
}
行的 auth.guard.service.ts 中出错。
答案 0 :(得分:3)
如果您使用的是最新版本的RxJS,则可能是因为map
运算符必须按如下方式进行管道传输:
return this.auth.user$.pipe(map(user => ...));
您可以在此处查看示例:https://www.learnrxjs.io/operators/transformation/map.html