vue对象比较,阻止后退

变量

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()
      }
    }

猜你喜欢

转载自www.cnblogs.com/ronle/p/10833356.html