是否有机会从 RxJs 'subscribe' 中省略 'next' 部分
Is there a chance to omit 'next' part from RxJs 'subscribe'
我想知道我是否可以将以下代码转换为没有来自 RxJS 'subscribe' 的 'next' 部分的代码。我对从服务中获得任何结果不感兴趣。唯一重要的是捕捉潜在的错误以做出正确的反应。
this.someService.getSomething(this.id)
.pipe(
takeUntil(this.destroy$)
)
.subscribe(() => {
// I don't need this part at all
}, (error: HttpErrorResponse) => {
if (error.status === 404) {
// doing sth important
}
});
有机会在 Angular 中用它做点什么吗?
您可以尝试使用 catch / catchError:
this.someService.getSomething(this.id)
.pipe(
takeUntil(this.destroy$),
catchError(
(error: HttpErrorResponse) => {
if (error.status === 404) {
// doing sth important
}
})
);
正如上面的回答,您可以使用 catchError
运算符。但是,如果你无论如何都需要订阅,你也可以使用这样的订阅方法 this.someService.getSomething(this.id).subscribe(null, (error) => {..})
你可以通过一个PartialObserver来订阅和定义或省略next
、error
和complete
:
source.subscribe({ error: (e: any) => console.error(e) })
我想知道我是否可以将以下代码转换为没有来自 RxJS 'subscribe' 的 'next' 部分的代码。我对从服务中获得任何结果不感兴趣。唯一重要的是捕捉潜在的错误以做出正确的反应。
this.someService.getSomething(this.id)
.pipe(
takeUntil(this.destroy$)
)
.subscribe(() => {
// I don't need this part at all
}, (error: HttpErrorResponse) => {
if (error.status === 404) {
// doing sth important
}
});
有机会在 Angular 中用它做点什么吗?
您可以尝试使用 catch / catchError:
this.someService.getSomething(this.id)
.pipe(
takeUntil(this.destroy$),
catchError(
(error: HttpErrorResponse) => {
if (error.status === 404) {
// doing sth important
}
})
);
正如上面的回答,您可以使用 catchError
运算符。但是,如果你无论如何都需要订阅,你也可以使用这样的订阅方法 this.someService.getSomething(this.id).subscribe(null, (error) => {..})
你可以通过一个PartialObserver来订阅和定义或省略next
、error
和complete
:
source.subscribe({ error: (e: any) => console.error(e) })