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

相关推荐
祈澈菇凉19 分钟前
如何结合使用thread-loader和cache-loader以获得最佳效果?
前端
垣宇22 分钟前
Vite 和 Webpack 的区别和选择
前端·webpack·node.js
java1234_小锋26 分钟前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
化作繁星29 分钟前
如何在 React 中测试高阶组件?
前端·javascript·react.js
初遇你时动了情32 分钟前
react module.scss 避免全局冲突类似vue中scoped
vue.js·react.js·scss
Au_ust36 分钟前
千峰React:函数组件使用(2)
前端·javascript·react.js
爱吃南瓜的北瓜1 小时前
npm install 卡在“sill idealTree buildDeps“
前端·npm·node.js
TTc_1 小时前
记录首次安装远古时代所需的运行环境成功npm install --save-dev node-sass
前端·npm·sass
翻滚吧键盘1 小时前
npm使用了代理,但是代理软件已经关闭导致创建失败
前端·npm·node.js
烂蜻蜓1 小时前
Uniapp 设计思路全分享
前端·css·vue.js·uni-app·html