具體步驟如下:
1、掛載完成后,判斷瀏覽器是否支持popstate
mounted(){ if (window.history && window.history.pushState) { history.pushState(null, null, document.URL); window.addEventListener('popstate', this.goBack, false); } },
2、頁面銷毀時,取消監聽。否則其他vue路由頁面也會被監聽
destroyed(){ window.removeEventListener('popstate', this.goBack, false); },
3、將監聽操作寫在methods里面,removeEventListener取消監聽內容必須跟開啟監聽保持一致,所以函數拿到methods里面寫
methods:{ goBack(){ //replace替換原路由,作用是避免回退死循環 this.$router.replace({path: '/mobileMtRoomList'}); } }