Angular 6 : where getting error module “rxjs/add/operator/map” and another error 'map' does not exist on type 'Observable<Response>'

拈花ヽ惹草 提交于 2019-12-30 17:19:49

问题


I am using Angular 6 where I am getting two errors -

  1. ERROR in ./src/app/app/img/img.service.ts Module not found: Error: Can't resolve 'rxjs/add/operator/map' in '/Users/user/Projects/A4/imageSearch/src/app/app/img'

  2. ERROR in src/app/app/img/img.service.ts(21,9): error TS2339: Property 'map' does not exist on type 'Observable'.


回答1:


I faced similar problem with rxjs map operator. Currently I'm using Angular 6. To know which version you are using:

ng --version

or

ng -v

If you are also using angular 6, then please checkout https://www.academind.com/learn/javascript/rxjs-6-what-changed/

  1. Different internal structure that requires you to change your import statements
  2. pipe() as a method to chain your operators, the old way of chaining them will not work

Let's say you use map for http.get method:

import { map } from 'rxjs/operators';

private url = "some site...";
  constructor(private http: HttpClient) { }

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

NOT: this.http.get(this.url).map(result => result);




回答2:


This is how we can import observable and map in rxjs 6

import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';



回答3:


Run command in your package.

npm install --save rxjs-compat

And import below line in your file.

import 'rxjs-compat';




回答4:


With rxjs 6, import path is changed. Please import operator like this

  1. import {Observable} from 'rxjs';
  2. import {map} from 'rxjs/operators';

Hope it will work.



来源:https://stackoverflow.com/questions/50837367/angular-6-where-getting-error-module-rxjs-add-operator-map-and-another-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!