【问题标题】:Angular 6 RxJS6 Type 'void' is not assignable to type 'ObservableInput<{}>'Angular 6 RxJS6 类型“void”不可分配给类型“ObservableInput<{}>”
【发布时间】: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


    【解决方案1】:

    我认为问题是由于您缺少 map 运算符中的 return 语句。

    import { of } from 'rxjs';
    
    this._reportingService.GetProjectReportsData(data).pipe(
        map(result => {
            if (result != null) {
               return this.reportData = result;
            }
            return of(null);
        }))
        .subscribe();
    

    也不确定您为什么要使用此验证,(结果!= null)。小心点。

    【讨论】:

      【解决方案2】:

      根据我的说法,双管道链接会产生问题。

      像这样试试。

       public GetProjectReportsData(data: any): Observable<any> {
              return this.http.post(this.GetProjectReportDataUrl, data)
                  .pipe(
                     map(res => <any[]>res),
                     catchError(this.handleError)
                   );
          }
      

      【讨论】:

        【解决方案3】:

        从catchError返回 observable:

        import { of } from 'rxjs';
        
        ...
        catchError(error => of(this.ErrorMessage('Unable to load workbook ' + error.toString()))
        ...
        

        【讨论】:

        • 你能推荐整个街区吗?没有得到正确的语法...得到曲线的数量。
        • 修改了代码并删除了 this.ErrorMessage。代码编译得很好。但是仍然需要知道如何为this.ErrorMessage添加代码
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-06
        • 2020-10-01
        • 2021-12-10
        • 1970-01-01
        • 2019-01-04
        • 1970-01-01
        • 2021-12-05
        相关资源
        最近更新 更多