VUE 离开页面路由拦截

  • VUE 提供一个钩子函数 beforeRouteLeave, 离开当前路由时会先触发该函数,当然也有beforeRouteEnter,进入路由前先触发该函数。

  • to: 目标路由的相关信息 对象
  • from:当前路由的相关信息 对象
  • next:实行跳转的 函数。传入false,不进行跳转。
    beforeRouteLeave (to, from, next) {
         this.targetName = to.name  // 提示框点击确认后跳转的 路由
       if (this.checkEdit()) { // 是否对页面进行了编辑
          this.dialogVisible = true    // 打开离开页面的提示框
          next(false)
        } else {
          this.dialogVisible = false
          next()
        }
    }
原文地址:https://www.cnblogs.com/hexuerui/p/13283370.html