Home > Enterprise >  Access this.$router in debounce function
Access this.$router in debounce function

Time:10-25

I want to change the URL according to the input a user types. The debounce function is working properly. But I can't seem to access the 'this' variable.

'this' implicitly has type 'any' because it does not have a type annotation

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

Also the data send back by debounce is the last letter. Is it possible to get the full sentence?

Edit: found the full value at e.target.value

CodePudding user response:

You can make an arrow function to keep this' context:

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