当我使用 async
管道时,我试图避免创建多个请求。
我有以下请求从 api 获取用户
getUser() {
this._user = this.http.get<User>(environment.baseAPIUrl + 'user')
.pipe(
tap((user: User) => {
// login successful if there's a jwt token in the response
console.log(user);
this.setIsVerified(user.verified);
this.userReady = true;
}),
catchError((error: HttpErrorResponse) => {
console.log(error.statusText);
if (error.status === 403) {
this.logout();
}
return throwError(error.error);
}
))
.publishLast()
.refCount();;
}
但是当我尝试运行该应用程序时出现以下错误:
ERROR TypeError: this.http.get(...).pipe(...).publishLast is not a function
我试着添加
从 'rxjs/operators' 导入 {catchError, tap, publishLast};
但问题仍然存在。
我用
Angular 6 和
"rxjs": "^6.1.0"
最佳答案
从 RxJS v6.x 开始,您需要将 publishLast()
作为 pipe
的参数(argument),如下所示:
getUser() {
this._user = this.http.get<User>(environment.baseAPIUrl + 'user')
.pipe(
tap((user: User) => {
// login successful if there's a jwt token in the response
console.log(user);
this.setIsVerified(user.verified);
this.userReady = true;
}),
catchError((error: HttpErrorResponse) => {
console.log(error.statusText);
if (error.status === 403) {
this.logout();
}
return throwError(error.error);
}
),
publishLast()
)
.refCount();;
}
您不应该像在 RxJS v5.0 中那样链接 publishLast()
。
关于javascript - Angular - .publishLast 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50924128/