变量
data() { return { flag: false, form: { title: '', subtitle: "", }, } }
方法
beforeRouteLeave(to, from, next) { //let one= JSON.parse(JSON.stringify(this.form)); let one = JSON.stringify({title: "1", subtitle: "2"}); let two = JSON.stringify({title: "1", subtitle: "3"}); if (one !== two && !this.flag) { next(false); setTimeout(() => { this.Confirm("数据没有保存,确定要退出吗?", () => { this.flag = true; this.$router.push({path: to.path}) }, () => { }); }, 500) } else { next() } }