Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route is changed when history.pushState() and history.back() manually #956

Closed
Senasiko opened this issue May 19, 2021 · 3 comments
Closed

Comments

@Senasiko
Copy link

Version

4.0.8

Reproduction link

https://jsfiddle.net/4um9abzg/4/

Steps to reproduce

  1. click pushState
  2. click back
  3. look at console

the callback of router.beforeEach is triggered.

but it won't be triggered in vue@2.x and vue-router@3.x

vue-route@3.x link: https://codesandbox.io/s/magical-leaf-mxd4l

What is expected?

the callback of router.beforeEach will not be triggered.

What is actually happening?

the callback of router.beforeEach is triggered.

@edison1105
Copy link
Member

Vue Router saves information on the history.state. If you have any code manually calling history.pushState(), you should likely avoid it or refactor it with a regular router.push() and a history.replaceState()

see https://next.router.vuejs.org/guide/migration/#usage-of-history-state

@Senasiko
Copy link
Author

@edison1105 , thanks, but it is not what I expected.

// replace
history.pushState(myState, '', url)
// with
await router.push(url)
history.replaceState({ ...history.state, ...myState }, '')

By this way, url and page content will both be changed, but I just want to change the url.

@posva
Copy link
Member

posva commented May 20, 2021

I see this rather as a fix and as @edison1105 mentioned you should not be calling history.pushState yourself.

If you want to change the url but not keep the router-view in sync, use the route prop of RouterView

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants