【发布时间】:2016-07-12 14:38:26
【问题描述】:
我有课。我需要在超时内做一些 http 工作。我面临的问题是超时内的http变量一直说它是未定义的。
export class MyClass {
http:Http:
constructor(private http:Http) {
this.http = http;
}
sendFriendRequest(){
this.http.post( ...//http variable is defined here
setTimeout(function(){
this.http.post(... //http is not defined here
}
}
}
【问题讨论】:
-
这是因为
this上下文:stackoverflow.com/questions/2130241/… -
我们真的需要 4 个答案来回答这个问题吗?基本上都说“使用箭头函数来获得正确的
this上下文”? -
@MarkRajcok 是的,只有 3 个答案是不够的!
标签: angular typescript