簡體   English   中英

當我從我的服務調用函數時,'this' 未定義

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM