【问题标题】:subscribe does not exist on type observable<any>订阅在 observable<any> 类型上不存在
【发布时间】:2018-09-18 12:23:41
【问题描述】:

我已将我的应用程序从 angular 4 升级到 angular 6。我遇到了错误 subscribe 在 observable 类型上不存在。有人可以告诉我角度 6 是否有任何变化

import { Injectable } from '@angular/core';
import { Dto, ApiResult } from '../api';
import { RunsProxy } from '../api/proxies';
import { Observable, ReplaySubject, Subject } from 'rxjs';
import { AlertService } from './alert.service';
import { TranslateService } from '@ngx-translate/core';
import { ReadonlyProvider } from '@wtw/toolkit/src/directives/read-only-inputs.directive';
import { Router, NavigationStart, NavigationCancel, NavigationEnd } from '@angular/router';
import { CurrencyInfo, RunExecution } from '../api/dtos';
import { tap , map, share, delay } from 'rxjs/operators'
import { fireAndForget } from "platform/tests/helpers";

     *public load(id: number): Observable<ApiResult<Dto.RunModel>> {
            const obs = this._runs.get(id).uiSignal('load run').share();
            obs.subscribe(ret => {
                if (!!!ret.data && this.blnShown === false) {
                    this.blnShown = true;
                    this._translate.get('GLOBAL.TOASTS.RUN_UNAVAILABLE').subscribe(o => {
                        this._alertService.error(o);
                    });
                }
                this._activeRun.next(ret.data);
            }, err => {
                if (err.status === 403 || err.status === 404) {
                    this._router.navigate(['/home']);
                    this._alertService.clear();
                    this._translate.get('GLOBAL.TOASTS.RUN_UNAVAILABLE').subscribe(o => this._alertService.error(o));
                } else throw err;
            });
            return obs;
        }*

【问题讨论】:

  • 在迁移到 Angular 6 之前,您的 rxjs 版本是多少?如果不是 6,那么您可能需要运行以下命令 npm install rxjs@6 rxjs-compat@6 --save 并保存 rxjs-compact
  • 你好 Rajeev,我不想安装 rxjs-compat 并走那条路。而是想更改我的代码以使用新版本。它是第 4 版,现在是第 6 版

标签: angular


【解决方案1】:

这是我的服务示例。您可以根据自己的要求放置相同的代码。

我的服务

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})

export class PollSerivce {

  constructor(private _http: Http) { }

  getPollPostData(url: any) {
    const options = new RequestOptions({ withCredentials: true });
    return this._http.get(url)
      .pipe(
        catchError(this.handleError)
      );
  }

  private handleError(error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}

我的组件文件

getDataSource() {
    this._pollSubscription = this.pollSerivce.getPollPostData(StaticKeywords.baseUrl).subscribe(response => {
      this.pollPostData = response.json().hits;
    },
      err => {
        console.log(err);
      });
  }

rxjs 6 有很多变化,所以如果你想了解更多,可以查看这个网站::

https://auth0.com/blog/whats-new-in-rxjs-6/

https://www.academind.com/learn/javascript/rxjs-6-what-changed/

https://www.learnrxjs.io/concepts/rxjs5-6.html

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 2018-04-05
    • 2018-02-01
    • 2017-10-20
    • 2016-09-01
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多