mounted() {
history.pushState(null, null, document.URL)
window.addEventListener('popstate', () => {
history.pushState(null, null, document.URL)
})
},
destroyed(){
window.removeEventListener("popstate",() => {
history.pushState(null, null, document.URL)
}, false);
}
说明
history.pushState(state, title, url)
方法向当前浏览器会话的历史堆栈中添加一个状态(state)。
- state: 状态对象可以是任何可以序列化的对象。
- title: 当前大多数浏览器都忽略此参数,尽管将来可能会使用它。
- url: 新历史
记录条目的 URL 由此参数指定。如果未指定此参数,则将其设置为文档的当前 URL。
popstate事件
当活动历史记录条目更改时,将触发 popstate 事件。如果被激活的历史记录条目是通过对history.pushState() 的调用创建的,或者受到对 history.replaceState() 的调用的影响,popstate事件的state属性包含历史条目的状态对象的副本。
需要注意的是调用history.pushState() 或 history.replaceState() 不会触发 popstate 事件。只有在做出浏览器动作时,才会触发该事件,如用户点击浏览器的回退按钮(或者在 Javascript 代码中调用 history.back() 或者 history.forward() 方法)