RXJS 自定义 catchererror 运算符
RXJS Custom catcherror operator
通常我可以定义一个自定义运算符并像这样构造一个管道:
public custOperator = () => {
return (source: Observable<any>) => {
return source.pipe(
map(val => {
//do stuff with val
})
)
}
};
test(){
of().pipe(
this.custOperator(),
catchError(err => {
//do stuff with err (more logic will go in here)
throw err
})
).subscribe()
}
然而,每次我需要通过管道传输 observable 时编写 catchererror 逻辑变得乏味。
我将如何着手创建一个类似的运算符,除了它期望一个错误而不是一个可观察的,以便它模仿 catchError?
更新
我能够像这样创建一个自定义的 catchererror 运算符:
private customCatchOperator = () => {
return catchError(err => {
// handle error
})
}
您可以按照以下方式进行:
customCatchOperator() {
return catchError(err => {
// handle error
})
}
通常我可以定义一个自定义运算符并像这样构造一个管道:
public custOperator = () => {
return (source: Observable<any>) => {
return source.pipe(
map(val => {
//do stuff with val
})
)
}
};
test(){
of().pipe(
this.custOperator(),
catchError(err => {
//do stuff with err (more logic will go in here)
throw err
})
).subscribe()
}
然而,每次我需要通过管道传输 observable 时编写 catchererror 逻辑变得乏味。
我将如何着手创建一个类似的运算符,除了它期望一个错误而不是一个可观察的,以便它模仿 catchError?
更新
我能够像这样创建一个自定义的 catchererror 运算符:
private customCatchOperator = () => {
return catchError(err => {
// handle error
})
}
您可以按照以下方式进行:
customCatchOperator() {
return catchError(err => {
// handle error
})
}