Vue-router: How to go back two same routes in history stack

Viewed 459

I am using Vue 2.x.

There is a post page. When I click on a button in the post page, I go to the edit page. And when I click submit on the edit page, I used this.$route.replace(link_to_post_page) in order to remove the edit page from the history stack, and push the post page.

So now I have two of the same routes(the post page) in my history stack.

The problem is, when I click on the browser's back button, I go to the same page, that is, the post page. This is a very unnatural flow of pages for the user, and I want to fix it.

I have researched on ways to fix this for 6 hours, however I failed to find the solution.

My first try was to use Navigation Guards. But Navigation Guards only work when the route changes. In my case, the route does not change.

I also tried using window.history.onPopState event listener but failed with that too, because I could not manipulate the route when using window.history.onPopState.

I would really appreciate your help. Thanks.

P.S. The most similar question on Stack Overflow is this vue-router: skip page when using browser back button but it does not answer my question at all. I have checked other questions but they also don't answer my question.

1 Answers

Why not going back in the history instead of pushing a new /edit route?

Steps Routes stack
/post ['post']
-> Click on "edit" button
/edit ['post', 'edit']
-> this.$router.back()
/post ['post', 'edit']

The drawback of this is that the user still can manually go forward to the /edit page.
But if they go on another page, it will override the next steps (the /edit route above on the stack)

Related