【debug】ElementPlus table组件column传入formatter类型报错

报错如下:

不能将类型"((row: ShortUserPayType) => string) | ((row: ShortUserPayType) => number) | undefined"分配给类型"((row: any, column: TableColumnCtx<any>, cellValue: any, index: number) => string | VNode<RendererNode, RendererElement, { ...; }>) | undefined"。

组件要求传入的formatter函数必须定义为:

javascript 复制代码
((row: any, column: TableColumnCtx`<any>`, cellValue: any, index: number) => string | VNode<RendererNode, RendererElement, { ...; }>) | undefined"

但我不需要这么多属性,我只需要row属性。

解决方法,自定义一个Column类型:

javascript 复制代码
export interface Column {
  key: string
  title: string
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  formatter?: (...args: any) => any
}

这使得formatter 函数可以接受 ElementPlus 所期望的四个参数,也可以只接受一个参数,或者根本不接受参数。

javascript 复制代码
const columns: Column[] = [...]
html 复制代码
<el-table-column
  v-for="column in columns"
  :key="column.key"
  :prop="column.key"
  :label="column.title"
  :formatter="column.formatter"
/>
相关推荐
IT_陈寒17 小时前
SpringBoot 3.2新特性实战:这5个隐藏技巧让你的应用性能飙升50%
前端·人工智能·后端
flashlight_hi17 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表
Java追光着18 小时前
React Native 自建 JS Bundle OTA 更新系统:从零到一的完整实现与踩坑记录
javascript·react native·react.js
努力往上爬de蜗牛18 小时前
react native 运行问题和调试 --持续更新
javascript·react native·react.js
todoitbo18 小时前
Rust新手第一课:Mac环境搭建踩坑记录
开发语言·macos·rust
laplace012318 小时前
PyQt5 + Qt Designer配置指令
开发语言·qt
eason_fan18 小时前
Monorepo性能噩梦:一行配置解决VSCode卡顿与TS类型崩溃
前端·typescript·visual studio code
nvd1118 小时前
Python 迭代器 (Iterator) vs. 生成器 (Generator)
开发语言·python
HalvmånEver18 小时前
Linux:基础开发工具(三)
linux·运维·服务器·开发语言·学习·gcc/g++