Error using Http JSON AngularJS 2

前端 未结 2 526
半阙折子戏
半阙折子戏 2021-01-20 06:20

I\'m trying to use the following code after trying several ways online tutorials I can not make it work.

..//
import {Http, HTTP_PROVIDERS} from \'angular2/h         


        
相关标签:
2条回答
  • 2021-01-20 06:31

    Seems like somehow Http isn't get injected correctly. Use @Inject to inject Http dependency.

    import { Component, Inject} from 'angular2/core';
    //above code should be at start.
    
    constructor(@Inject(Http) http: Http) {
    

    @Inject is needed for acquiring dependency when not using Typescript for transpilation of JS.

    0 讨论(0)
  • 2021-01-20 06:33

    Well, as the error message states this.http is not a function, it's an object which has several methods. You need this.http.get() method:

    this.http.get('resources/users.json')
        .map(res => res.json())
        .subscribe(users => this.users = users);
    
    0 讨论(0)
提交回复
热议问题