ElementPlus里的类型别名声明及使用

前言

最近刚开始使用ts,定义的变量总是不知道类型,特别是第三方库中,更是不知道有哪些类型,笨人本办法,遇到一个就记录一下,方便我下次使用查询

组件实例

我们通过组件的ref属性获取组件实例时,定义的实例变量需要指定类型。下面是常见的一些组件实例类型

el-scrollbar

vue 复制代码
<template>
  <el-scrollbar ref="scrollbarRef"></el-scrollbar>
</template>

<script lang="ts" setup>
  import type { ElScrollbar } from 'element-plus';
  const scrollbarRef = ref<InstanceType<typeof ElScrollbar>>();
  scrollbarRef.value!.setScrollTop(value)
</script>

el-form

vue 复制代码
<template>
  <el-form ref="ruleFormRef"></el-form>
</template>

<script lang="ts" setup>
  import type { ElForm } from 'element-plus';
  // 可以单独定义一个类型
  type FormInstance = InstanceType<typeof ElForm>;
  const ruleFormRef = ref<FormInstance>();
  ruleFormRef.value!.resetFields();
</script>

el-table

vue 复制代码
<template>
  <el-table ref="multipleTable"></el-table>
</template>

<script lang="ts" setup>
  import type { ElTable } from "element-plus";
  const multipleTable = ref<InstanceType<typeof ElTable>>();
  multipleTable.value!.clearSelection();
</script>

el-input

vue 复制代码
<template>
  <el-input ref="multipleTable" v-model="value"></el-table>
</template>

<script lang="ts" setup>
  import type { ElInput } from "element-plus";
  const multipleTable = ref<InstanceType<typeof ElInput>>();
  multipleTable.value!.focus()
</script>

Props 属性类型

当我们要动态设置某些组件的props属性时,有些属性也是有类型的,如果定义不对,也是会有ts类型错误提示的。

组件的 size 属性

ComponentSize

export declare const componentSizes: readonly ["", "default", "small", "large"];

vue 复制代码
// template
<el-table ref="multipleTable" :size="tableSize"></el-table>

// ts
import type { ComponentSize } from 'element-plus';
const tableSize = ref<ComponentSize>('default');

时间组件的类型

DatePickType

export declare const datePickTypes: readonly ["year", "month", "date", "dates", "week", "datetime", "datetimerange", "daterange", "monthrange"];

相关推荐
恋猫de小郭7 分钟前
Flutter 设计包解耦新进展,material_ui 和 cupertino_ui 发布预告
android·前端·flutter
linux_cfan28 分钟前
[2026深度评测] 打造“抖音级”丝滑体验:Web直播播放器选型与低延迟实践
前端·javascript·html5
天天向上的鹿茸43 分钟前
前端适配方案
前端·javascript
We་ct1 小时前
LeetCode 226. 翻转二叉树:两种解法(递归+迭代)详解
前端·算法·leetcode·链表·typescript
叫我一声阿雷吧1 小时前
JS实现无限滚动加载列表|适配多端+性能优化【附完整可复用源码】
开发语言·javascript·性能优化
哆啦A梦15881 小时前
Vue3魔法手册 作者 张天禹 013_pinia
前端·vue.js·typescript
哆啦A梦15881 小时前
Vue3魔法手册 作者 张天禹 014_组件通信
前端·vue.js·typescript
木斯佳1 小时前
前端八股文面经大全:有赞前端一面二面HR面(2026-1-13)·面经深度解析
前端·状态模式
VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue养老院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
码云数智-园园2 小时前
Vue 3 + TypeScript 企业级项目架构实战:从0到1打造可维护的前端工程体系
前端·vue.js·typescript