【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"
/>
相关推荐
weixin_4482902535 分钟前
书庐开发实战教学
javascript·css·html
默_笙41 分钟前
🛴 从散件到整机:DeepAgents 与 Agent 身上预留的那些"插槽"(前置介绍)
前端·javascript
zhangzeyuaaa1 小时前
深入理解 Ruby 运算符:本质、分类、坑点与重载实战
开发语言·前端·ruby
一木 之林1 小时前
DeepSeek Agent 开发(一)
开发语言·前端·javascript
朔北之忘 Clancy1 小时前
青少年软编等考七级题解目录
开发语言·c++·青少年编程·gesp·csp·信奥赛·noi
ss2731 小时前
AI全栈实战 | 3.2-01 Python 基础:四大数据容器怎么选,推导式为什么是 Pythonic 的灵魂
开发语言·人工智能·python
Mr_Macallon1 小时前
C++回顾(基础)(01)
c语言·开发语言·c++·学习
未来之窗软件服务2 小时前
Go 利器 GoReSym:剥离符号恢复 Go 二进制元数据—东方仙盟
开发语言·golang·仙盟创梦ide·东方仙盟
树下水月2 小时前
Typora破解
linux·服务器·前端
朝朝辞暮i2 小时前
C++ 第 32 课:main()、rclcpp::init()、spin()、shutdown()
开发语言·c++·算法