【问题标题】:Property 'subscribe' does not exist on type 'OperatorFunction<unknown, any>''OperatorFunction<unknown, any>' 类型上不存在属性 'subscribe'
【发布时间】:2019-06-29 21:11:36
【问题描述】:

我正在尝试从 mongodb 获取一些数据,并不断收到此错误:

“OperatorFunction”类型上不存在属性“订阅”

//app.component.ts

import { Component } from '@angular/core';
import {FormGroup, FormControl, Validators, FormsModule} from '@angular/forms';
import {CommonService} from './common.service';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { map } from 'rxjs/operators';
import { from } from 'rxjs';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {

    constructor(private newService: CommonService) {
    }

    Repdata;
    valbutton = "Save";

    ngOnInit() {
        this.newService.GetProcesses().subscribe(data => this.Repdata = data);
    }
}

//common.service.ts


import { Injectable } from '@angular/core';
import {Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { map, tap } from 'rxjs/operators';
import 'rxjs/add/operator/do';
import 'rxjs/Rx';
import { catchError } from 'rxjs/operators';

@Injectable()
export class CommonService {

    constructor(private http: Http) { }

    GetProcesses(){
        return this.http.get('http://localhost:4000/getProcesses/')
                   .pipe(map((response: Response) => response.json())),
            catchError(error => Observable.of(null))

    }
}

“OperatorFunction”类型上不存在属性“订阅”

【问题讨论】:

  • 你使用的是哪个版本的 Angular 和 RXJS?
  • 你缺少这个')'来关闭你的管道
  • @GerardoJaramillo 提出一个重要问题。HttpModuleHttp 自 Angular 2.x 以来就没有使用过,相反,您可以在 Angular 4.x+ 中使用 HttpClientModuleHttpClient。使用 RxJS 5+,你真的不再需要像 import 'rxjs/add/operator/do'; 这样的静态导入,你可以像 import { map, tap } from 'rxjs/operators'; 这样命名导入。
  • 您好,感谢您的回复,我目前正在使用 angular 2.4.1 和 rxjs 6.5.2,是否应该升级到更新版本的 angular?
  • 取决于您的应用程序的复杂程度以及您是否有时间检查所有内容,如果您问我应该。但如果您有任何疑问,请访问此页面update.angular.io/#2.1:8.0

标签: angular


【解决方案1】:

您的 common.service.ts 文件中的括号似乎放错了位置。

我制作了这段代码 sn-p 供你试用,希望它能满足你的需要

//common.service.ts

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {catchError, map} from 'rxjs/operators';
import 'rxjs/add/operator/do';
import 'rxjs/Rx';

@Injectable()
export class CommonService {

    constructor(private http: Http) {
    }

    GetProcesses() {
        let url = 'http://localhost:4000/getProcesses/';
        return this.http.get(url).pipe(
            map((response: Response) => response.json()),
            catchError(error => Observable.of(null))
        );
    }
}

【讨论】:

  • GetProcesses() 中有subscribe() 将不起作用。从subscribe() 中返回一些东西不会返回任何东西。它不像 .then(response =&gt; response.json()) 的承诺,返回或隐式返回将其传递给其他 .then() 语句。这就是为什么需要pipe() 来处理数据并将处理后的数据转发给使用服务并执行subscribe() 的组件/指令/等,在本例中为AppComponent
  • 哦,我的错,通常我只是订阅服务中的 api 并将服务方法返回的输出分配给组件范围内的变量。我在组件中错过了他的.subscribe()。我猜他唯一的问题是括号。
  • 不好。如果您尝试订阅 GetProcess(),则需要将其设为 asObservable(),这就是为什么 pipe() 会帮助您
  • 感谢@Talg123,但亚历山大已经澄清了这一点。我更新了我的答案以避免进一步的混淆。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 2020-11-01
  • 2019-12-26
  • 2019-06-06
  • 1970-01-01
  • 2022-10-16
相关资源
最近更新 更多