news 2026/7/25 8:48:31

Vue的组件通信方式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue的组件通信方式

一. props 父=》子 ;emit 子=》父 通信

//父组件 <template> <div class="demo"> <Child :text="text" @changeText="changeText" />//子组件触发change-text事件,更新text的值 <h1>{{ appText }}</h1>//点击子组件按钮后为hello worldchanged </div> </template> <script lang="ts" setup> import Child from '@/views/childOne .vue' import { ref } from 'vue' const text = ref('hello world') const appText = ref('hello world') // 父组件监听子组件触发的change-text事件,更新text的值 const changeText = (newText: string) => { appText.value += newText } </script> <style scoped></style> //子组件 <template> <div> <h2>{{ props.text }}</h2> <button @click="changeText">change</button> </div> </template> <script lang="ts" setup> // 子组件接收父组件传递的text属性 const props = defineProps({ text: String }); const emit = defineEmits(['changeText']) // 子组件点击按钮触发changeText事件,传递新的文本 const changeText = () => { emit('changeText', ' changed')//子组件触发changeText事件,传递新的文本 } </script> <style scoped></style>

二.expose +ref 导出获取子组件的属性和方法

<template> <div>父组件:拿到子组件的message数据:{{ msg }}</div> <button @click="callChildFn">调用子组件的方法</button> <hr /> <Child ref="com" />//ref绑定子组件 </template> <script setup> import Child from './child.vue'; const com = ref(null); // 通过 模板ref 绑定子组件 const msg = ref(''); onMounted(() => { // 在加载完成后,将子组件的 message 赋值给 msg msg.value = com.value.message; }); function callChildFn() { // 调用子组件的 show方法 com.value.show(); // 重新将 子组件的message 赋值给 msg msg.value = com.value.message; } </script> //子组件 <template> <div> Expose子组件</div> </template> <script setup> const message = ref('子组件传递得信息'); const show = () => { console.log('子组件得方法'); }; //交出子组件函数 defineExpose({ message, show, }); </script>

三.v-model 双向通信

<template> <div> <Child v-model="text" v-model:msg1="message1" v-model:msg2="message2"/> </div> </template> <script setup lang="ts"> import Child from '@/views/childOne .vue'; import { ref } from 'vue'; const text = ref<string>('默认'); const message1 = ref<string>('水果1'); const message2 = ref<string>('水果2'); </script>
//子组件: <template> <div> <div><button @click="changeText">修改text</button> {{ modelValue }}</div> <div><button @click="changeMsg1">修改msg1</button> {{ msg1 }}</div> <div><button @click="changeMsg2">修改msg2</button> {{ msg2 }}</div> </div> </template> <script setup lang="ts"> // 接收 defineProps({ 'modelValue': String, //默认为modelValue msg1: String, msg2: String, }); //默认为modelValue,update:+方法名 const emit = defineEmits(['update:modelValue', 'update:msg1', 'update:msg2']); function changeMsg1() { emit('update:msg1', '蔬菜1'); } function changeMsg2() { emit('update:msg2', '蔬菜2'); } function changeText() { emit('update:modelValue', 'Text2') } </script>

总结:

  1. Props(单向数据流)

    • 细节:父组件通过v-bind(或:) 将数据绑定到子组件的属性上。子组件需要在props选项中声明接收的属性名,并可指定类型 (type)、是否必需 (required)、默认值 (default)、验证函数 (validator)。
  2. Emit (自定义事件)

    • 细节:子组件const emit = defineEmits(['event-name']); emit('event-name', payload)(Composition API +<script setup>) 触发自定义事件。payload是传递给父组件的数据。
    • 父组件处理:父组件在模板中使用v-on(或@) 监听子组件触发的事件,如@event-name="handler"handler是父组件中定义的方法,接收payload作为参数进行处理。
  3. Expose + Ref

    • 细节 (Vue 3):子组件使用defineExpose显式暴露其内部属性或方法(如defineExpose({ publicMethod }))。
    • 父组件操作:父组件通过ref属性 (<ChildComponent ref="childRef" />) 为子组件实例创建引用。在父组件脚本中,const childRef = ref(null); childRef.value?.publicMethod()(Composition API) 访问暴露的内容。
    • 注意:访问需确保子组件已挂载(如在mounted钩子或watchref变化后)。
  4. v-model (双向绑定语法糖)

    • 细节 (Vue 3):v-model默认对应一个名为modelValueprop和一个名为update:modelValue的事件。父组件v-model="parentData",子组件接收modelValueprop,并在需要更新时触发emit('update:modelValue', newValue)
    • 参数:可指定参数实现多个绑定,如v-model:title="pageTitle",对应titlepropupdate:title事件。
    • 本质::modelValue="parentData" @update:modelValue="parentData = $event"的语法糖。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/24 4:49:06

63、工程领域的概率模型与可靠性分析

工程领域的概率模型与可靠性分析 1. 反向传播神经网络模型用于桩沉降预测 1.1 训练与停止准则 反向传播神经网络(BPNN)的训练基于一阶梯度下降来优化连接权重,旨在为高度非线性优化问题找到全局解。该模型能够跳出误差表面的局部极小值,产生最优或接近最优的解。停止准则…

作者头像 李华
网站建设 2026/7/22 4:19:11

5个高效技巧:精通照片元数据管理的终极指南

你是否曾经遇到过这样的情况&#xff1a;成百上千张照片散落在各个文件夹中&#xff0c;拍摄时间错乱&#xff0c;地理位置信息缺失&#xff0c;想要整理却无从下手&#xff1f;&#x1f4f8; 在数字摄影时代&#xff0c;照片元数据管理已成为摄影爱好者和专业用户必须面对的挑…

作者头像 李华
网站建设 2026/7/25 8:34:43

强力指南:5分钟掌握.brd电路板文件查看的完整解决方案

在电子设计领域&#xff0c;面对复杂的.brd电路板文件&#xff0c;工程师常常陷入"有文件却无法查看"的困境。传统专业软件价格昂贵、操作复杂&#xff0c;让许多开发者和学生望而却步。OpenBoardView作为一款完全免费的开源工具&#xff0c;彻底改变了这一现状&…

作者头像 李华
网站建设 2026/7/25 3:15:31

decimal.js 终极指南:彻底解决JavaScript高精度计算难题

decimal.js 终极指南&#xff1a;彻底解决JavaScript高精度计算难题 【免费下载链接】decimal.js An arbitrary-precision Decimal type for JavaScript 项目地址: https://gitcode.com/gh_mirrors/de/decimal.js 为什么你的JavaScript计算总是出错&#xff1f; 在财务…

作者头像 李华
网站建设 2026/7/21 8:25:25

Nugget命令行工具:极简文件下载的终极解决方案

Nugget命令行工具&#xff1a;极简文件下载的终极解决方案 【免费下载链接】nugget minimalist wget clone written in node. HTTP GET files and downloads them into the current directory 项目地址: https://gitcode.com/gh_mirrors/nu/nugget 在当今快节奏的数字时…

作者头像 李华
网站建设 2026/7/25 1:28:23

openpilot自动驾驶系统终极指南:从零开始掌握开源驾驶辅助技术

openpilot自动驾驶系统终极指南&#xff1a;从零开始掌握开源驾驶辅助技术 【免费下载链接】openpilot openpilot 是一个开源的驾驶辅助系统。openpilot 为 250 多种支持的汽车品牌和型号执行自动车道居中和自适应巡航控制功能。 项目地址: https://gitcode.com/GitHub_Trend…

作者头像 李华