user9348468
user9348468

Reputation:

change browser go back event in vue js

I need to detect the back function in the browser and need to redirect to a different page using vue js. My attempt is as below.

mounted: function () {
    window.onpopstate = function(event) {
      this.$router.push({
        path: "/register"
      });
   };
}

But it will not identify $router and saying it's not a function. How can I solve this problem?

Upvotes: 0

Views: 1015

Answers (1)

Ferry Kranenburg
Ferry Kranenburg

Reputation: 2635

this inside the function is not instance of vue component so it will not work. Try this code:

  mounted: function () {
    window.onpopstate = (event) => {
      this.$router.push({
        path: "/register"
      });
   };

Upvotes: 1

Related Questions