如何在typescript中的公共函数中初始化Http导入?

时间:2016-01-23 11:01:39

标签: javascript typescript

在下面的Typescript代码中,我想调用一个需要导入的函数。我可以在类的构造函数中初始化导入,但是如何在公共函数中执行此操作?

import {Http} from "angular2/http";

export class AppComponent {

  constructor( properties.
    public http: Http
    ) {
      this.http.get(...){} //FUNCTION 1: this function works fine

      this.getAll(); //this does not work as FUNCTION 2 does not work
    }

 public getAll = function(){ //FUNCTION 2: this function does not work (cannot find name'http')
      http.get(...){} 
    }

 public otherfunction = function(){
      this.getAll(); //this does not work as FUNCTION 2 does not work
 }

1 个答案:

答案 0 :(得分:1)

可以通过this(当前实例)引用访问公共属性:

public getAll = function() {
  this.http.get(...); 
}