Observable 因错误而关闭
Observable closed on error
我在 Angular 2.
中遇到 Observable 的问题
我将我的组件订阅到一个可观察对象,然后当我的服务有新值时,我的组件会收到通知。
问题是当观察者推送错误时,比如 HTTP 错误,我的 observable 被关闭,所以我的组件不再被通知。
问题
我怎样才能让我的组件在出现错误时继续监听我的服务?
例子
这里有一个 example
这是我的代码:
组件
constructor(private appService: AppService) {
//I subscribe my component to an observable
this.appService.commentsObservable.subscribe((comments) => {
console.log(comments);
}, (err) => {
console.log(err);
});
}
getComments() {
//I ask the service to pull some comments
this.appService.getComments()
}
服务
private commentsObserver: Observer<any>;
commentsObservable: Observable<any>;
constructor() {
this.commentsObservable = new Observable((observer) => {
this.commentsObserver = observer;
});
}
getComments() {
setTimeout(() => {
//You will see the result displayed by the component
this.commentsObserver.next([]);
}, 0);
setTimeout(() => {
//You will see the result displayed by the component
this.commentsObserver.next([]);
}, 500);
setTimeout(() => {
//You will see the error displayed by the component
this.commentsObserver.error({_body: 'Nice errroorr'});
}, 1000);
setTimeout(() => {
//You won't see this one, why ?
this.commentsObserver.next([]);
}, 1500);
}
这是预期的行为。 According to the documentation,
In an Observable Execution, zero to infinite Next notifications may be delivered. If either an Error or Complete notification is delivered, then nothing else can be delivered afterwards.
上面的代码,可能是
this.appService
// error is caught, but the observable is completed anyway
.catch((err) => {
console.error(err)
return Observable.empty();
})
// re-subscribe to completed observable
.repeat()
.subscribe((comments) => console.log(comments));
但是考虑到预期的行为,使用 RxJS 错误处理来提供具有非关键错误值的连续可观察值是不切实际的。相反,它可能会更改为
setTimeout(() => {
//You will see the error displayed by the component
this.commentsObserver.next(new Error('Nice errroorr'));
}, 1000);
和
this.appService.commentsObservable.subscribe((comments) => {
if (comments instanceof Error)
console.error(comments);
else
console.log(comments);
});
具体做法可能会因实际情况而异。
我在 Angular 2.
中遇到 Observable 的问题我将我的组件订阅到一个可观察对象,然后当我的服务有新值时,我的组件会收到通知。
问题是当观察者推送错误时,比如 HTTP 错误,我的 observable 被关闭,所以我的组件不再被通知。
问题
我怎样才能让我的组件在出现错误时继续监听我的服务?
例子
这里有一个 example
这是我的代码:
组件
constructor(private appService: AppService) {
//I subscribe my component to an observable
this.appService.commentsObservable.subscribe((comments) => {
console.log(comments);
}, (err) => {
console.log(err);
});
}
getComments() {
//I ask the service to pull some comments
this.appService.getComments()
}
服务
private commentsObserver: Observer<any>;
commentsObservable: Observable<any>;
constructor() {
this.commentsObservable = new Observable((observer) => {
this.commentsObserver = observer;
});
}
getComments() {
setTimeout(() => {
//You will see the result displayed by the component
this.commentsObserver.next([]);
}, 0);
setTimeout(() => {
//You will see the result displayed by the component
this.commentsObserver.next([]);
}, 500);
setTimeout(() => {
//You will see the error displayed by the component
this.commentsObserver.error({_body: 'Nice errroorr'});
}, 1000);
setTimeout(() => {
//You won't see this one, why ?
this.commentsObserver.next([]);
}, 1500);
}
这是预期的行为。 According to the documentation,
In an Observable Execution, zero to infinite Next notifications may be delivered. If either an Error or Complete notification is delivered, then nothing else can be delivered afterwards.
上面的代码,可能是
this.appService
// error is caught, but the observable is completed anyway
.catch((err) => {
console.error(err)
return Observable.empty();
})
// re-subscribe to completed observable
.repeat()
.subscribe((comments) => console.log(comments));
但是考虑到预期的行为,使用 RxJS 错误处理来提供具有非关键错误值的连续可观察值是不切实际的。相反,它可能会更改为
setTimeout(() => {
//You will see the error displayed by the component
this.commentsObserver.next(new Error('Nice errroorr'));
}, 1000);
和
this.appService.commentsObservable.subscribe((comments) => {
if (comments instanceof Error)
console.error(comments);
else
console.log(comments);
});
具体做法可能会因实际情况而异。