1. 点击元素之外触发函数
    <template>
      <div v-clickoutside="clickItemOut"></div>
    </template>
    
    const clickoutside = {
      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)
      },
      unbind (el, binding) {
        document.removeEventListener('click', el.__vueClickOutside__)
        delete el.__vueClickOutside__
      }
    }
    
    组件中声明自定义指令(全局也可以,改写成全局各式就好了)
    directives: { clickoutside }
    
    methods: {
        clickItemOut (e) {
          // 点击元素之外触发函数
        }

     

相关文章:

  • 2021-07-29
  • 2021-09-11
  • 2021-06-04
猜你喜欢
  • 2021-10-14
  • 2021-08-31
  • 2021-11-28
  • 2021-08-13
  • 2021-06-19
  • 2021-12-10
相关资源
相似解决方案