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

Vue中父子组件间的数据传递

子组件数据变化同步父组件

使用自定义事件(简单场景)

子组件:通过 $emit 触发自定义事件

<script>
export default {data() {return {childData: '初始值'}},methods: {updateParent() {// 触发自定义事件并传递数据this.$emit('data-change', this.childData);}},watch: {childData(newVal) {this.updateParent(); // 数据变化时触发}}
}
</script>

父组件:监听自定义事件

<template><child-component @data-change="handleDataChange"/>
</template><script>
export default {methods: {handleDataChange(newValue) {console.log('收到子组件数据:', newValue);this.parentData = newValue; // 更新父组件数据}}
}
</script>

使用 Vuex/Pinia 状态管理(复杂场景)

// store.js (以vuex为例)
export default new Vuex.Store({state: {sharedData: null},mutations: {updateData(state, payload) {state.sharedData = payload}}
})

子组件

<script>
export default {
..........................methods: {updateStore() {this.$store.commit('updateData', this.childData)}}
}
</script>

父组件

<template><div>{{ sharedData }}</div>
</template><script>
import { mapState } from 'vuex'export default {computed: {...mapState(['sharedData'])}
}
</script>

父组件数据变化同步子组件

Props 传递

父组件通过 props 向子组件传递数据,父数据变化时子组件会自动更新。

注意:子组件不要直接修改props传递的值,通过$emit通知父组件修改。

<!-- 父组件 -->
<template><Child :message="parentMsg" />
</template><script>
export default {data() {return { parentMsg: "初始值" };},mounted() {setTimeout(() => {this.parentMsg = "父组件修改后的值"; // 修改后子组件自动更新}, 2000);}
};
</script><!-- 子组件 -->
<template><div>{{ message }}</div> <!-- 显示父组件传递的值 -->
</template><script>
export default {props: ['message'] // 声明接收父组件数据
};
</script>

Vuex/Pinia 状态管理

全局状态管理,父子组件共享同一状态。

// store.js (以 Pinia 为例)
export const useStore = defineStore('main', {state: () => ({ count: 0 }),actions: {updateCount(val) { this.count = val; }}
});
<!-- 父/子组件共用方式 -->
<script setup>
import { useStore } from './store';
const store = useStore();
</script><template><div>{{ store.count }}</div><button @click="store.updateCount(10)">修改状态</button>
</template>

直接修改子组件数据

通过 ref 直接操作子组件(破坏数据流,仅应急使用)

<template><ChildComponent ref="child" /><button @click="forceUpdate">强制修改</button>
</template><script>
export default {methods: {forceUpdate() {this.$refs.child.internalData = "新值"; // 直接修改子组件数据}}
};
</script>

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

相关文章:

  • oc-mirror plugin v2 错误could not establish the destination for the release i
  • 什么是STLC(软件测试生命周期)?
  • 招标网站用户规模评测:基于第三方流量数据的 10 大平台对比分析​
  • [Git] 如何拉取 GitHub 仓库的特定子目录
  • 05高级语言逻辑结构到汇编语言之逻辑结构转换 while (...) {...} 结构
  • GaussDB 并发自治事务数达到最大值处理案例
  • consul-基础概念
  • Leetcode 343. 整数拆分 动态规划
  • 【教程】在 VMware Windows 虚拟机中使用 WinPE 进行离线密码重置或取证操作
  • 通信急先锋,稳联技术Profinet与EtherCAT锂电行业应用案例
  • 2025年5月架构设计师综合知识真题回顾,附参考答案、解析及所涉知识点(六)
  • AMPAK正基科技系列产品有哪些广泛应用于IOT物联网
  • Git的初步学习
  • GStreamer无线图传:树莓派到计算机的WiFi图传方案
  • 反向代理实现服务器联网
  • RNN(循环神经网络)和Transformer是处理自然语言处理(NLP)任务区别
  • 【深度学习新浪潮】如何利用多模态大模型优化结构力学性能?
  • 「内力探查术」:用 Instruments 勘破 SwiftUI 卡顿迷局
  • 开源 C++ QT Widget 开发(一)工程文件结构
  • linux系统装google chrome,amd64
  • Qt——文件操作
  • Vercel v0 iOS版重磅发布:AI驱动的移动开发新篇章
  • SWMM排水管网水力、水质建模及在海绵与水环境中的应用
  • 纯Qt结合ffmpeg实现本地摄像头采集/桌面采集/应用程序窗口采集/指定采集帧率和分辨率等
  • 数据仓库OLTPOLAP维度讲解
  • Qt事件处理机制详解
  • [激光原理与应用-308]:光学设计 - 266皮秒深紫外激光器设计图纸示例解析(基于工程实现与专利技术)
  • 《C++进阶之STL》【二叉搜索树】
  • 11.Ansible自动化之-内容集管理
  • 云原生俱乐部-shell知识点归纳(1)