web app single-page application is written with vue vue-router.
Some pages need to be prohibited from going back. I wrote whether the current page can go back in the routing meta information meta, such as allowBack.
I found the information and the way to disable backing is
history.pushState(null, null, location.href)
The previous project used vue1.0, the solution is
1. 在全局的router.beforeEach 里面 判断当前路由的handler里面的allowBack变量是否为false
2. 如果为false 则 history.pushState(null, null, location.href)
Now we switch to vue2.0, the original method doesn’t work anymore,
The problem now is that I don’t know where to put the history.pushState(null, null, location.href) code.
Or do you have any other solutions? Thanks! !
In fact, the main usage scenario is this,
Click the tabbar to switch to a different page. If I am currently on page a and click tabbar to go to page b, I cannot return to page a through the return key, but I can click tabbar goes to page a
Question and answer...
The requirement is: a certain route cannot be returned through the browser without affecting switching between each other
Let’s sort out the solutions and usage:
1. Add meta information to this route in the routing configuration, such as:
2. Get the allowBack status in the global router.beforeEach function, and update the allowBack value of vuex at the same time, such as:
This code must be written after next(), because location.href written before next() is not the address of to, which is a bit different from vue1.0
3. The next step is the core, register the onpopstate event in mounted in app.vue:
Removing history.pushState(null, null, location.href) can also prevent backing off, but the component will be re-rendered, so this part is also critical
Try this and see