VUE之组件通信(一)

embedded/2025/2/1 22:43:09/

1、props

概述:props是使用频率最高的一种通信方式,常用与:父<——>子。

  • 若 父传子:属性值是非函数。
  • 若 子传父:属性值是函数。

父组件:

javascript"><template><div class="father"><h3>父组件</h3><h4>汽车:{{car}}</h4><h4 v-show="toy">子给的玩具:{{toy}} </h4><Child :car="car" :sendToy="getToy" /><div>
</template><script setup lang="ts" name="Father">import Child from './Child.vue'import {ref} from 'vue'//数据let car = ref('奔驰')let toy = ref('')// 方法function getToy(value:string){toy.value = value}
</script>
javascript"><template><div class="child"><h3>子组件</h3><h4>玩具:{{toy}}</h4><h4>父给的车:{{car}} </h4><button @click="sendToy(toy)">把玩具给父亲</button><div>
</template><script setup lang="ts" name="Child">import {ref} from 'vue'//数据let toy = ref('奥特曼')// 声明接收propsdefineProps(['car','sendToy'])
</script>

2、自定义事件

javascript"><template><div class="child"><h3>子组件</h3><h4>玩具:{{toy}}</h4><button @click="emit('send-toy',toy)">测试</button><div>
</template><script setup lang="ts" name="Child">import {ref} from 'vue'let toy = ref('奥特曼')//声明事件const emit = defineEmits(['send-toy'])
</script>

 

javascript"><template><div class="father"><h3>父组件</h3><h4 v-show="toy">子给的玩具:{{toy}}</h4><Child @send-toy='saveToy'/><div>
</template><script setup lang="ts" name="Father">import Child from './Child.vue'import {ref} from 'vue';let toy = ref('')function saveToy(value:string){console.log('xyz',value)toy.value = value}
</script>

3、mitt

安装mitt 

npm i mitt
>> utils/emitter.ts
// 引入mitt
import mitt from 'mitt'
// 调用mitt得到emitter.能绑定事件,触发事件
const emitter = mitt()
// 绑定时间
emitter.on('test1',()=>{console.log('test1被调用了')
})
emitter.on('test2',()=>{console.log('test2被调用了')
})// 触发事件
setImterval(() => {emitter.emit('test1')emitter.emit('test2')
},2000);setTimeout(()=>{//emitter.off('test1')//emitter.off('test2')emitter.all.clear()
},3000);//暴露
export fefault emitter >> main.tsimport emitter from '@/utils/emitter'
>> Child1.vue
<template><div class="child1"><h3>子组件1</h3><h4>玩具:{{toy}}</h4><button @click="emitter.emit('send-toy',toy)">玩具给弟弟</button></div>
</template><script setup lang="ts" name="Child1">import {ref} from 'vue'let toy = ref('奥特曼')</script>>> Child2.vue<template><div class="child2"><h3>子组件2</h3><h4>电脑:{{computer}}</h4><h4>哥哥给的玩具:{{toy}}</h4></div>
</template><script setup lang="ts" name="Child2">import {ref,onUnmounted} from 'vue'import emitter from '@/utils/emitter';let computer= ref('联想')let toy = ref('')// 给emitter绑定send-toy事件emitter.on('send-toy',(value:string)=>{toy.value = value})
//在组件卸载时解绑send-toy事件
onUnmounted(()=>{emitter.off('send-toy')
})</script>

http://www.ppmy.cn/embedded/158746.html

相关文章

Linux《基础指令》

在之前的Linux《Linux简介与环境的搭建》当中我们已经初步了解了Linux的由来和如何搭建Linux环境&#xff0c;那么接下来在本篇当中我们就要来学习Linux的基础指令。在此我们的学习是包括两个部分&#xff0c;即指令和关于Linux的基础知识&#xff1b;因此本篇指令和基础知识的…

如何跨互联网adb连接到远程手机-蓝牙电话集中维护

如何跨互联网adb连接到远程手机-蓝牙电话集中维护 --ADB连接专题 一、前言 随便找一个手机&#xff0c;安装一个App并简单设置一下&#xff0c;就可以跨互联网的ADB连接到这个手机&#xff0c;从而远程操控这个手机做各种操作。你敢相信吗&#xff1f;而这正是本篇想要描述的…

吴恩达深度学习——深层神经网络

来自https://www.bilibili.com/video/BV1FT4y1E74V&#xff0c;仅为本人学习所用。 符号约定 对于该深层网络&#xff0c;有四层&#xff0c;包含三个隐藏层和一个输出层。 隐藏层中&#xff0c;第一层有五个单元、第二层有五个单元&#xff0c;第三层有三个单元。标记 l l l…

品牌RWA化构建指南:资产数字化与价值共创

随着区块链技术的飞速发展&#xff0c;RWA&#xff08;Real World Asset&#xff0c;现实世界资产&#xff09;化已成为品牌融资和价值增值的新趋势。通过将品牌的核心资产、无形资产及未来收益等进行代币化&#xff0c;品牌不仅能拓宽融资渠道&#xff0c;还能赋能投资者、消费…

性能优化案例:通过合理设置spark.shuffle.memoryFraction参数的值来优化PySpark程序的性能

在PySpark中&#xff0c;合理调整spark.shuffle.memoryFraction参数可以有效优化Shuffle阶段的性能&#xff0c;尤其是在存在大量磁盘溢出的场景下。 通过合理设置spark.shuffle.memoryFraction并结合其他优化手段&#xff0c;可显著减少Shuffle阶段的磁盘I/O&#xff0c;提升P…

多级缓存(亿级并发解决方案)

多级缓存&#xff08;亿级流量&#xff08;并发&#xff09;的缓存方案&#xff09; 传统缓存的问题 传统缓存是请求到达tomcat后&#xff0c;先查询redis&#xff0c;如果未命中则查询数据库&#xff0c;问题如下&#xff1a; &#xff08;1&#xff09;请求要经过tomcat处…

ollama改模型的存盘目录解决下载大模型报c:盘空间不足的问题

使用Ollama和Open WebUI快速玩转大模型&#xff1a;简单快捷的尝试各种llm大模型&#xff0c;比如DeepSeek r1&#xff0c;非常简单方便&#xff0c;参见&#xff1a;使用Ollama和Open WebUI快速玩转大模型&#xff1a;简单快捷的尝试各种llm大模型&#xff0c;比如DeepSeek r1…

Hive:静态分区(分区语法,多级分区,分区的查看修改增加删除)

hive在建表时引入了partition概念。即在建表时&#xff0c;将整个表存储在不同的子目录中&#xff0c;每一个子目录对应一个分区。在查询时&#xff0c;我们就可以指定分区查询&#xff0c;避免了hive做全表扫描&#xff0c;从而提高查询率。 oracle和Hive分区的区别 orcale在…