"在你的回答中:\n您可以尝试使用v-bind指令将data-product-name属性绑定到td元素上,并在click事件处理程序中传递该属性的值作为参数。以下是修改后的代码示例:\n\nhtml\n<div class="Products">\n <table>\n <tr th:each="product, productStat : ${indexProducts}" th:if="${productStat.index % 3 == 0}">\n <td :data-product-name="product.name" @click="getDialogData($event.target.getAttribute('data-product-name'))">\n <el-image class="ProductImage" th:src="${product.imgUrl}" :fit="'cover'"></el-image>\n <span class="ProductName" th:text="${product.name}">product-name</span>\n </td>\n <td :data-product-name="indexProducts[productStat.index + 1].name" @click="getDialogData($event.target.getAttribute('data-product-name'))">\n <el-image class="ProductImage" th:src="${product.imgUrl}" :fit="cover"></el-image>\n <span class="ProductName" th:text="${indexProducts[productStat.index + 1].name}">product-name</span>\n </td>\n <td :data-product-name="indexProducts[productStat.index + 2].name" @click="getDialogData($event.target.getAttribute('data-product-name'))">\n <el-image class="ProductImage" th:src="${product.imgUrl}" :fit="cover"></el-image>\n <span class="ProductName" th:text="${indexProducts[productStat.index + 2].name}">product-name</span>\n </td>\n </tr>\n </table>\n <el-button type="primary" @click="dialogVisible = true">Click</el-button>\n <el-dialog title="Tips" v-model="dialogVisible">\n <div class="dialog">\n <div class="dialogLeft">\n <el-image class="dialogImage" src="" :fit="cover"></el-image>\n </div>\n <div class="dialogRight"></div>\n </div>\n </el-dialog>\n</div>\n\n\n通过将data-product-name属性绑定到td元素上,您可以在click事件处理程序中使用$event.target.getAttribute('data-product-name')获取该属性的值并传递给getDialogData方法。这样,您就可以将参数正确传递给方法了。\n\n我这么做了,但并没有效果内容:很抱歉,我的回答可能有误导性。在Vue.js中,使用v-bind指令无法直接绑定自定义的HTML属性。相反,您可以使用v-bind指令绑定Vue实例中的属性,并在click事件处理程序中访问该属性。以下是修改后的代码示例:\n\nhtml\n<div class="Products">\n <table>\n <tr v-for="(product, index) in indexProducts" :key="index" v-if="index % 3 === 0">\n <td @click="getDialogData(product.name)">\n <el-image class="ProductImage" :src="product.imgUrl" :fit="'cover'"></el-image>\n <span class="ProductName">{{ product.name }}</span>\n </td>\n <td @click="getDialogData(indexProducts[index + 1].name)">\n <el-image class="ProductImage" :src="indexProducts[index + 1].imgUrl" :fit="cover"></el-image>\n <span class="ProductName">{{ indexProducts[index + 1].name }}</span>\n </td>\n <td @click="getDialogData(indexProducts[index + 2].name)">\n <el-image class="ProductImage" :src="indexProducts[index + 2].imgUrl" :fit="cover"></el-image>\n <span class="ProductName">{{ indexProducts[index + 2].name }}</span>\n </td>\n </tr>\n </table>\n <el-button type="primary" @click="dialogVisible = true">Click</el-button>\n <el-dialog title="Tips" v-model="dialogVisible">\n <div class="dialog">\n <div class="dialogLeft">\n <el-image class="dialogImage" :src="selectedProduct.imgUrl" :fit="cover"></el-image>\n </div>\n <div class="dialogRight"></div>\n </div>\n </el-dialog>\n</div>\n\n\n在这个修改后的代码示例中,我直接将product.name传递给了getDialogData方法,并且在对话框中显示了选中的产品的图像。请确保在Vue实例中定义了selectedProduct属性,并在getDialogData方法中更新selectedProduct属性以显示正确的图像。

Vue.js 中使用 v-bind 指令传递参数的正确方法

原文地址: https://www.cveoy.top/t/topic/qAjZ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录