vant-ui dialog 取消默认关闭事件

在使用 dialog 组件时,想要实现一个效果:点击确定时,先进行表单验证,验证不通过时,不能关闭弹框,但是vant 默认会关闭,因此我们只能通过 beforeClose 去阻止默认关闭事件:

 

<van-dialog
      v-model="visible"
      title="标题"
      show-cancel-button
      @confirm="confirm"
      @cancel="cancelDialog"
      :beforeClose="onBeforeClose"
>
  <van-form @submit="onSubmit" ref="form" />
</van-dialog>

methods: {
  confirm() {
  this.$refs.form.validate().then(res => {
    this.visible= false // 表单验证通过后手动关闭弹框
  }).catch(res => {
    ...
  })
},

  // 取消默认关闭弹框事件
  onBeforeClose(action, done) {
    return done(false)
  },
}

 

posted @ 2022-08-08 14:39  我就尝一口  阅读(2212)  评论(0编辑  收藏  举报