首页 文章

Angular6 Map 不存在

提问于
浏览
1

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

但我的service.ts文件中出现错误 . 抛出错误“[ts]属性'map'在类型'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);
  }

}

所以我请你帮我解决这个问题 . 谢谢 .

2 回答

  • 0

    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)); 
    }
    
  • 1

    根据最新的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)}));
      }
    

相关问题