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

VUE之组件通信(三)

1、$refs与$parent

1)概述:

  • $refs用于:父——>子。
  • $parent用于:子——>父。

2)原理如下:

属性说明
$refs值为对象,包含所有被ref属性标识的DOM元素或组件实例。
$parent值为对象,当前组件的父组件实例对象

$refs 包含所有子组件的实例对象 

<template><div class="father"><h3>父组件</h3><h4>房产:{{house}}</h4><button @click="changeToy">修改Child1的玩具</button><button @click="changeComputer">修改Child2的电脑</button><button @click="getAllChild($refs)">获取所有子组件的实例对象</button><Child1 ref="c1"/><Child2 ref="c2" /></div>
</template><script setup lang="ts" name="Father">import Child1 from './Child1.vue'import Child2 from './Child2.vue'import {ref} from "vue"let c1 = ref()let c2 = ref()// 数据let house = ref(4)// 方法function changeToy(){console.log(c1.value)c1.value.toy = '小猪佩奇'}function changeComputer(){c1.value.computer= '华为'}function getAllChild([refs:any}){for(let key in refs){console.log(refs[key])refs[key]+=3}}// 向外部提供数据defineExpose({house})</script>
<template><div class="child1"><h3>子组件1</h3><h4>玩具:{{toy}} 个</h4><h4>书籍:{{book}} 本</h4><button @click="minusHouse($parent)">干掉父亲的一套房产</button></div>
</template><script setup lang="ts" name="Child1">import {ref} from "vue"//数据let toy = ref('奥特曼')let book = ref(3)//方法function minusHouse(parent:any){console.log(parent)parent.house -= 1}// 把数据交给外部defineExpose({toy,book})</script>
<template><div class="child2"><h3>子组件2</h3><h4>电脑:{{computer}}</h4><h4>书籍:{{book}} 本</h4></div>
</template><script setup lang="ts" name="Child2">import {ref} from "vue"//数据let computer= ref('奥特曼')let book = ref(6)// 把数据交给外部defineExpose({computer,book})
</script>

一个注意点,有时候.value,有时候不需要:

let obj = reactive({ a:1,b:2,c:ref(3)})  let x = ref(4)

console.log(obj.a) console.log(obj.b) console.log(obj.c)//自动解包

console.log(x.value)

2、provide-inject

<template><div class="father"><h3>父组件2</h3><h4>银子:{{money}}万元</h4><h4>车子:一辆{{car.brand}}车,价值{{car.price}}万元</h4><Child/></div>
</template><script setup lang="ts" name="Child2">import Child from './Child.vue'import {ref,reactive,provide} from "vue"let money = ref(100)let car = reactive({brand:'奔驰',price:100})function updateMoney(value){money.value -= value}// 向后代提供数据provide('qian',{money,updateMoney})provide('che',car)
</script>
<template><div class="grand-child"><h3>我是孙组件</h3><h4>银子:{{x}}</h4><h4>车子:一辆{{car.brand}}车,价值{{car.price}}万元</h4><button @click="updateMoney(6)">花爷爷的钱</button></div>
</template><script setup lang="ts" name="GrandChild">import {inject} from 'vue'let {money,updateMoney} = inject('qian',{money:0,updateMoney:(x:number)=>{}})let car= inject('che',{brand:'未知',price:0}) //通过默认值隐慧教推断</script>
http://www.lryc.cn/news/533142.html

相关文章:

  • 【Redis实战】投票功能
  • linux常用基础命令 最新1
  • UnityShader学习笔记——多种光源
  • 深入浅出谈VR(虚拟现实、VR镜头)
  • 项目2 车牌检测
  • Linux: 网络基础
  • 【实战篇】巧用 DeepSeek,让 Excel 数据处理更高效
  • Flink CDC YAML:面向数据集成的 API 设计
  • RabbitMQ技术深度解析:打造高效消息传递系统
  • DeepSeek与人工智能的结合:探索搜索技术的未来
  • TAPEX:通过神经SQL执行器学习的表格预训练
  • Qt:Qt基础介绍
  • 加速度计信号处理
  • 基于SpringBoot养老院平台系统功能实现六
  • Conmi的正确答案——Rider中添加icon作为exe的图标
  • 机试题——DNS本地缓存
  • Day38【AI思考】-彻底打通线性数据结构间的血脉联系
  • 【LeetCode】152、乘积最大子数组
  • [MRCTF2020]Ez_bypass1(md5绕过)
  • MySQL 缓存机制与架构解析
  • LabVIEW自定义测量参数怎么设置?
  • 海思的一站式集成环境Hispark Studio更新了
  • TresJS:用Vue组件构建3D场景的新选择
  • Axure设计教程:动态排名图(中继器实现)
  • 攻防世界 文件上传
  • 从 .NET Framework 升级到 .NET 8 后 SignalR 问题处理与解决方案
  • 《Node.js Express 框架》
  • Unity LineRenderer 画线及代码控制--Unity小记
  • llama.cpp GGML Quantization Type
  • k8s部署go-fastdfs