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

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

    Using Observable.subscribe directly should work.

    @Injectable()
    export class HallService {
        public http:Http;
        public static PATH:string = 'app/backend/'    
    
        constructor(http:Http) {
            this.http=http;
        }
    
        getHalls() {
        // ########### No map
               return this.http.get(HallService.PATH + 'hall.json');
        }
    }
    
    
    export class HallListComponent implements OnInit {
        public halls:Hall[];
        / *** /
        ngOnInit() {
            this._service.getHalls()
               .subscribe(halls => this.halls = halls.json()); // <<--
        }
    }
    

提交回复
热议问题