[英]'this' is undefined when I call a function from my service
我的component page
上有这个调用
this.timerSessionService.startTimer(this.finish.bind(this), this.onSynchronice);
那么,它与伟大工程this.finish
,但是当我打电话onSynchronice
从我的Service
仍正常工作,并进入我的component
时,我对这个问题涉及component
我尝试调用的函数是在我的组件使用的.this
,它说:
错误错误:未捕获(承诺):类型错误:无法设置未定义的属性“pew”
这是因为我的onSynchronice
功能我用.this
,然后它说,它不存在......我该如何解决?
onSynchronice(pew) {
console.log(pew["pew"]); //Works perfect
this.pew= pew; //Crashes because of .this
this.anyfunction(pew); //It crashes and says the same, anyfunction() does not exist, even if it exists
}
您可以将onSynchronice
定义为箭头函数:
onSynchronice = (pew) => {
console.log(pew["pew"]);
this.pew= pew;
this.anyfunction(pew);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.