只需3步即可监听物理返回键:
1.mounted中注册监听方法:
//监听返回键
if (window.history && window.history.pushState) {
history.pushState(null, null, document.URL);
window.addEventListener('popstate', this.backButton, false);//false阻止默认事件
}
2.methods中写方法的实现:
backButton () {//点击返回键时实现的业务逻辑
},
3.在destroyed中写:(退出页面时销毁监听事件,防止其他页面使用)
destroyed () {
window.removeEventListener('popstate',this.backButton,false);//false阻止默认事件
},