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 里面 判斷當(dāng)前路由的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:
{
path: '/home',
component: xxx,
meta: {allowBack: false}
}
2. Get the allowBack status in the global router.beforeEach function, and update the allowBack value of vuex at the same time, such as:
let allowBack = true // 給個默認值true
if (to.meta.allowBack !== undefined) {
allowBack = to.meta.allowBack
if (!allowBack) {
history.pushState(null, null, location.href)
}
}
store.dispatch('updateAppSetting', {
allowBack: allowBack
})
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:
window.onpopstate = () => {
if (!this.allowBack) { // 這個allowBack 是存在vuex里面的變量
history.go(1)
}
}
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
//改寫返回函數(shù),返回的時候就會觸發(fā)這個,
//你也可以直接監(jiān)聽瀏覽器的返回事件,定義一個變量就行了,邏輯跟這個差不多
Router.prototype.goBack = function () {
this.isBack = true
window.history.go(-1)
}
//假如當(dāng)前頁面是b頁面,是由a頁面點擊過來的,現(xiàn)在b頁面點擊返回鍵,不能返回到a頁面
router.beforeEach( (to, from, next) => {
//一當(dāng)點擊返回鍵,那么to就是a頁面,from就是b頁面
if(!from.meta.allowBack){
//進行頁面判斷,取出history里面之前的url,對這個url進行判斷,看他等不等于to這個頁面
//因為安全原因,js沒法獲取history里的url,或者獲取麻煩,所以你就要自己來記住url
//就是每進入一個頁面,你都去把之前的頁面路徑存在sessionStorage中
//···判斷過程省略
//這里取出,然后對比就可以了
//如果等于的話,直接禁止
//取出結(jié)果
var path = sessionStorage.getItem('path');
//這個this我沒有實驗,應(yīng)是指向router
if(path == to.path && this.isBack){
//什么都不做,只要不執(zhí)行next方法,路由是不會跳的
this.isBack = false;
} else {
//否則的話,就代表不是點擊的返回鍵,該跳轉(zhuǎn)就跳轉(zhuǎn)
//這里也存儲
sessionStorage.setItem('path',from.path);
this.isBack = false;
next()
}
}else{
//在這里存儲
sessionStorage.setItem('path',from.path);
this.isBack = false;
next();
}
});