Vue.js 表格数据修改操作:处理 row 未定义错误
在使用 Vue.js 开发表格组件时,你可能会遇到 `Property or method "row" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.` 这种错误,通常是因为你尝试在模板中使用 `row` 变量,但它在 Vue 实例中没有定义或没有被声明为响应式属性。
为了解决这个问题,你需要在你的 Vue 组件的 `data` 选项中定义 `row` 变量,并确保它是一个响应式属性。以下是示例代码:
// 完整代码示例
<template>
<div>
<el-table
ref="multipleTable"
:data="tableData"
@selection-change="handleSelectionChange"
border
style="width: 100%; margin-top: 20px"
>
<el-table-column type="selection" width="55" align="center" />
</el-table>
<div style="margin-top: 20px">
<el-button class="btnClass" size="mini" @click="closeView">返回</el-button>
<el-button class="btnClass" size="mini">导入</el-button>
<el-button class="btnClass" size="mini">导出</el-button>
<el-button class="btnClass" size="mini" @click="handleUpdate(row)">修改</el-button>
<el-input
size="mini"
v-model="params.searchValue"
@blur="handleBlur"
style="width: 276px; margin-right: 10px"
></el-input>
<el-button size="mini" class="btnClass">查询</el-button>
</div>
</div>
</template>
<script>
export default {
data() {
return {
row: null, // 定义 row 变量
tableData: [],
selection: [],
ids: [],
single: false,
multiple: false,
params: {
companyId: '',
reportingPeriodDate: [],
incomeReportingCode: '',
searchValue: '',
},
form: {},
open: false,
title: '',
};
},
methods: {
handleUpdate(row) {
this.reset();
const reportingPeriodDate = this.selection.map((item) => item.reportingPeriodDate);
console.log(row, 'rowwww');
this.params.companyId = this.companyId;
editReportingDonationDeduction(this.params, reportingPeriodDate).then((response) => {
console.log(response, '修改综合所得申报');
this.form = response.data;
console.log(this.form, '修改综合');
this.open = true;
this.title = '修改综合所得申报-稿酬所得';
});
},
handleSelectionChange(selection) {
this.selection = selection;
console.log(selection, 'selectionsss');
this.ids = selection.map((item) => item.personId);
this.single = selection.length !== 1;
this.multiple = !selection.length;
},
closeView() {
// ...
},
},
};
</script>
在这个例子中,我们定义了 `row` 变量并将其初始化为 `null`。现在,你可以安全地在你的模板中访问 `row` 变量,并在你的 `handleUpdate` 方法中使用它来访问被选中的行数据。
需要注意的是,`row` 变量应该是一个响应式属性,以便 Vue.js 能够跟踪它的变化并更新视图。如果 `row` 变量不是响应式的,那么即使它被修改了,视图也不会更新。
通过定义 `row` 变量并将其声明为响应式属性,你就可以解决 `row` 未定义的问题,并确保你的代码能够正常工作。
原文地址: https://www.cveoy.top/t/topic/pgqj 著作权归作者所有。请勿转载和采集!