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

前端 未结 19 2442
时光取名叫无心
时光取名叫无心 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:02
    import 'rxjs/add/operator/map';
    

    will resolve your problem

    I tested it in angular 5.2.0 and rxjs 5.5.2

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

    I tried below commands and it gets fixed:

    npm install rxjs@6 rxjs-compat@6 --save
    
    
    import 'rxjs/Rx';
    
    0 讨论(0)
  • 2020-11-22 04:03

    Angular version 6 "0.6.8" rxjs version 6 "^6.0.0"

    this solution is for :

      "@angular-devkit/core": "0.6.8",
      "rxjs": "^6.0.0"
    

    as we all know angular is being developed every day so there are lots of changes every day and this solution is for angular 6 and rxjs 6
    first to work with http yo should import it from : after all you have to declare the HttpModule in app.module.ts

    import { Http } from '@angular/http';
    

    and you have to add HttpModule to Ngmodule -> imports

      imports: [
        HttpModule,
        BrowserModule,
        FormsModule,
        RouterModule.forRoot(appRoutes)
      ],
    

    second to work with map you should first import pipe :

    import { pipe } from 'rxjs';
    

    third you need the map function import from :

    import { map } from 'rxjs/operators';
    

    you have to use map inside pipe like this exemple :

     constructor(public http:Http){  }
    
        getusersGET(){
            return this.http.get('http://jsonplaceholder.typicode.com/users').pipe(
             map(res => res.json()  )  );
        }
    

    that works perfectly good luck !

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

    Angular 6 - only import 'rxjs/Rx' did the trick for me

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

    Just add the line in you file,

    import 'rxjs/Rx';

    It will import bunch of dependencies.Tested in angular 5

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

    Just some background... The newly minted Server Communication dev guide (finally) discusses/mentions/explains this:

    The RxJS library is quite large. Size matters when we build a production application and deploy it to mobile devices. We should include only those features that we actually need.

    Accordingly, Angular exposes a stripped down version of Observable in the rxjs/Observable module, a version that lacks almost all operators including the ones we'd like to use here such as the map method.

    It's up to us to add the operators we need. We could add each operator, one-by-one, until we had a custom Observable implementation tuned precisely to our requirements.

    So as @Thierry already answered, we can just pull in the operators we need:

    import 'rxjs/add/operator/map';
    import 'rxjs/operator/delay';
    import 'rxjs/operator/mergeMap';
    import 'rxjs/operator/switchMap';
    

    Or, if we're lazy we can pull in the full set of operators. WARNING: this will add all 50+ operators to your app bundle, and will effect load times

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