将旧的httpGet转换为管道运算符

时间:2019-02-04 20:59:14

标签: rxjs angular6 angular7 ionic4

我有一个执行CRUD操作的abstract.service。现在我正在从离子3迁移到离子4,但似乎rxjs已更新,现在不再允许我这样做:

httpGet(url, requestObject = new RequestObject, onSuccess = (resp, listData) => {}, onFail = (err) => {}) {
    let data;
    let headers;

    return this.http.get(url, {headers: headers, params: data})
        .map(response => response).catch(this.handleError).subscribe(
            (resp) => {
                if (!resp) resp = {items: []};
                if (requestObject.loader) this.globalService.onHideLoader.emit(requestObject.loaderId)

                onSuccess(resp, {});
                requestObject.onDone(resp);
            },
            (err) => {
                if(requestObject.loader) this.globalService.onHideLoader.emit(requestObject.loaderId);

                // Execute own errorHandling when response has no errorHandling
                // if(!requestObject.hasErrorHandling) this.onError(err);

                onFail(err);
                requestObject.onDone(err);
            }
        );
}

那么我该如何尝试用Pipable operators将上面这段代码转换为一个示例,这是我尝试过的:

httpGet(url, requestObject = new RequestObject, onSuccess = (resp, listData) => {}, onFail = (err) => {}) {
    let data;
    let headers;

    return this.http.get(url, {headers: headers, params: data}).pipe(

        map(response => {
            if (!response) response = {items: []};
            if (requestObject.loader) this.globalService.onHideLoader.emit(requestObject.loaderId)

            onSuccess(response, {});
            requestObject.onDone(response);
        }),
        catchError(error => {
            this.handleError(error);
            if(requestObject.loader) this.globalService.onHideLoader.emit(requestObject.loaderId);

            // Execute own errorHandling when response has no errorHandling
            if(!requestObject.hasErrorHandling) this.onError(err);

            onFail(error);
            requestObject.onDone(error);
        })
    );
}

但是在catchError方法中,它引发了这个错误:

Argument of type '(error: any) => void' is not assignable to parameter of type '(err: any, caught: Observable<void>) => ObservableInput<{}>'.

有人可以帮我把旧方法转换成新方法吗?

0 个答案:

没有答案