Vue 禁用浏览器的前进后退操作

一、禁用前进后退功能
1、main.js中,增加popstate监听

window.addEventListener('popstate', function() {
    
    
 history.pushState(null, null, document.URL)
})

2、router的index.js中

const router = new Router({
    
    
 mode: 'hash',
 routes,
 scrollBehavior: () => {
    
    
  history.pushState(null, null, document.URL)
 }
})

也可以尝试放在router的beforeEach/afterEach中

router.afterEach((to, from) => {
    
    
 history.pushState(null, null, location.protocol + '//' + location.host + '/#' + to.path)
})

猜你喜欢

转载自blog.csdn.net/u013994400/article/details/129144048