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

前端 未结 4 506
生来不讨喜
生来不讨喜 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:19

    How I've already answered it here, If you write code in ES7, use static getter for parameters property to specify injections into constructor of your component. For example:

    import { Http } from 'angular2/http';
    // other imports ...
    
    // component decorators ...
    export class Login {
    
      static get parameters() {
        return [[Http]];
      }
    
      constructor(http) {
        this.http = http;
        console.log('http', http);
      }
    
      // other methods
    }
    

    I think it most concise method at this moment.

    Remember there is no proposal to support parameter decorators in ES7 at this moment (for example see this issue for Babel).

提交回复
热议问题