// 2:需要在退出页面的时候销毁这个监听事件
destroyed() {
window.removeEventListener("popstate", this.goback, false);
},
mounted() {
if (window.history) {
history.pushState(null, "", document.URL); //这里有没有都无所谓,最好是有以防万一
window.addEventListener("popstate", this.goback, false); // 回退时执行
}
},
methods: {
goback(){
if(this.status=='已交卷'){
this.$router.push({
path:"/exam/mytest",
})
}else{
this.submit()
}
},
submit(){
let num =0
this.examvaultList.forEach((item)=>{
if(item.userAnswer==''){
num+=1
}
})
if(num>0){
this.$confirm("您还有未作答的题,确认交卷?", {
type: "warning",
}).then((_) => {
this.submitExam()
}).catch((_) => {
//一定要在取消时重新写一遍,否则只生效一次,第二次返回浏览器就不管用了
history.pushState(null, "", document.URL);
window.addEventListener("popstate", this.goBack, false);
});
}else{
this.submitExam()
}
},
},
vue答题界面监听浏览器返回事件
最新推荐文章于 2023-08-04 10:30:34 发布