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

Vue组件通信讲解[父子组件通信]

Vue组件通信讲解

在Vue中,父子组件之间的通信可以通过props和emit来实现。props用于从父组件向子组件传递数据,而$emit用于从子组件向父组件触发事件。

以下是一个包含子传父和父传子通信的Vue案例解决方案:

父组件:Parent.vue

<template><div><h2>父组件</h2><p>子组件传递的数据:{{ messageFromChild }}</p><Child :message="messageFromParent" @childEvent="handleChildEvent" /></div>
</template><script>
import Child from "./Child.vue";export default {data() {return {messageFromParent: "Hello from parent",messageFromChild: ""};},components: {Child},methods: {handleChildEvent(message) {this.messageFromChild = message;}}
};
</script>

子组件:Child.vue

<template><div><h3>子组件</h3><p>父组件传递的数据:{{ message }}</p><button @click="sendMessageToParent">向父组件发送消息</button></div>
</template><script>
export default {props: {message: String},methods: {sendMessageToParent() {this.$emit("childEvent", "Hello from child");}}
};
</script>

在上述示例中,父组件(Parent.vue)通过将messageFromParent作为props传递给子组件(Child.vue),同时监听子组件的childEvent事件。当子组件触发childEvent事件时,父组件的handleChildEvent方法会被调用,并将子组件传递的消息更新到messageFromChild属性上。这样就实现了子传父的通信。

另外,子组件中的按钮点击事件sendMessageToParent通过this.$emit方法向父组件触发childEvent事件,并将消息作为参数传递给父组件,实现了父传子的通信。

实现Vue中的父子组件通信时,除了使用props和$emit方法,还有其他一些方法可以实现更复杂的场景。

1. 使用$refs:可以通过在父组件中使用ref属性来获取子组件的引用,并直接访问子组件的属性和方法。这种方法适用于父组件需要直接操作子组件的情况。

父组件:Parent.vue

<template><div><h2>父组件</h2><button @click="callChildMethod">调用子组件方法</button><Child ref="childComponent" /></div>
</template><script>
import Child from "./Child.vue";export default {components: {Child},methods: {callChildMethod() {this.$refs.childComponent.childMethod();}}
};
</script>

子组件:Child.vue

<template><div><h3>子组件</h3></div>
</template><script>
export default {methods: {childMethod() {console.log("子组件方法被调用");}}
};
</script>

在上述示例中,父组件通过ref属性给子组件命名为"childComponent",然后可以使用this.$refs.childComponent来访问子组件的属性和方法。父组件中的callChildMethod方法调用了子组件的childMethod方法。

2. 使用事件总线:可以创建一个用于中央事件处理的事件总线实例,让父组件和子组件通过事件触发和监听进行通信。这种方法适用于非父子关系组件之间的通信。

事件总线:eventBus.js

import Vue from "vue";
export const eventBus = new Vue();

父组件:Parent.vue

<template><div><h2>父组件</h2><button @click="sendMessageToChild">向子组件发送消息</button></div>
</template><script>
import { eventBus } from "./eventBus";export default {methods: {sendMessageToChild() {eventBus.$emit("messageToChild", "Hello from parent");}}
};
</script>

子组件:Child.vue

<template><div><h3>子组件</h3></div>
</template><script>
import { eventBus } from "./eventBus";export default {created() {eventBus.$on("messageToChild", message => {console.log("子组件收到消息:" + message);});}
};
</script>

在上述示例中,通过创建一个名为eventBus的事件总线实例,父组件可以通过eventBus.$emit方法触发名为"messageToChild"的事件并传递消息。子组件通过eventBus.$on方法监听"messageToChild"事件,并在事件触发时执行相应的回调函数。

Vue的组件通信有子传父和父传子,子传入父用emit发送回调函数,父传子直接在props中传入参数即可.

http://www.lryc.cn/news/293822.html

相关文章:

  • Qt应用开发(安卓篇)——调用ioctl、socket等C函数
  • centos 安装docker CE
  • 某赛通电子文档安全管理系统 UploadFileList 任意文件读取漏洞复现
  • Kafka运维相关知识
  • 鸿蒙Native项目生产动态库(.so) 和静态库(.a)
  • B站课程评分
  • 【C++】拷贝构造函数和赋值运算符重载详解
  • BUUCTF-Real-[ThinkPHP]5-Rce
  • 物联网中基于WIFI的室内温度检测系统设计
  • 驱动开发-系统移植
  • MySQL数据存储
  • 带着问题读源码——Spring MVC是怎么找到接口实现类的?
  • [NAND Flash 7.1] 闪存系统性能优化方向集锦?AC timing? Cache? 多路并发?
  • 【数据结构】分治策略
  • 【PLC一体机】PLC一体机中如何实现触摸屏和PC电脑的通讯
  • 如何保证订单异步回调的幂等性
  • Linux下vim命令详解
  • 机器学习6-逻辑回归
  • 关于Clone
  • 【C++入门学习指南】:函数重载提升代码清晰度与灵活性
  • MySql主从同步,同步SQL_ERROR 1032解决办法
  • Webpack的性能优化
  • PyTorch中tensor.backward()函数的详细介绍
  • Linux 驱动开发基础知识——内核对设备树的处理与使用(十)
  • 编程笔记 html5cssjs 077 Javascript 关键字
  • LeetCode_19_中等_删除链表的倒数第N个结点
  • C++泛编程(3)
  • python基于django的公交线路查询系统mf383
  • ElementUI 组件:Container 布局容器实例
  • 【数据结构 09】哈希