如何解决Angular Firebase switchmap 和 authstate 代码已弃用
我正在学习 Angular,并且正在处理 youtube 上的一些学习视频。下面的代码显示了如何使用 authState
。遗憾的是,以下代码已弃用。
@Injectable()
export class AuthService {
currentUser: User;
constructor(private afAuth: AngularFireAuth,private db: AngularFireDatabase) {
this.afAuth.authState.switchMap(auth => {
if (auth) {
this.currentUser = new User(auth)
return this.db.object(`/users/${auth.uid}`)
} else return [];
})
.subscribe(user => {
this.currentUser['username'] = user.username
})
}
}
我已经更改了一些推荐的部分,这就是我现在得到的:
import { Injectable } from "@angular/core";
import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFirestore } from "@angular/fire/firestore";
import {map,mergeMap,switchMap} from "rxjs/operators";
export class User {
uid: string;
username: string = "";
constructor(auth) {
this.uid = auth.uid
}
}
@Injectable()
export class AuthService {
currentUser: User;
constructor(private afAuth: AngularFireAuth,private db: AngularFirestore) {
this.afAuth.authState.switchMap(auth => {
if (auth) {
this.currentUser = new User(auth)
return this.db.collection(`/users/${auth.uid}`)
} else return [];
})
.subscribe(user => {
this.currentUser['username'] = user.username
})
}
}
但是我仍然无法使用 switchMap。我从谷歌搜索中尝试了几件事,但我在过时的实现上陷入困境或跌跌撞撞。希望有人能帮忙
我想实现自定义用户名验证。如果您想查看资源,代码取自此视频:https://www.youtube.com/watch?v=NLWHEiH1FZY 可惜是 2017 年的。
编辑:
我已阅读使用 pipe()
。我想出了这个代码:
@Injectable()
export class AuthService {
currentUser: User;
constructor(private afAuth: AngularFireAuth,private db: AngularFirestore) {
this user = this.afAuth.authState.pipe(switchMap(auth => {
if (auth) {
this.currentUser = new User(auth)
return this.db.collection(`/users/${auth.uid}`)
} else return [];
}))
.subscribe(user => {
this.currentUser['username'] = user.username
})
}
}
在 .switchmap(auth
(parameter) auth: firebase.User
Argument of type '(auth: User) => any[] | AngularFirestoreCollection<unkNown>' is not assignable to parameter of type '(value: User,index: number) => ObservableInput<any>'.
Type 'any[] | AngularFirestoreCollection<unkNown>' is not assignable to type 'ObservableInput<any>'.
Type 'AngularFirestoreCollection<unkNown>' is not assignable to type 'ObservableInput<any>'.
Property '[Symbol.iterator]' is missing in type 'AngularFirestoreCollection<unkNown>' but required in type 'Iterable<any>'.ts(2345)
lib.es2015.iterable.d.ts(51,5): '[Symbol.iterator]' is declared here.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。