Vue.js 中使用 @click 传递参数到方法
在你的代码中,你使用了 @click 来绑定点击事件,但是你没有在方法名后面加上括号来传递参数。你需要修改你的代码,将 @click 绑定的方法改为以下形式:
@click="getDialogData(product.name)"
这样,当你点击 td 时,它将调用 getDialogData 方法,并将 product.name 作为参数传递给它。
以下是一些需要注意的细节:
- 确保你的
getDialogData方法已经定义在你的 Vue 组件中。 - 在
@click绑定的方法中,你需要将参数放在括号中,就像你在调用函数时一样。 - 在传递参数时,你需要使用单引号将参数值包裹起来。
修改后的代码示例:
<div class='Products'>
<table>
<tr th:each='product, productStat : ${indexProducts}' th:if='${productStat.index % 3 == 0}'>
<td th:data-product-name='${product.name}' @click='getDialogData($event.target.getAttribute('data-product-name'))'>
<el-image class='ProductImage' th:src='${product.imgUrl}' :fit=''cover'></el-image>
<span class='ProductName' th:text='${product.name}'>product-name</span>
</td>
<td @click='getDialogData(${indexProducts[productStat.index + 1].name})'>
<el-image class='ProductImage' th:src='${product.imgUrl}' :fit='cover'></el-image>
<span class='ProductName' th:text='${indexProducts[productStat.index + 1].name}'>product-name</span>
</td>
<td @click='getDialogData(${indexProducts[productStat.index + 2].name})'>
<el-image class='ProductImage' th:src='${product.imgUrl}' :fit='cover'></el-image>
<span class='ProductName' th:text='${indexProducts[productStat.index + 2].name}'>product-name</span>
</td>
</tr>
</table>
<el-button type='primary' @click='dialogVisible = true'>Click</el-button>
<el-dialog title='Tips' v-model='dialogVisible'>
<div class='dialog'>
<div class='dialogLeft'>
<el-image class='dialogImage' src='' :fit='cover'></el-image>
</div>
<div class='dialogRight'></div>
</div>
</el-dialog>
</div>
通过这种方式,你就可以在点击 td 时,将 product.name 参数传递给 getDialogData 方法,并实现你的功能。
原文地址: https://www.cveoy.top/t/topic/qAks 著作权归作者所有。请勿转载和采集!