【问题标题】:Cannot read property 'of' of undefined for Observable after upgrading from Angular 2 to Angular 4从 Angular 2 升级到 Angular 4 后,无法读取 Observable 的未定义属性“of”
【发布时间】:2018-02-15 07:43:59
【问题描述】:

在我将 Angular 2 和 RxJs 升级到最新版本之前,以下服务及其 Observable 运行良好。

在尝试执行以下代码行时,错误提示“无法读取未定义的属性'of'

   if (this.loggedContact) {
            return Observable.of(this.loggedContact);
        }

我看到 this.loggedContact 已经有价值。看起来它认为 Observable 是未定义的。有什么东西改变了我们过去将对象作为 Observable 返回的方式吗?

 "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "angular2-busy": "2.0.4",
    "bootstrap": "3.3.7",
    "core-js": "2.5.1",
    "jquery": "3.2.1",
    "ng2-completer": "^1.3.1",
    "ng2-toasty": "^4.0.3",
    "reflect-metadata": "0.1.10",
    "rxjs": "^5.4.3",
    "systemjs": "^0.20.18",
    "zone.js": "^0.8.17"

以下是服务:

import 'rxjs/add/operator/toPromise';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';


loggedContact: CostarContactVM;
observable: Observable<any>;

getLoggedContact(): Observable<Contact> {
        if (this.loggedContact) {
            return Observable.of(this.loggedContact);
        }
        else if (this.observable)
            return this.observable;
        else {
            this.observable = this.http.get(this.contactUrl)
                .map(res => {
                    this.observable = null;
                    if (res.status == 400) {
                        return "FAILURE";
                    }
                    else if (res.status === 200) {
                        this.loggedContact = res.json() as Contact;
                        return this.loggedContact;
                    }
                })
                .share();
            return this.observable; 
        }
    }

以下是相同的 System.Config:

(function (global) {
    System.config({
        meta: {
            '*': { authorization: true }
        },
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        map: {
            app: 'app',
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
            '@angular/compiler': npm:@angular/compiler/bundles/compiler.umd.js',
            '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
            '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
            '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
            '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
            'rxjs': 'npm:rxjs'
            },
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

【问题讨论】:

  • 我可能听起来很愚蠢,但你可以试试这个import 'rxjs/Rx'; 删除所有其他导入
  • 很抱歉,这没有帮助
  • 我建议提供一种方法来复制问题 - 一个 plunk、一个 repo 等,因为发布的代码没有反映它。

标签: angular angular2-services rxjs5


【解决方案1】:

我通过升级到 Angular 4.0 找到了问题的解决方案。基本上,我将我的 RxJs 从 5.0.1 升级到 5.4.3,它现在支持来自位置 @angular/common/http 的 HttpClient 模块,而不是来自 @angular/http 的 Http。

我将所有引用升级到 HttpClient 并更新了我的代码,如下所示,它解决了这个问题。我希望它可以帮助某人。 关键是在升级到 Angular 4 的过程中,记得将你的 http 升级到 httpClient。

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http';

@Injectable()
export class ContactService {


loggedContact: CostarContactVM;

  getLoggedContact(): Observable<ContactVM> {
     return this.httpClient.get<ContactVM>(this.contactUrl)
        .catch(this.handleError)
        .do((data) => {
            this.assignPermissions(data);
        });
}

【讨论】:

    猜你喜欢
    • 2021-01-06
    • 2020-12-04
    • 1970-01-01
    • 2022-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    相关资源
    最近更新 更多