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

相关推荐
董员外41 分钟前
LangChain.js 快速上手指南:模型接入、流式输出打造基础
前端·javascript·后端
AomanHao1 小时前
基于高德地图JS的旅游足迹,可嵌入个人博客中
前端
用户4099322502121 小时前
Vue3组件开发中如何兼顾复用性、可维护性与性能优化?
前端·vue.js·trae
千寻girling1 小时前
面试官 : “ 请问你实际开发中用过 函数柯理化 吗? 能讲一下吗 ?”
前端·javascript·面试
golang学习记1 小时前
Claude Opus 4.6 正式发布:Agent 时代的编程王者与长上下文革命
前端·人工智能·后端
双向331 小时前
RAG实战解密:三步构建你的智能文档问答系统(附开源方案)
前端
DEMO派2 小时前
前端CSRF攻击代码演示及防御方案解析
前端·csrf
change_fate2 小时前
vite 修改base之后需要修改public路径
javascript·vue.js
REDcker2 小时前
Media Source Extensions (MSE) 详解
前端·网络·chrome·浏览器·web·js
阿珊和她的猫2 小时前
Chrome 的 SameSite 属性:原理与解决方案
前端·chrome