如何在打字稿的公共函数中初始化 Http 导入

How to initialize Http import in public function in typescript?

本文关键字:函数 初始化 导入 Http      更新时间:2023-09-26

在下面的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
 }

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

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