当前位置: 首页 > news >正文

Vue 中 props 传递数据的坑

场景:父页面写了一个用户信息的表格,表格中每一行用户数据对应一个重置密码按钮,点击按钮会弹出对话框。重置密码需要传递用户 id 。

代码如下:

<a-table><template #bodyCell="{ column, record }"><template v-else-if="column.key === 'action'"><div class="editable-row-operations"><a-typography-link type="link" @click="handleResetPasswordModal()">重置密码</a-typography-link></div><ResetPasswordModalref="resetPasswordModalRef":id="record.id":onSuccess="async () => {await fetchData()}"/></template></template>
</a-table>

子组件定义了 ref ,并且全部都是 resetPasswordModalRef 的同一个组件实例,由于表格中的每一行数据都是循环出来的,在循环中渲染组件时,所有实例共享同一个 ref,导致总是操作最后一个实例。所以传递 id 的时候,总是会传递循环中的最后一个 id 过去,导致子组件总是接收不到对应的 id。

解决方案:

1. 动态设置 ref

  1. 使用 ref 数组,动态设置 ref,避免所有实例共享一个 ref
<template><a-table :data-source="data"><a-column><template #default="text, record, index">{{ record.id }}<ResetPasswordModal:ref="(el) => setModalRef(el, index)"  <!-- 动态设置ref -->:id="record.id":onSuccess="fetchData"/><a-button @click="handleReset(record, index)">重置密码</a-button></template></a-column></a-table>
</template>
<script setup lang="ts">
import { ref } from 'vue'const modalRefs = ref<any[]>([])  // 存储所有模态框的refconst setModalRef = (el: any, index: number) => {if (el) {modalRefs.value[index] = el}
}const handleReset = (record: any, index: number) => {console.log('操作记录:', record.id)modalRefs.value[index]?.openModal()
}
</script>

2. 单个模态框 + 动态数据(最推荐)

  • 父页面
<template><a-table :data-source="data"><a-column><template #default="text, record">{{ record.id }}<a-button @click="handleReset(record)">重置密码</a-button></template></a-column></a-table><!-- 单个模态框在循环外部 --><ResetPasswordModalref="resetPasswordModalRef":id="currentRecordId":onSuccess="fetchData"/>
</template><script setup lang="ts">
import { ref } from 'vue'const resetPasswordModalRef = ref()
const currentRecordId = ref('')const handleReset = (record: any) => {console.log('设置当前记录ID:', record.id)currentRecordId.value = record.idresetPasswordModalRef.value?.openModal()
}
</script>
  • 子组件

通过 props 接收,略

3. 使用方法传递参数

  • 父页面
<template><a-table :data-source="data"><a-column><template #default="text, record">{{ record.id }}<a-button @click="handleReset(record.id)">重置密码</a-button></template></a-column></a-table><!-- 不传id,完全通过方法参数 --><ResetPasswordModalref="resetPasswordModalRef":onSuccess="fetchData"/>
</template><script setup lang="ts">
import { ref } from 'vue'const resetPasswordModalRef = ref()const handleReset = (id: string) => {console.log('传递ID:', id)resetPasswordModalRef.value?.openModal(id)
}
</script>
  • 子组件

定义一个响应式数据接收 id,略

http://www.dtcms.com/a/465058.html

相关文章:

  • Descheduler for Kubernetes(K8s 重调度器)
  • Embedding(嵌入):让机器理解世界的通用语言
  • sql练习题单-知识点总结
  • 网站空间域名续费湖南送变电建设公司 网站
  • 国产化PDF处理控件Spire.PDF教程:C#中轻松修改 PDF 文档内容
  • 文件预览(pdf、docx、xlsx)
  • AutoCAD如何将指定窗口导出成PDF?
  • 测试DuckDB电子表格读取插件rusty_sheet 0.2版
  • 用「心率」重塑极限,以「中国精度」见证热爱——宜准产品体验官于淼成功挑战北京七环
  • 18003.TwinCat3配置LAN9253从站XML文件(Ethercat)- 示例(一)
  • 解锁特征工程:机器学习的秘密武器
  • 南昌企业网站开发公司hao123网址导航
  • 中山市有什么网站推广长臂挖机出租东莞网站建设
  • 网站建设多少钱一个月青岛网站公司哪家好
  • PowerBI一直在为个人版用户赋能,QuickBI目前正在拥抱个人版用户,FineBI正在抛弃个人版用户
  • 做网站和平台多少钱dedecms 网站地图 插件
  • 在 C# 中显示或隐藏 PDF 图层
  • 货车智能化配置手机控车远程启动一键启动无钥匙进入
  • Unity 项目外部浏览并读取PDF文件在RawImage中显示,使用PDFRender插件
  • 网站规划与建设评分标准昆明的互联网公司有哪些
  • 免费网站登录口看完你会感谢我wordpress能承载多少数据库
  • PostgreSQL选Join策略有啥小九九?Nested Loop/Merge/Hash谁是它的菜?
  • 数据链路层协议之RSTP协议
  • 让AI说“人话“:TypeChat.NET如何用强类型驯服大语言模型的“野性“
  • .pth文件
  • 北京网站建设销售招聘宣传式网站
  • Navicat笔记之使用技巧
  • 第五天:自动化爬虫
  • 长春企业网站哪里做的好12306网站制作
  • Java学习之旅第二季-16:接口