search

Home  >  Q&A  >  body text

Access this.$router in debounce function

I want to change the URL based on what the user enters. The anti-shake function works fine. But I can't seem to access the "this" variable.

"this" implicitly has type "any" because it has no type annotation

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

And the data sent back by debounce is the last letter. Can I get a complete sentence?

EDIT: Found complete value at e.target.value

P粉541796322P粉541796322462 days ago685

reply all(1)I'll reply

  • P粉968008175

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

    You can create an arrow function to preserve this' context:

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

    reply
    0
  • Cancelreply