vue3+el-select实现触底加载更多功能(ts版)
- 技术交流
- 2024-10-01 11:37:01
目录
- 思路
- main.ts
- home.vue
思路
使用全局指令来实现触底加载功能
通过
el-select
中的popper-class
给下拉框加入一个类名
在src下的utils文件夹中创建selectLoadMoreDirective.ts文件
import { Directive, DirectiveBinding } from 'vue' const loadMore: Directive = { beforeMount(el: any, binding: DirectiveBinding) { console.log(el) const selectDom = (document.querySelector(".more_select_dropdown") as any).querySelector(".el-select-dropdown .el-select-dropdown__wrap"); console.log(selectDom, 'selectDom++++++++++++++'); function loadMores(this: any) { const isBase = this.scrollHeight - this.scrollTop <= this.clientHeight if (isBase) { binding.value && binding.value() } } el.selectDomInfo = selectDom el.userLoadMore = loadMores nextTick(() => { selectDom?.addEventListener('scroll', loadMores) }) }, beforeUnmount(el: any) { if (el.userLoadMore) { el.selectDomInfo.removeEventListener('scroll', el.userLoadMore) delete el.selectDomInfo delete el.userLoadMore } } } export default loadMore
main.ts
import { createApp } from 'vue' import App from './App.vue' import router from '/@/router' import loadMore from '@/utils/selectLoadMoreDirective' const app = createApp(App) app.use(router) app.directive('loadMore', loadMore)
home.vue
到此这篇关于vue3+el-select实现触底加载更多功能的文章就介绍到这了,更多相关vue3+el-select 加载更多功能内容请搜索讯客以前的文章或继续浏览下面的相关文章希望大家以后多多支持讯客!
vue3+el-select实现触底加载更多功能(ts版)由讯客互联技术交流栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“vue3+el-select实现触底加载更多功能(ts版)”