【问题标题】:Angular6 map doesn't existAngular6地图不存在
【发布时间】:2018-06-22 07:19:18
【问题描述】:

这是我在 Angular 6 中的第一个项目。我想在我的项目中使用 chart.js。我安装了chart.js并点击链接https://coursetro.com/posts/code/126/Let's-build-an-Angular-5-Chart.js-App---Tutorial

但我的 service.ts 文件出现错误。它抛出错误“[ts] Property 'map' does not exist on type 'Observable'。”

我的服务代码是

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';

@Injectable({
  providedIn: 'root'
})
export class DragchartService {

  constructor(private _http: HttpClient) { }
  dailyForecast() {
    return this._http.get("http://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22")
      .map(result => result);
  }

}

所以我请求您帮助我解决这个问题。谢谢。

【问题讨论】:

标签: javascript object rxjs observable angular6


【解决方案1】:

来自 CLI V6 的引导应用程序使用 rxjs 6。Rxjs6 使用可管道操作符,因此会引发该错误。

您引用的代码使用的是较早版本的 rxjs(可能是 rxjs 5)。

为了让您的代码与 rxjs 6 一起工作,它应该更新如下

import { map } from 'rxjs/operators';

// Your other code

dailyForecast() {
  return this._http.get(<your url>).pipe(map(result => result)); 
}

【讨论】:

    【解决方案2】:

    根据最新的 RxJS 版本,他们进行了更改,现在 http 请求可以扩展可以接受参数的管道,例如

    filter(x => x % 2 === 1), map(x => x + x),catchError(res=>{console.log(res)})
    

    从哪里导入

    import { Observable, Subject, ReplaySubject, from, of, range } from 'rxjs';
    import { map, filter, switchMap , CatchError } from 'rxjs/operators';
    

    所以现在如果你想使用 Map、CatchError 等,那么我们需要将它作为管道的参数传递

     dailyForecast() {
        return this._http.get("http://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22")
          .pipe(map(x => console.log(x)),catchError(res=>{console.log(res)}));
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 2019-01-25
      • 2019-03-20
      • 2019-01-09
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多