简体   繁体   English

RXJS 自定义catcherror算子

[英]RXJS Custom catcherror operator

Normally I would be able to define a custom operator and structure a pipe like this:通常我可以像这样定义一个自定义运算符并构造一个 pipe:

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()
}

However it's getting tedious writing the catcherror logic every time I need to pipe an observable.但是,每次我需要 pipe 一个 observable 时,编写 catcherror 逻辑都会变得乏味。

How would I go about creating a similar operator except that it expects an error instead of an observable, such that it mimics catchError?我将如何 go 创建一个类似的运算符,除了它期望一个错误而不是一个可观察的,以便它模仿 catchError?

Update更新

I was able to create a custom catcherror operator as such:我能够像这样创建一个自定义的 catcherror 运算符:

private customCatchOperator = () => {
    return catchError(err => {
        // handle error 
    })
}

You can do it as follows:你可以这样做:

customCatchOperator() {
   return catchError(err => {
            // handle error
        })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM