【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"
/>
相关推荐
zh_xuan12 分钟前
kotlin的常见空检查
android·开发语言·kotlin
Justin3go8 小时前
HUNT0 上线了——尽早发布,尽早发现
前端·后端·程序员
Tony Bai8 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
怕浪猫8 小时前
第一章 JSX 增强特性与函数组件入门
前端·javascript·react.js
铅笔侠_小龙虾9 小时前
Emmet 常用用法指南
前端·vue
wjs20249 小时前
Swift 类型转换
开发语言
钦拆大仁9 小时前
跨站脚本攻击XSS
前端·xss
前端小L9 小时前
贪心算法专题(十):维度权衡的艺术——「根据身高重建队列」
javascript·算法·贪心算法
秃了也弱了。9 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
weixin_440730509 小时前
java数组整理笔记
java·开发语言·笔记