vue + ts + element下拉加载更多指令

20 篇文章 0 订阅
7 篇文章 0 订阅
import { DirectiveOptions } from "vue";

/**
 * 对 element-ui 的无限滚动在 el-table 上使用的封装
 */
import elInfiniteScroll from "element-ui/lib/infinite-scroll";

const elScope = "ElInfiniteScroll"; // scope name
const msgTitle = `[el-table-infinite-scroll]: `; // message title
const elTableScrollWrapperClass = ".el-table__body-wrapper";

const loadMore: DirectiveOptions = {
  inserted(el, binding, vnode, oldVnode) {
    // 获取 table 中的滚动层
    const scrollElem: HTMLImageElement | null = el.querySelector(
      elTableScrollWrapperClass
    );

    // 如果没找到元素,返回错误
    if (!scrollElem) {
      throw `${msgTitle}找不到 ${elTableScrollWrapperClass} 容器`;
    }

    // 设置自动滚动
    scrollElem.style.overflowY = "auto";

    // dom 渲染后
    setTimeout(() => {
      if (!el.style.height) {
        scrollElem.style.height = "400px";
        console.warn(
          `${msgTitle}请尽量设置 el-table 的高度,可以设置为 auto/100%(自适应高度),未设置会取 400px 的默认值(不然会导致一直加载)`
        );
       }
      console.log(7777777777777, scrollElem);
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      asyncElOptions(vnode, el, scrollElem);

      // 绑定 infinite-scroll
      elInfiniteScroll.inserted(scrollElem, binding, vnode, oldVnode);

      // 将子集的引用放入 el 上,用于 unbind 中销毁事件
      el[elScope] = scrollElem[elScope];
    }, 0);
  },
  componentUpdated(el, binding, vnode) {
    const targetElem = el.querySelector(elTableScrollWrapperClass);

    if (targetElem) {
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      asyncElOptions(vnode, el, targetElem);
    }
  },
  unbind: elInfiniteScroll.unbind
};

/**
 * 同步 el-infinite-scroll 的配置项
 * @param sourceVNode
 * @param sourceElem
 * @param targetElem
 */
function asyncElOptions(
  sourceVNode: VNode,
  sourceElem: HTMLElement,
  targetElem: Element
) {
  let value: string | null;
  ["disabled", "delay", "immediate"].forEach(name => {
    name = "infinite-scroll-" + name;
    value = sourceElem.getAttribute(name);
    if (value !== null) {
      targetElem.setAttribute(name, value);
    }
  });

  // fix: windows/chrome 的 scrollTop + clientHeight 与 scrollHeight 不一致的 BUG
  const name = "infinite-scroll-distance";
  value = sourceElem.getAttribute(name);
  targetElem.setAttribute(name, value < 1 ? 1 : value);
}

export default loadMore;
Vue 3 结合 TypeScript 和 Element UI 的项目中,要显示图片列表,你可以按照以下步骤操作: 1. **设置环境**: - 安装必要的依赖:首先确保已经安装了 Vue CLI、TypeScript 和 Element UI。可以运行 `vue create my-project` 创建新项目,然后添加 TypeScript 支持 `vue add typescript`。 2. **配置 TypeScript**: 在 `src/main.ts` 中配置 TypeScript 编译选项,确保正确导入和使用 TypeScript。 ```typescript import { createApp } from 'vue' import App from './App.vue' import { ElOption, ElCarouselItem } from '@element-plus/components/carousel' // 引入Element UI 图片组件 // 配置TypeScript const app = createApp(App).use(ElementPlus) app.component('el-option', ElOption) // 允许在Vue中直接使用Element Plus的组件 app.component('el-carousel-item', ElCarouselItem) app.mount('#app') ``` 3. **创建组件**: - 创建一个显示图片列表的组件,例如 `ImageList.vue`,可以使用 Element 的轮播图 (`<el-carousel>`) 或者普通的列表 (`<el-row>` 和 `<el-col>`): ```html <template> <div> <!-- 使用Element Plus的轮播图展示图片 --> <el-carousel :options="carouselOptions"> <el-carousel-item v-for="(item, index) in images" :key="index"> <img :src="item.url" alt="图片描述"> </el-carousel-item> </el-carousel> <!-- 或者使用列表展示 --> <!-- <el-row> <el-col v-for="item in images" :key="item.id"> <img :src="item.url" alt="图片描述"> </el-col> </el-row> --> </div> </template> <script lang="ts"> import { Component, Prop } from 'vue' interface Image { id: number url: string } @Component({ components: { ElOption, ElCarouselItem } }) export default class ImageList extends Vue { @Prop() images: Image[] = [] carouselOptions = { autoplay: true, // 自动播放轮播 interval: 5000, // 每隔5秒切换一张图片 dots: true, // 显示指示点 arrows: false // 是否显示左右箭头 } } </script> ``` 4. **在父组件中使用**: 在主应用组件(如 `App.vue`)中引入并传入图片数据给 `ImageList` 组件。 现在你已经有了一个基本的图片列表展示,可以根据需求调整样式和交互。有关详细的文档或遇到问题时,记得查看官方 Element UI 和 Vue 的文档以及TypeScript 的指南。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜丶陌颜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值