首頁  >  問答  >  主體

在 debounce 函數中存取 this.$router

我想根據使用者輸入的內容更改 URL。 防手震功能運作正常。但我似乎無法存取“this”變數。

“this”隱式有型別“any”,因為它沒有型別註解

searchbarPokemon: debounce(function (e: any) {
  this.$router.push(e.target.value);  
  console.log(e)
}, 1000)

而且去抖動發回的資料是最後一個字母。可以得到完整的句子嗎?

編輯:在e.target.value處找到完整值

P粉541796322P粉541796322378 天前603

全部回覆(1)我來回復

  • P粉968008175

    P粉9680081752023-09-07 15:38:08

    您可以建立一個箭頭函數來保留this'上下文:

    searchbarPokemon: debounce((e: any) => {
      this.$router.push(e.target.value);  
      console.log(e)
    }, 1000)
    

    回覆
    0
  • 取消回覆