Angular 通用 RxJ "Observable_1.Observable.throw is not a function"

Angular Universal RxJs "Observable_1.Observable.throw is not a function"

我在通用服务器控制台日志中收到以下错误(仅使用 SSR 而非 ng serve

ERROR TypeError: Observable_1.Observable.throw is not a function

我的服务是这样设置的

import { Injectable, Injector } from '@angular/core';
import { HttpClient, HttpParams, HttpErrorResponse } from '@angular/common/http';
import { Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Subject, from } from 'rxjs';
import 'rxjs/add/observable/throw';


@Injectable()
export class BcProductService {

    constructor(
        private http: HttpClient,
        private configsService: ConfigsService,
        private injector: Injector
    ) {}

    getProductById(product_id) {
        const data = { product_id: product_id };
        return this.http.get<any>('/getProductById', { params: data }).catch(err => this.errHandler(err));
    }

    errHandler(error: HttpErrorResponse) {
        console.error(error);
        return Observable.throw(error.error || "unknown error");
    }

}

*** 我正在另一个组件中订阅 getProductById()

在整个互联网上,我看到人们忘记了 import 'rxjs/add/observable/throw';,但我有,而且错误只发生在服务器端渲染期间。我输入错误的 Observable 了吗?

版本

rxjs 6.3.2 Angular CLI: 6.2.4 节点:9.2.0 OS: darwin x64 Angular: 6.1.7 rxjs-compat: ^6.3.3

从 RxJS 6 开始,您需要使用 throwError 而不是 throw。

import { throwError } from 'rxjs';

errHandler(error: HttpErrorResponse) {
    console.error(error);
    return Observable.throwError(error.error || "unknown error");
}

Package.json:

"rxjs": "6.3.2",
"rxjs-compat": "6.2.2",

注意:未使用胡萝卜(^)符号。删除 package.lock.json 文件并执行 'npm install'.