[Vuejs]-How to call vue router from inside custom function

2👍

Multiple ways to handle this, I’d suggest you use arrow functions and learn about their differences to the other function style.

to be clear:
replace

function (data) {
    EventBus.$emit('loaderStop')
    this.$router.push('/awaiting');
}

with

data => {
    EventBus.$emit('loaderStop');
    this.$router.push('/awaiting');
}
👤Sandro

1👍

You question context is not clear enough. If the code is executed in exponent methods, you can use arrow funciton like (agrs,...) => {...}. Otherwise, if this is not the case, you can use bind function like (function() {}).bind(this). Or just import $router in your code module.

Hope this can help you.

👤Xhua

0👍

The answers from Sandro and Xhua are perfect. I just want to explain, WHY you get the error:

The problem is “this.”. It refers to the parent object. So in your case “this.” refers to the authUser Object and not to Vue. For your understanding: You could define "var that = this" outside of your authUser object and then use “that.” inside. Or you go for the more sophisticated solutions.

👤ESP32

Leave a comment