[英]'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.