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

前端 未结 19 2900
时光取名叫无心
时光取名叫无心 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: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 !

提交回复
热议问题