javascript - vue single page app prevents browser from going back?
世界只因有你
世界只因有你 2017-07-05 11:02:51
0
2
2048

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

世界只因有你
世界只因有你

reply all(2)
给我你的怀抱

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

//改写返回函数,返回的时候就会触发这个,
//你也可以直接监听浏览器的返回事件,定义一个变量就行了,逻辑跟这个差不多
Router.prototype.goBack = function () {
  this.isBack = true
  window.history.go(-1)
}

//假如当前页面是b页面,是由a页面点击过来的,现在b页面点击返回键,不能返回到a页面
router.beforeEach( (to, from, next) => {
    //一当点击返回键,那么to就是a页面,from就是b页面
    if(!from.meta.allowBack){
        //进行页面判断,取出history里面之前的url,对这个url进行判断,看他等不等于to这个页面
        //因为安全原因,js没法获取history里的url,或者获取麻烦,所以你就要自己来记住url
        //就是每进入一个页面,你都去把之前的页面路径存在sessionStorage中
        //···判断过程省略
        //这里取出,然后对比就可以了
        //如果等于的话,直接禁止
        //取出结果
        var path = sessionStorage.getItem('path');
        //这个this我没有实验,应是指向router
       if(path == to.path && this.isBack){
           //什么都不做,只要不执行next方法,路由是不会跳的
           this.isBack = false;
       } else {
           //否则的话,就代表不是点击的返回键,该跳转就跳转
           //这里也存储
            sessionStorage.setItem('path',from.path);
             this.isBack = false;
           next()
       }
    }else{
        //在这里存储
        sessionStorage.setItem('path',from.path);
        this.isBack = false;
        next();
    }
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template