vue脚手架项目禁用页面浏览器回退与前进功能(完整且通用)

举例:

我这里有两个页面,分别如下,由登录signin页面点击进入到layout页面。现在需要禁用如下浏览器回滚页面:
在这里插入图片描述

步骤一

在main.js中加入如下代码:

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

步骤二

今天router下的index.js文件。

  1. 在const router = new Router({})中加入如下代码:
const router = new Router({
    
    
//从这里往下是需要添加的部分
mode: 'hash',
scrollBehavior: () => {
    
    
	history.pushState(null, null, document.URL)
},
//从这里往上是需要添加的部分
routes: [{
    
    }]
}
  1. 加入如下代码:
router.afterEach((to, from) => {
    
    
	history.pushState(null, null, location.protocol + '//' + location.host + '/#' + to.path)
})
完成以上两步即可全局禁止回退与前进

猜你喜欢

转载自blog.csdn.net/jxysgzs/article/details/115208957