vue-clickoutside d

js文件

export default {
  bind(el, binding, vnode) {
    function documentHandler(e) {
      if (el.contains(e.target)) {
        return false
      }
      if (binding.expression) {
        binding.value(e)
      }
    }
    el.__vueClickOutside__ = documentHandler
    document.addEventListener('click', documentHandler)
    document.addEventListener('touchmove', documentHandler)
  },
  update() {},
  unbind(el, binding) {
    document.removeEventListener('click', el.__vueClickOutside__)
    document.removeEventListener('touchmove', el.__vueClickOutside__)
    delete el.__vueClickOutside__
  }
}

组件里头

v-clickoutside="handleClose"

import clickoutside from '../../directives/clickoutside'

directives: { clickoutside }

即刻!

posted on 2019-05-08 09:17  ouruixi  阅读(418)  评论(0编辑  收藏  举报

导航