在我的代码中,我有一个拦截器,该拦截器处理所有出站http请求并将令牌添加到其中。 代码看起来像这样
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const promise = this.storage.get('token');
return Observable.fromPromise(promise)
.mergeMap(token => {
const clonedReq = this.addToken(request, token);
return next.handle(clonedReq).pipe(
catchError(async err => {
// Perhaps display an error for specific status codes here already?
if (err.status === 400 && err.error.message === 'Failed to authenticate token.') {
this.auth.logout(true);
}
if (err.status === 401 && err.error.error === 'TokenExpiredError') {
this.auth.logout();
// this.unauthorizedWatcher.getInterceptedSource().next(err.status);
}
let errorText = '';
if (err.error) {
errorText = err.error.message;
} else if (err.statusText) {
errorText = err.statusText;
}
const toast = this.toastCtrl.create({
message: errorText ? errorText : err.name,
duration: 3000,
position: 'top',
cssClass: 'error',
});
(await toast).present();
// Pass the error to the caller of the function
return throwError(err);
})
);
});
}
因为我从Promise的rxjs:“ 5.5.11”升级到rxjs:“ 6.5.1”,所以不再可用。我如何将这一行转换为有效的6.5.x jxjs代码
答案 0 :(得分:1)
对于RxJS 6+,应将fromPromise()
运算符替换为from()
运算符。另外,您将必须使用pipe()
来链接您的运算符。
const promise = this.storage.get('token');
return from(promise)
.pipe(
mergeMap(token => {
.....
}),
)
根据documentation,from()
运算符的用途是
将数组,promise或可迭代数组转换为可观察对象。