0

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?

M.Cooper
  • 317
  • 1
  • 5
  • 16
  • Does this answer your question? [How does the "this" keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – Ben Fortune Nov 05 '20 at 11:48

1 Answers1

2

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"
      });
   };
Ferry Kranenburg
  • 2,073
  • 1
  • 15
  • 21