Javascript 角度-.publishLast不是一个函数
我试图避免在使用Javascript 角度-.publishLast不是一个函数,javascript,angular,rxjs,Javascript,Angular,Rxjs,我试图避免在使用async管道时创建多个请求。 我有以下请求从api获取用户 getUser(){ this.\u user=this.http.get(environment.baseapirl+'user') .烟斗( 点击((用户:用户)=>{ //如果响应中有jwt令牌,则登录成功 console.log(用户); 此.setIsVerified(用户.verified); this.userReady=true; }), catchError((错误:HttpErrorRespons
async
管道时创建多个请求。
我有以下请求从api获取用户
getUser(){
this.\u user=this.http.get(environment.baseapirl+'user')
.烟斗(
点击((用户:用户)=>{
//如果响应中有jwt令牌,则登录成功
console.log(用户);
此.setIsVerified(用户.verified);
this.userReady=true;
}),
catchError((错误:HttpErrorResponse)=>{
console.log(error.statusText);
如果(error.status==403){
此参数为.logout();
}
返回投掷器(error.error);
}
))
.publishLast()
.refCount();;
}
从RxJS v6.x开始,您需要将publishLast()
作为管道的参数(参数),如下所示:
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();;
}
getUser(){
this.\u user=this.http.get(environment.baseapirl+'user')
.烟斗(
点击((用户:用户)=>{
//如果响应中有jwt令牌,则登录成功
console.log(用户);
此.setIsVerified(用户.verified);
this.userReady=true;
}),
catchError((错误:HttpErrorResponse)=>{
console.log(error.statusText);
如果(error.status==403){
此参数为.logout();
}
返回投掷器(error.error);
}
),
最后出版()
)
.refCount();;
}
您不应该像在RxJS v5.0中那样链接publishLast()
。您的意思是什么,对不起?RxJS的哪个版本?“RxJS”:“^6.1.0”操作符进入管道()
,请参见“是”,它可以工作,谢谢。但是我还必须添加refCount()
作为pipe
的参数,这正常吗?是的,refCount()会在订阅者来来去去时自动订阅和取消订阅。如果要避免使用refCount(),可以使用shareReplay(1)而不是publishLast()。请更新您的答复,refCount()代码>应该是.pipe(refCount())代码>