vue3 + ts,element-plus中el-table,使用自定义指令自适应表格高度

 怎么注册自定义指令就不说了,这个会有一个500s的动画效果。

使用的时候直接v-custom="{bottomOffset: 65}"就行了,bottomOffset是距离底部的距离,如果有分页组件或者别的内容,需要预留一下距离。

感兴趣小伙伴可以试一下。

import { DirectiveBinding } from "vue";
import { debounce } from "lodash-es";

interface HTMLElementCustom extends HTMLElement {
  resizeListener?:  () => void;
}


const doResize = debounce(async (el: HTMLElement, binding: DirectiveBinding) => {
  // 获取调用传递过来的数据
  const { value: { bottomOffset = 0 } = {} } = binding;
  const height = window.innerHeight - el.getBoundingClientRect().top - bottomOffset;
  el.style.transition = 'height 0.5s';
  el.style.height = `${height}px`;
}, 500)

export default {
  // 初始化设置
  beforeMount(el: HTMLElementCustom, binding: DirectiveBinding) {
    el.resizeListener = async () => {
      await doResize(el, binding);
    }
    el.resizeListener();
    window.addEventListener('resize', el.resizeListener);
  },
  // 销毁时设置
  beforeUnmount(el: HTMLElementCustom) {
    // 移除resize监听
    window.removeEventListener('resize', el.resizeListener as (this: Window, ev: UIEvent) => any);
  }
};

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Vue3和TypeScript结合Element-Plus实现头像上传的示例代码: ```vue <template> <div> <el-upload class="avatar-uploader" action="/upload" :show-file-list="false" :before-upload="beforeUpload" :on-success="handleSuccess" :on-error="handleError" > <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'AvatarUploader', setup() { const imageUrl = ref(''); const beforeUpload = (file: File) => { // 验证文件类型和大小等 return true; }; const handleSuccess = (response: any) => { // 上传成功后的处理逻辑 imageUrl.value = response.data.url; }; const handleError = (error: any) => { // 上传失败后的处理逻辑 console.log(error); }; return { imageUrl, beforeUpload, handleSuccess, handleError, }; }, }); </script> <style scoped> .avatar-uploader { display: inline-block; width: 100px; height: 100px; border-radius: 50%; overflow: hidden; background: #f8f8f8; position: relative; cursor: pointer; } .avatar { width: 100%; height: 100%; object-fit: cover; } .avatar-uploader-icon { font-size: 28px; color: #8c939d; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> ``` 这是一个名为`AvatarUploader`的Vue组件,使用了`el-upload`组件来实现头像上传功能。在`beforeUpload`方法,你可以添加文件类型和大小的验证逻辑。在`handleSuccess`方法,你可以处理上传成功后的逻辑,例如更新头像的URL。在`handleError`方法,你可以处理上传失败后的逻辑。 你可以将以上代码保存为一个.vue文件,然后在你的Vue项目使用该组件来实现头像上传功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值