最近使用uniapp做微信H5时,有一个需求是如果该用户登录后会显示用户禁用的页面,点击手机物理返回按钮,不能返回到前一个页面 使用的方法是监听用户点击返回按钮的事件,当用户在当前页面点击了返回按钮,就重新将当前页面的url插入浏览器的历史记录中
//在mounted里面开启监听返回按钮
mounted(){
//页面加载后先插一条记录
window.history.pushState(null, null, document.URL);
window.addEventListener("popstate", this.onBack(), false);
}
methods:{
onBack(){
//检测到返回再插一条
window.history.pushState(null, null, document.URL);
}
},
//在destroyed中销毁监听
destroyed() {
window.removeEventListener("popstate", this.onBack, false);
},