Reputation:
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
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