vue3 通过自定义指令在table中滚动加载数据

1.在utils文件中新建一个loadMore.ts文件。

import type { Directive, App } from 'vue';

const debounce = function (func: any, delay: any) {
  let timer: any = null
  return function () {
    if (timer) clearTimeout(timer)
    timer = null
    let self = this
    let args = arguments
    timer = setTimeout(() => {
        func.apply(self, args)
    }, delay)
  }
}

const loadMore: Directive = {
  mounted (el: any, binding: any, vnode: any) {
    const { expand } = binding.modifiers
    // 使用更丰富的功能,支持父组件的指令作用在指定的子组件上
    if (expand) {
      /**
       * target 目标DOM节点的类名
       * distance 减少触发加载的距离阈值,单位为px
       * func 触发的方法
       * delay 防抖时延,单位为ms
       * load-more-disabled 是否禁用无限加载
       */
      let { target, distance = 0, func, delay = 200 } = binding.value
      if (typeof target !== 'string') return
      let targetEl = el.querySelector(target)
      if (!targetEl) {
        console.log('找不到容器')
        return
      }
      binding.handler = function () {
        const { scrollTop, scrollHeight, clientHeight } = targetEl
        
        let disabled = el.getAttribute('load-more-disabled')
        disabled = vnode[disabled] || disabled
        if (scrollHeight <= scrollTop + clientHeight + distance) {
          if (disabled == 'true') return
          func && func()
        }
      }
      targetEl.addEventListener('scroll', binding.handler)
    } else {
      binding.handler = debounce(function () {
        const { scrollTop, scrollHeight, clientHeight }  = el
        if (scrollHeight === scrollTop + clientHeight) {
            binding.value && binding.value()
        }
      }, 200)
      el.addEventListener('scroll', binding.handler)
    }
  },
  unmounted (el: any, binding: any) {
    let { arg } = binding
    // 使用更丰富的功能,支持父组件的指令作用在指定的子组件上
    if (arg === 'expand') {
        /**
         * target 目标DOM节点的类名
         * offset 触发加载的距离阈值,单位为px
         * method 触发的方法
         * delay 防抖时延,单位为ms
         */
        const { target } = binding.value
        if (typeof target !== 'string') return
        let targetEl = el.querySelector(target)
        targetEl && targetEl.removeEventListener('scroll', binding.handler)
        targetEl = null
    } else {
        el.removeEventListener('scroll', binding.handler)
        el = null
    }
  }
};

export function setupLoadMoreDirective(app: App) {
  app.directive('loadMore', loadMore);
}

export default loadMore;

2.在main.ts引入该文件,挂在到app上。

import { setupLoadMoreDirective } from '@/utils/loadMore';

const app = createApp(App);
setupLoadMoreDirective(app)

3.在vue文件中加上该指令

  
const loadmore = () => {
  console.log(2);
}

你可能感兴趣的:(elementPlus,vue3,elementPlus)