gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:25:57 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com