【发布时间】:2019-04-06 08:03:30
【问题描述】:
我正在将我的代码转换为使用 RxJS6 语法来使用管道和映射,但我遇到了错误。
error TS2345: Argument of type '(error: any) => void' is not assignable to parameter of type '(err:
any, caught: Observable<void>) => ObservableInput<{}>'.
Type 'void' is not assignable to type 'ObservableInput<{}>'.
现有代码工作正常,但我遇到的问题是在返回结果之前,会调用其他方法。所以据我所知,使用管道和地图 将解决此问题。 这是我最初的代码:
this._reportingService.GetProjectReportsData(data).subscribe(result => {
if (result != null) {
this.reportData = result;
}
}, error => {
this.ErrorMessage('Unable to load workbook ' + error.toString());
this._reportingService.isLoading = false;
});
这是我试图转换为使用管道和地图的代码:
我已经连同其他进口商品一起进口了
import { Observable, of, throwError } from 'rxjs';
import { map, catchError, retry } from 'rxjs/operators';
并在方法中(修改;去掉this.error)请指导如何为errorMessage添加代码:
this._reportingService.GetProjectReportsData(data).pipe(
map(result => {
if (result != null) {
this.reportData = result;
}
}))
.subscribe();
还有我的服务等级:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { map, tap, catchError } from 'rxjs/operators';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable({
providedIn: 'root'
})
export class ReportingService extends BehaviorSubject<any[]>{
constructor(private http: HttpClient) {
super(null);
}
public GetProjectReportsData(data: any): Observable<any> {
return this.http.post(this.GetProjectReportDataUrl, data)
.pipe(map(res => <any[]>res))
.pipe(catchError(this.handleError));
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
return Observable.throw(errMsg);
}
}
【问题讨论】:
标签: angular observable rxjs6