没有订阅时的默认操作?

问题描述 投票:1回答:1

我们有一个通用的HTTP服务来包装一些HTTP行为。当出现错误时,我们将错误添加到BehaviorSubject。

我想知道是否有办法以某种方式...只有在没有订阅主题时才显示此错误。

return this.$http.get(url, jsonData).map((res) => res.json().response_data)
  .catch((err) => {
    this._errors.next(err);
});

基本上尝试实现HTTP错误的“最终”处理,如果没有其他处理它。

angular
1个回答
1
投票

您可以将带有handled标志的对象传递给订阅者,这应该在处理错误后将标志设置为true。在通知订阅者后未设置标志,显示默认错误消息。

export class ErrorHandling {
    public handled: boolean = false;
    constructor(public error: any) {
    }
}

return this.$http.get(url, jsonData).map((res) => res.json().response_data)
  .catch((err) => {
    let errorHandling = new ErrorHandling(err);
    this._errors.next(errorHandling);
    if (!errorHandling.handled) {
        // The error was not handled, show default message
        ...
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.