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

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

提交回复
热议问题