VUE之组件通信(一)

server/2025/2/1 11:58:10/

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/server/164059.html

相关文章

360大数据面试题及参考答案

数据清理有哪些方法? 数据清理是指发现并纠正数据文件中可识别的错误,包括检查数据一致性,处理无效值和缺失值等。常见的数据清理方法有以下几种: 去重处理:数据中可能存在重复的记录,这不仅会占用存储空间,还可能影响分析结果。通过对比每条记录的关键属性,若所有关键…

【橘子ES】使用docker搭建ELK环境

我们在搭建ELK环境的时候&#xff0c;一般有三种选择。 1、本地安装&#xff0c;你可以到官网把安装包下载下来然后解压&#xff0c;修改配置文件&#xff0c;然后启动&#xff0c;这样比较麻烦&#xff0c;比较考验操作。那有的玩家就要问了&#xff0c;这种操作太吃手感了&am…

Linux(19)——使用正则表达式匹配文本

新年快乐&#xff01; 目录 一、正则表达式&#xff1a; 二、通过 grep 匹配正则表达式&#xff1a; 三、查找匹配项&#xff1a; 一、正则表达式&#xff1a; 正则表达式使用模式匹配机制查找特定内容&#xff0c;vim、grep 和 less 命令都可以使用正则表达式&#xff0c;P…

Ubuntu介绍、与centos的区别、基于VMware安装Ubuntu Server 22.04、配置远程连接、安装jdk+Tomcat

目录 ?编辑 一、Ubuntu22.04介绍 二、Ubuntu与Centos的区别 三、基于VMware安装Ubuntu Server 22.04 下载 VMware安装 1.创建新的虚拟机 2.选择类型配置 3.虚拟机硬件兼容性 4.安装客户机操作系统 5.选择客户机操作系统 6.命名虚拟机 7.处理器配置 8.虚拟机内存…

【Leetcode 热题 100】62. 不同路径

问题背景 一个机器人位于一个 m n m \times n mn 网格的左上角。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。 问总共有多少条不同的路径&#xff1f; 数据约束 1 ≤ m , n ≤ 100 1 \le m, n \le 100 1≤m,n≤100题目数据保证答案小于等于 2 1 0…

深入解析 Linux 内核中的页面错误处理机制

在现代操作系统中,页面错误(Page Fault)是内存管理的重要组成部分。当程序试图访问未映射到物理内存的虚拟内存地址时,CPU 会触发页面错误异常。Linux 内核通过一系列复杂的机制来处理这些异常,确保系统的稳定性和性能。本文将深入解析 Linux 内核中处理页面错误的核心代码…

Ansible自动化运维实战--yaml的使用和配置(7/8)

文章目录 一、YAML 基本语法1.1. 缩进1.2. 注释1.3. 列表1.4. 字典 二、Ansible 中 YAML 的应用2.1. Ansible 剧本&#xff08;Playbooks&#xff09;2.2. 变量定义2.3. 角色&#xff08;Roles&#xff09;2.4. Inventory 文件2.5. 数据类型2.6. 引用变量 在 Ansible 里&#x…

App.Current.Services.GetService<UserView>()无限循环

代码无线循环 public partial class UserView : UserControl{public UserView(){InitializeComponent();InitData();}private void InitData(){DataContext App.Current.Services.GetService<UserView>();}} } DataContext App.Current.Services.GetService<User…