使用ANT DESIGN VUE组件库中的table时,使用文档中的行点击方法,获取到点击行的信息,并且赋值给其他字段报错TypeError: Cannot set properties of undefined (setting ‘name’)


<a-table
:customRow="customRow"
:columns="columns"
:data-source="data"
>
</a-table>
data(){
return{
data:[],
columns:[],
name:'' //需要赋值的已经定义
}}
methods:{
customRow(record, index) {
return {
on: {
click() {
console.log(record) //打印成功
this.name = record.name //赋值报错
}
}
}
}
}
解决办法:var that=this
customRow(record, index) {
var that = this
return {
on: {
click() {
console.log(record)
that.time = record.time
}
}
}
},

本文档介绍了在使用Ant Design Vue的Table组件时遇到的问题,即在尝试通过行点击事件获取数据并赋值给其他字段时出现TypeError。解决方案是通过在方法中保存上下文`this`,然后在事件处理函数内部引用保存的`that`来正确地设置属性。
1万+

被折叠的 条评论
为什么被折叠?



