只需3步即可監(jiān)聽物理返回鍵:
1.mounted中注冊監(jiān)聽方法:
//監(jiān)聽返回鍵
if (window.history && window.history.pushState) {
? history.pushState(null, null, document.URL);
? window.addEventListener('popstate', this.backButton, false);//false阻止默認(rèn)事件
}
2.methods中寫方法的實(shí)現(xiàn):
backButton () {//點(diǎn)擊返回鍵時(shí)實(shí)現(xiàn)的業(yè)務(wù)邏輯
},
3.在destroyed中寫:(退出頁面時(shí)銷毀監(jiān)聽事件全庸,防止其他頁面使用)
destroyed () {
window.removeEventListener('popstate',this.backButton,false);//false阻止默認(rèn)事件
? },