typescript 错误类型错误:无法读取未定义的属性“get”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45049545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:42:44 来源:igfitidea点击:
ERROR TypeError: Cannot read property 'get' of undefined
提问by has van
ERROR TypeError: Cannot read property 'get' of undefined
im new in angular i really need help and thanks in advance
我是 angular 新手,我真的需要帮助并提前致谢
constructor(private http: Http, private authService: AuthService, private router: Router) {
this.token = localStorage.getItem('token');
if(token){
this.interval = setInterval(this.ingameee, 5000);
}
}
ingameee() {
this.http.get('http://url.com/api/matchmaking/keepalive?token='+ this.token)
.subscribe(
response => {
this.dataa = response;
this.mod = this.dataa.json().mod;
this.playersinqueue = this.dataa.json().playersinqueue;
this.region_id = this.dataa.json().region_id;
this.party_id = this.dataa.json().party_id;
},
error => console.log(error),
);
}
回答by Pengyy
keep in mind to use arrow function
for keeping the context while using callbacksbecause this
(the context) will change.
请记住arrow function
用于在使用回调时保持上下文,因为this
(上下文)会改变。
this.interval = setInterval(() => this.ingameee(), 5000);