我知道之前曾被问过多次,但是,我无法弄清楚如何使用以下文章更改自己的代码。

Property 'subscribe' does not exist on type 'OperatorFunction<Response, Recipe[]>'

Property 'subscribe' does not exist on type 'OperatorFunction<Product[], ParamMap>'

Property 'subscribe' does not exist on type 'OperatorFunction<unknown, any>'



rest-api.service.ts

specificObjectRequest(ids: string[]): Observable<any> {
  var idString = ids.join();
  const url = `${ApiServerUrl}/media_objects?ID=${idString}`;
  console.log("requesting from url: " + url);
  this.http.get(url, {}, {})
    .then(res => this.extractData = res.data)
    .catch(this.handleError);
  return;
}

tagRequest(topic: string){
    var url = `${ApiServerUrl}/tags?topic=${topic}`;
    url = encodeURI(url);
    console.log("requesting from url: " + url);
    this.http.get(url, {}, {})
      .then(res => this.extractData = res.data)
      .catch(this.handleError);
    return map(this.extractData);
  }


我使用官方文档https://ionicframework.com/docs/native/http和本教程创建了此请求

filter.page.ts

async requestMapTags(topic: string){
    await this.api.tagRequest(topic).subscribe(res => {  //Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>'
      console.log(res);
      this.tagsRequestAnswer = res;
    }, err => {
      console.log(err);
    });
}


错误信息:

[ng] ERROR in src/app/filter/filter.page.ts(89,35): error TS2339: Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>'.


据我了解,这是Promises和rxjs之间的冲突。

题:
我需要进行哪些更改才能使其正常工作?有没有一种方法可以在我的代码中添加所需的“管道”函数以使其正常工作?

最佳答案

在这里使用诺言似乎是不必要的,您可以轻松完成

import { of } from 'rxjs';

tagRequest(topic: string){
    var url = `${ApiServerUrl}/tags?topic=${topic}`;
    url = encodeURI(url);
    return of(this.http.get(url, {}, {})).pipe(map(res => res.data));
  }

requestMapTags(topic: string){
    this.api.tagRequest(topic).subscribe(res => {
      console.log(res);
      this.tagsRequestAnswer = res;
    }, err => {
      console.log(err);
    });
}

09-25 22:10