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

相关推荐
Nanachi8 小时前
跨框架的前端源码定位,再加上点LLM
前端
人无远虑必有近忧!8 小时前
fetch请求图片报跨域
前端·javascript
谢院柯8 小时前
解决修改 node_modules 依赖库源码后重复安装问题的几种方案
前端
疯狂打码的少年8 小时前
【程序语言与编译】NFA转DFA(子集构造法)
前端·笔记
2401_868534788 小时前
5G和4G接入网对比介绍
vue.js
半只小闲鱼8 小时前
合并多个excel文件到一个文件中
前端·python·数据分析
fobwebs8 小时前
Chrome谷歌浏览器多开教程,如何在电脑上同时登录多个GMAIL账号
前端·chrome·多开·同时登录多个gmail
前端 贾公子9 小时前
小程序蓝牙打印探索与实践 (最终章)
前端·微信小程序·小程序
chushiyunen9 小时前
vue export default
前端·javascript·vue.js
右耳朵猫AI9 小时前
前端周刊2026W23 | React 19.2.7、Conductor重写提速、Lovable切换TanStack Start
前端·react.js·前端框架