observable.throw不是函数

时间:2019-03-05 10:31:15

标签: angular typescript

我试图在服务中实现捕获处理,但是控制台中出现未定义的函数错误。

这是错误消息:

  

TypeError:   rxjs_internal_Observable__WEBPACK_IMPORTED_MODULE_2 __。Observable.throw   不是功能

import { Injectable } from '@angular/core';
import { config } from '../../../config/config';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { HttpResponseHandler } from './http-response-handler.service';
import { Observable } from 'rxjs/internal/Observable';

@Injectable()
export class DataService<T> {

constructor(
        protected url: string,
        protected httpClient: HttpClient,
        protected responseHandler: HttpResponseHandler
    ) { }

    getOneById<T>(id): Observable<T> {
        return this.httpClient
            .get<T>(config.ecmBackUrl + this.url + '/' + id)
            .pipe(catchError((err, source) => this.responseHandler.onCatch(err, source)));
    }
}

@Injectable()
export class HttpResponseHandler {

    public onCatch(response: any, source: Observable<any>): Observable<any> {
       console.log(response);
    }
}

1 个答案:

答案 0 :(得分:1)

更改Observable

的导入路径

来自

import { Observable } from 'rxjs/internal/Observable';

import { Observable } from 'rxjs/Observable';