具体步骤以下:vue
一、挂载完成后,判断浏览器是否支持popstate浏览器
mounted(){ if (window.history && window.history.pushState) { history.pushState(null, null, document.URL); window.addEventListener('popstate', this.goBack, false); } },
二、页面销毁时,取消监听。不然其余vue路由页面也会被监听函数
destroyed(){ window.removeEventListener('popstate', this.goBack, false); },
三、将监听操做写在methods里面,removeEventListener取消监听内容必须跟开启监听保持一致,因此函数拿到methods里面写this
methods:{ goBack(){ //replace替换原路由,做用是避免回退死循环 this.$router.replace({path: '/mobileMtRoomList'}); } }