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"];

相关推荐
网络点点滴4 小时前
watch监视-ref基本类型数据
前端·javascript·vue.js
西洼工作室4 小时前
前端接口安全与性能优化实战
前端·vue.js·安全·axios
大布布将军4 小时前
《前端九阴真经》
前端·javascript·经验分享·程序人生·前端框架·1024程序员节
幸运小圣4 小时前
for...of vs for 循环全面对比【前端JS】
开发语言·前端·javascript
用户9545156811625 小时前
实际开发中 | 与 || 的使用方法及组件封装方案解析
前端
得帆云低代码5 小时前
COC Asia 2025|得帆云 ETL:顺应 Hive 新特性,重塑数据管道的未来
前端
十字路口的火丁5 小时前
前端开发如何灵活使用 css 变量
前端
_志哥_5 小时前
深度解析:解决 backdrop-filter 与 border-radius 的圆角漏光问题
前端·javascript·html
南囝coding5 小时前
100% 用 AI 做完一个新项目,从 Plan 到 Finished 我学到了这些
前端·后端
qiao若huan喜5 小时前
10、webgl 基本概念 + 坐标系统 + 立方体
前端·javascript·信息可视化·webgl