【发布时间】:2018-11-14 16:03:33
【问题描述】:
当我发送请求并且响应为 401 状态时,我想显示模式框(带有用户和密码)以重新登录用户,然后重新发送原始请求。
我发现以前版本的 RxJs 的帖子非常有趣,我无法将其更新到新版本 How to catch an error on a Request, then open a modal, then retry when modal closes with RxJS
当我有 401 时,我的代码会显示模式,但同时它会重新发送我的请求并且它不会等待关闭操作。
你能帮帮我吗?
return this.http.get<Object>('http://localhost/test', httpOptions).
pipe(
tap(),
retryWhen(errors =>
errors.pipe(
tap(val => {
if (val.status === 401) {
let closedSubject = new Subject();
let modalRef = this.modalService.open(ModalLoginComponent);
modalRef.result.then((data) => {
console.log('closed me');
closedSubject.next(val);
}, (reason) => {
});
return <any>closedSubject;
}
else {
return Observable.throw(val.json());
}
})
)
)
);
【问题讨论】:
-
您想使用
catchError运算符而不是retryWhen -
使用 catchError 我无法在模式关闭操作后重试请求。