Angular HTTP GET with TypeScript error http.get(…).map is not a function in [null]

前端 未结 19 2445
时光取名叫无心
时光取名叫无心 2020-11-22 03:36

I have a problem with HTTP in Angular.

I just want to GET a JSON list and show it in the view.

Service class

im         


        
相关标签:
19条回答
  • 2020-11-22 04:18

    this is happening because you are using the rxjs and in rxjs function are not static which means you can't call them directly you have to call the methods inside the pipe and import that function from the rxjs library

    But if you are using rxjs-compat then you just need to import the rxjs-compat operators

    0 讨论(0)
  • 2020-11-22 04:20

    The map you using here, is not the .map() in javascript, it's Rxjs map function which working on Observables in Angular...

    So in that case you need to import it if you'd like to use map on the result data...

    map(project: function(value: T, index: number): R, thisArg: any): Observable<R> Applies a given project function to each value emitted by the source Observable, and emits the resulting values as an Observable.

    So simply import it like this:

    import 'rxjs/add/operator/map';
    
    0 讨论(0)
  • 2020-11-22 04:21

    Plus what @mlc-mlapis commented, you're mixing lettable operators and the prototype patching method. Use one or the other.

    For your case it should be

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { Observable } from 'rxjs';
    import 'rxjs/add/operator/map';
    
    @Injectable()
    export class SwPeopleService {
        people$ = this.http.get('https://swapi.co/api/people/')
          .map((res:any) => res.results);
    
        constructor(private http: HttpClient) {} 
    }
    

    https://stackblitz.com/edit/angular-http-observables-9nchvz?file=app%2Fsw-people.service.ts

    0 讨论(0)
  • 2020-11-22 04:22

    For Angular versions 5 and above, the updated importing line looks like :

    import { map } from 'rxjs/operators';
    

    OR

    import { map } from 'rxjs/operators';
    

    Also these versions totally supports Pipable Operators so you can easily use .pipe() and .subscribe().

    If you are using Angular version 2, then the following line should work absolutely fine :

    import 'rxjs/add/operator/map';
    

    OR

    import 'rxjs/add/operators/map';
    

    If you still encounter a problem then you must go with :

    import 'rxjs/Rx';
    

    I won't prefer you to use it directly bcoz it boosts the Load time, as it has a large number of operators in it (useful and un-useful ones) which is not a good practice according to the industry norms, so make sure you try using the above mentioned importing lines first, and if that not works then you should go for rxjs/Rx

    0 讨论(0)
  • 2020-11-22 04:24

    With Angular 5 the RxJS import is improved.

    Instead of

    import 'rxjs/add/operator/map';
    

    We can now

    import { map } from 'rxjs/operators';
    
    0 讨论(0)
  • 2020-11-22 04:27

    Since Http service in angular2 returns an Observable type, From your Angular2 installation directory('node_modules' in my case),We need to import map function of the Observable in your component using http service,as:

    import 'rxjs/add/operator/map';
    
    0 讨论(0)
提交回复
热议问题