在vue项目中设置阻止浏览器的回退

由于我们的项目导航是用的elmentUI的导航,点击设置activeIndex,所以当浏览器返回页面的时候,触发不了导航当前项的变化,所以要禁止掉

在main.js中设置

new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>',
    data(){
        return {
            IsUseWebsocket: true,
            myCtrl: null
        }
    },
    mounted(){
        this.prevent_back();
    },
    methods:{
        prevent_back(){
            const _this = this;
            window.pushStateFun = function(){
                let path = '#'+_this.$route.path;;
                console.log(path);
                let state = { 
                    title: null,
                    url: path,
                }; 
                window.history.pushState(state,null, path);
            }
            window.pushStateFun();
            window.removeEventListener("popstate", window.pushStateFun, false);
            window.addEventListener("popstate", window.pushStateFun, false);
        }
       
    }
})

猜你喜欢

转载自blog.csdn.net/qq_34664239/article/details/107938209