VUE之组件通信(一)

news/2025/2/1 8:50:38 标签: vue.js, javascript, 前端

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('奥特曼')
  // 声明接收props
  defineProps(['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.ts

import 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.niftyadmin.cn/n/5839218.html

相关文章

Python NumPy(7):连接数组、分割数组、数组元素的添加与删除

1 连接数组 函数描述concatenate连接沿现有轴的数组序列stack沿着新的轴加入一系列数组。hstack水平堆叠序列中的数组&#xff08;列方向&#xff09;vstack竖直堆叠序列中的数组&#xff08;行方向&#xff09; 1.1 numpy.concatenate numpy.concatenate 函数用于沿指定轴连…

解决makailio的 kamctl命令的错误

创建FIFO文件并设置权限&#xff1a; 如果FIFO文件不存在&#xff0c;可以手动创建它并设置正确的权限。 mkdir -p /run/kamailio mkfifo /run/kamailio/kamailio_rpc.fifo# 如果设置了权限 要继续执行 chown kamailio:kamailio /run/kamailio/kamailio_rpc.fifo chmod 660 /r…

[Collection与数据结构] B树与B+树

&#x1f338;个人主页:https://blog.csdn.net/2301_80050796?spm1000.2115.3001.5343 &#x1f3f5;️热门专栏: &#x1f9ca; Java基本语法(97平均质量分)https://blog.csdn.net/2301_80050796/category_12615970.html?spm1001.2014.3001.5482 &#x1f355; Collection与…

第05章 18 vtkCutter切割类及与之相似的类说明

vtkCutter 是 VTK&#xff08;Visualization Toolkit&#xff09;中的一个类&#xff0c;用于通过几何体&#xff08;如多边形网格、曲面等&#xff09;进行切割操作&#xff0c;生成切割后的等值面或多段线。它通常用于生成等值线图、等值面或其他类型的切割效果。 vtkCutter…

Keepalived高可用集群入门学习

一、高可用集群 1.LB&#xff1a;load Balance 负载均衡 HA&#xff1a;High Availability 高可用集群 HPC: High Performance Computing 高性能集群 2.提高系统高用性的解决方案&#xff1a;降低MTTR&#xff08;mean time to repair&#xff09;平均故障时间 建立冗余机制…

Solon Cloud Gateway 开发:熟悉 Cloud Gateway

Solon Cloud Gateway 是基于 Solon Cloud、Vert.X 和 Solon-Rx(reactive-streams) 接口实现&#xff0c;响应式的接口体验。因为内置了 solon-boot-vertx &#xff0c;同时也支持 常规的 web 开发。 1、完整的配置说明&#xff08;对应的配置结构类为&#xff1a;GatewayPrope…

22.Word:小张-经费联审核结算单❗【16】

目录 NO1.2 NO3.4​ NO5.6.7 NO8邮件合并 MS搜狗输入法 NO1.2 用ms打开文件&#xff0c;而不是wps❗不然后面都没分布局→页面设置→页面大小→页面方向→上下左右&#xff1a;页边距→页码范围&#xff1a;多页&#xff1a;拼页光标处于→布局→分隔符&#xff1a;分节符…

python项目之requirements.txt文件

Python项目中可以包含一个 requirements.txt 文件&#xff0c;用于记录所有依赖包及其精确的版本号用以新环境部署。 当我们开发新项目的时候&#xff0c;会用virtualenv创建很多python独立环境&#xff0c;这时候就会出现在不同环境下安装相同的模块的情况&#xff0c;这时候…