Is there any negative to calling a method after a router push? (Vue.js)

1.2k Views Asked by At

Is there any negative to calling a method after a router push?

eg.

if (response.data.processPayment.successfulPayment) {
   this.$router.push('/success-payment')
   this.gtm_tracking(response.data.processPayment)
} else {
   ...
}

As opposed to having the gtm_tracking before the $router.push incase something fails in the tracking I dont want it to prevent the redirect. It appears to work fine. I’m just wondering if anyone knows any gotchas with doing this?

1

There are 1 best solutions below

2
Kaicui On

To prevent the tracking code from disrupting the route push,you can catch the exception of the code:


if (response.data.processPayment.successfulPayment) {

   // try to execute the tracking code
   try{
      this.gtm_tracking(response.data.processPayment);
   }catch(e){
      // handle tracking error
   }

   // this will allways be executed :)
   this.$router.push('/success-payment')

} else {
   ...
}