vue 登录后禁止指定url浏览器后退
在main.js中
window.addEventListener('popstate', function() {
if (this.document.URL.indexOf('/homePage') > -1) {
history.pushState(null, null, document.URL)
}
})
在src/router/index.js
scrollBehavior: () => {
if (this.document.URL.indexOf('/homePage') > -1) {
history.pushState(null, null, document.URL)
}
}
将if条件去掉后,就可以使项目全部禁止浏览器后退
如果只想让浏览器能回退一次
在login/index.vue中,写在mounted里
history.pushState(null, null, window.location.href)
window.addEventListener('popstate', function() {
history.pushState(null, null, window.location.href)
})
如果控制台报document未定义的错,可以把this去掉试试看