javascript - 如何在 typescript 的公共(public)函数中初始化Http导入?

标签 javascript typescript

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

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
 }

最佳答案

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

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

关于javascript - 如何在 typescript 的公共(public)函数中初始化Http导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34962617/

相关文章:

javascript - 第二个 Wordpress Ajax 未触发

javascript - Ext.dom 未定义 Sencha Touch

javascript - d3 路径仅显示路径/线最多 328 个点而不是 329 个或更多

angular - 外部 javascript 库在 '{module}/dist/' 上返回 404

angular - 在给定时间发出 API 请求以更新当前广播节目 Angular2 RxJS

javascript - 在 TypeScript 中加载文件

javascript - Angular 7 使用 ng build --prod 缩小 Assets

javascript - 我可以在淡入/淡出 JQuery 函数中使用 <a> 标签按钮类吗?

javascript - 设置新单元格值时保留格式(Google 电子表格)

html - 我的问题是关于使用 Swiper http ://idangero. us 和 ionic 4