解決方案
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。
更多 history 的介紹參考:MDN(history)
popstate事件
當活動歷史記錄條目更改時,將觸發 popstate 事件。如果被激活的歷史記錄條目是通過對history.pushState()的調用創建的,或者受到對history.replaceState()的調用的影響,popstate事件的state屬性包含歷史條目的狀態對象的副本。
需要注意的是調用history.pushState()或history.replaceState()不會觸發popstate事件。只有在做出瀏覽器動作時,才會觸發該事件,如用戶點擊瀏覽器的回退按鈕(或者在Javascript代碼中調用history.back()或者history.forward()方法)
