话不多说,直接上代码(项目用的是vue)
mounted() {
// 挂载完成后,判断浏览器是否支持popstate--物理返回键
if (window.history && window.history.pushState) {
history.pushState(null, null, document.URL);
window.addEventListener("popstate", this.goBack, false);
}
},
// 如支持popstate,在点击手机返回键时会触发下面的方法
goBack() {
console.log('触发物理返回键')
this.$router.go(-1);
},
//页面销毁时,取消监听。否则其他vue路由页面也会被监听--物理返回键
destroyed() {
window.removeEventListener("popstate", this.goBack, false);
},