举例:
我这里有两个页面,分别如下,由登录signin页面点击进入到layout页面。现在需要禁用如下浏览器回滚页面:
步骤一
在main.js中加入如下代码:
window.addEventListener('popstate', function() {
history.pushState(null, null, document.URL)
})
步骤二
今天router下的index.js文件。
- 在const router = new Router({})中加入如下代码:
const router = new Router({
//从这里往下是需要添加的部分
mode: 'hash',
scrollBehavior: () => {
history.pushState(null, null, document.URL)
},
//从这里往上是需要添加的部分
routes: [{
}]
})
- 加入如下代码:
router.afterEach((to, from) => {
history.pushState(null, null, location.protocol + '//' + location.host + '/#' + to.path)
})