vue3笔记 - 父子组件通信
父传子
说明:父组件将数据绑定在组件标签上;子组件props接收
父组件:
<template>
<Child :msg="msg" />
</template>
<script setup>
import Child from './child.vue'
import { ref } from 'vue'
const msg = ref('hello world')
</script>
子组件,使用defineProps接收:
<template>
<div>收到父组件传的值:{{ msg }}</div>
</template>
<script setup>
const props = defineProps({
msg: {
type: String
}
})
</script>
子传父
说明:父组件定义自定义事件,绑定在子组件标签上;子组件使用emit,触发方法、传值
父组件:
<template>
<Child @addCount="addCount" @resetCount="resetCount" />
<div>count: {{ count }}</div>
</template>
<script setup>
import { ref } from 'vue'
import Child from './child.vue'
let count = ref(0)
function addCount(e) {
count.value += e
}
function resetCount() {
count.value = 0
}
</script>
子组件使用defineEmits声明事件:
<template>
<el-button type="primary" @click="addCount">累加count</el-button>
<el-button type="primary" @click="resetCount">重置count</el-button>
</template>
<script setup>
import { defineEmits } from 'vue'
const emit = defineEmits(['addCount', 'resetCount'])
function addCount() {
emit('addCount', 22)
}
function resetCount() {
emit('resetCount')
}
</script>
子组件直接修改父组件传过来的值
父组件:
vue2如果想让子组件能直接修改数据,使用的是.sync, vue3 使用v-model
<template>
<Child v-model:count="count" />
<div>count: {{ count }}</div>
</template>
<script setup>
import { ref } from 'vue'
import Child from './child.vue'
let count = ref(0)
</script>
子组件:
需要声明一个 update:count 事件
<template>
<el-button type="primary" @click="updateCount">修改count</el-button>
</template>
<script setup>
import { defineEmits } from 'vue'
const props = defineProps({
count: {
type: Number
}
})
const emit = defineEmits(['addCount', 'resetCount', 'update:count'])
function updateCount() {
emit('update:count', 100)
}
</script>
defineExpose
使用<script setup>的组件,不会暴露任何声明的变量属性,也就是无法向vue2中:this.$refs.child.data类似这样获取数据
如果想操作,可以使用defineExpose来显式的指定出,需要暴露出去的属性
父组件:
<template>
<el-button type="warning" @click="getChildVal">获取子组件中的值</el-button>
<div>{{ childMsg }}</div>
<el-button type="primary" @click="runChildFunc">执行子组件的方法</el-button>
<child ref="child" />
</template>
<script setup>
import { ref } from 'vue'
import Child from './child.vue'
// 必须跟子组件 ref 保持一致
const child = ref(null)
const childMsg = ref('')
// 获取子组件的数据
function getChildVal() {
childMsg.value = child.value.msg
}
// 执行子组件的方法
function runChildFunc() {
child.value.testFunc()
}
</script>
子组件需要使用defineExpose暴露变量和方法:
<template>
<div v-if="isShow">被父组件执行了,你个吊毛</div>
</template>
<script setup>
import { ref } from 'vue'
const msg = ref('在座的各位都是辣鸡!!!')
const isShow = ref(false)
const testFunc = function () {
isShow.value = !isShow.value
}
defineExpose({
msg,
testFunc
})
</script>
ref属性
使用ref获取DOM或者组件实例
与vue2写法的区别,vue2中:
this.$refs.child.data
vue3没有this,需要声明一个 和 标签ref保持一致的响应式变量,进行操作;如果想获取子组件的数据,可以看上面的defineExpose
- 操作单个组件 或者 DOM
<template>
<child ref="child" />
</template>
<script setup>
import Child from './child.vue'
// 变量名称必须跟子组件 ref 保持一致
const child = ref()
// 如果想获取子组件的数据,可以看上面的defineExpose
console.log(child.value)
console.log(child.value.msg)
</script>
- 操作多个DOM
定义一个函数,动态绑定到ref上即可
<template>
<ul>
<li v-for="item in 10" :ref="setRef">{{ item }}</li>
</ul>
</template>
<script setup>
const setRef = (el) => {
console.log(el)
}
</script>
上面写法不太好区分某个DOM,也可以写成如下:
<template>
<ul>
<li v-for="item in 10" :ref="el => setRef(el, item)">{{ item }}</li>
</ul>
</template>
<script setup>
const setRef = (el, item) => {
console.log(el, item)
}
</script>
vue3笔记 - 父子组件通信的更多相关文章
- vue 父子组件通信
算是初学vue,整理一下父子组件通信笔记. 父组件通过 prop 给子组件下发数据,子组件通过事件给父组件发送消息. 一.父组件向子组件下发数据: 1.在子组件中显式地用props选项声明它预期的数据 ...
- 关于React的父子组件通信等等
//==================================================此处为父子组件通信 1.子组件调用父组件: 父组件将子组件需要调用方法存入props属性内,子组 ...
- Vue 非父子组件通信
组件是Vue核心的一块内容,组件之间的通信也是很基本的开发需求.组件通信又包括父组件向子组件传数据,子组件向父组件传数据,非父子组件间的通信.前两种通信Vue的文档都说的很清楚,但是第三种文档上确只有 ...
- Vue 非父子组件通信方案
Vue 非父子组件通信方案 概述 在 Vue 中模块间的通信很普遍 如果是单纯的父子组件间传递信息,父组件可以使用 props 将数据向下传递到子组件,而在子组件中可以使用 events (父组件需要 ...
- vue父子组件及非父子组件通信
1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: <parent> <child :child-msg="msg" ...
- Vue(二十六)父子组件通信
今天写了一个分页公共组件,就出现了父子组件通信的问题,今天来总结下我遇到的父子组件通信问题 一.子组件调取父组件的数据或方法 (1)props 想要把父组件的值,传到子组件中,使用props 比如你在 ...
- 三大前端框架(react、vue、angular2+)父子组件通信总结
公司业务需要,react.vue.angular都有接触[\无奈脸].虽然说可以拓展知识广度,但是在深度上很让人头疼.最近没事的时候回忆各框架父子组件通信,发现很模糊,于是乎稍微做了一下功课,记录于此 ...
- Vuejs——(10)组件——父子组件通信
版权声明:出处http://blog.csdn.net/qq20004604 目录(?)[+] 本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...
- 从$emit 到 父子组件通信 再到 eventBus
故事还是得从$emit说起,某一天翻文档的时候看到$emit的说明 触发当前实例上的事件?就是自身组件上的事件呗,在父子组件通信中,父组件通过props传递给子组件数据(高阶组件可以用provide和 ...
- vue2.0父子组件以及非父子组件通信
官网API: https://cn.vuejs.org/v2/guide/components.html#Prop 一.父子组件通信 1.父组件传递数据给子组件,使用props属性来实现 传递普通字符 ...
随机推荐
- SCHED_RR和SCHED_FIFO的区别
SCHED_RR和SCHED_FIFO是Linux内核中用来调度进程的两种调度策略,它们有以下几点区别: 调度方式:SCHED_RR采用轮转调度方式,而SCHED_FIFO则采用先进先出调度方式. 优 ...
- iOS之动画(transform和UIView动画)学习
1.transform 形变 这个是UIView的属性,继承UIView的控件都具有这个属性 UIImageView *imageview=[[UIImageView alloc]init]; ima ...
- vue的计算属性computed和监视属性waatch的区别
共同的:都是用于监听数据变化的属性: 计算属性:必须有返回值return ,依赖其它属性值,其它属性值发生变化的时候就会重新计算 : 监视属性:每当数据变化的时候就会触发执行,watch有两个新值和旧 ...
- KubeSphere 3.1.0 GA:混合多云走向边缘,让应用无处不在
2021 年 4 月 29 日,KubeSphere 开源社区激动地向大家宣布,KubeSphere 3.1.0 正式发布!为了帮助企业最大化资源利用效率,KubeSphere 打造了一个以 Kube ...
- 一文彻底弄懂MySQL的MVCC多版本控制器
InnoDB 的 MVCC(Multi-Version Concurrency Control,多版本并发控制) 是 MySQL 实现高并发事务处理的一种机制.通过 MVCC,InnoDB 可以在高并 ...
- Ubuntu 22.04 和 Windows 时间冲突解决方案
默认情况下,Ubuntu(和大多数其他 Linux 发行版)假设硬件时钟设置为协调世界时间(UTC + 0),而 Windows 则假设硬件时钟设置为当地时间,这导致 Ubuntu 快 8 小时. 这 ...
- 游戏引擎数学库 Plane
0 前言 平面的表达方式有很多,常用的就两种.向量形式的点法式,标量形式的平面方程.两者可以互相转化. \[(\mathbf{p}-\mathbf{p_0})\cdot\mathbf{n}=0 \] ...
- 轻量级网络-CSPNet 论文解读
摘要 1,介绍 2,相关工作 3,改进方法 3.1,Cross Stage Partial Network 3.2,Exact Fusion Model 4,实验 4.1,实验细节 4.2,消融实验 ...
- 为数据集而生的 SQL 控制台
随着数据集的使用量急剧增加,Hugging Face 社区已经变成了众多数据集默认存放的仓库.每月,海量数据集被上传到社区,这些数据集亟需有效的查询.过滤和发现. 每个月在 Hugging Face ...
- Windows Server 开启远程桌面
工作中不可避免地经常要用到WindowsServer,为了使用的方便,配置了远程桌面,这里记录一下远程桌面配置过程中遇到的一些问题以及解决方法. Windows Server2008 R2 x64 1 ...