How to Inject Angular2 Http service into es6/7 Class?

前端 未结 4 507
生来不讨喜
生来不讨喜 2020-12-03 21:52

If I use es6/7 (babel - stage 1) instead of TypeScript, how are services, and specifically Http, injected?

Here\'s my component JS:

import {Compone         


        
4条回答
  •  情书的邮戳
    2020-12-03 22:24

    Method from the official API Review works for me:

    import {Http, HTTP_PROVIDERS} from 'angular2/http';
    @Component({
      selector: 'http-app',
      viewProviders: [HTTP_PROVIDERS],
      templateUrl: 'people.html'
    })
    class PeopleComponent {
      constructor(http: Http) {
        http.get('people.json')
          .map(res => res.json())
          .subscribe(people => this.people = people);
      }
    }
    

提交回复
热议问题