父传子

说明:父组件将数据绑定在组件标签上;子组件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笔记 - 父子组件通信的更多相关文章

  1. vue 父子组件通信

    算是初学vue,整理一下父子组件通信笔记. 父组件通过 prop 给子组件下发数据,子组件通过事件给父组件发送消息. 一.父组件向子组件下发数据: 1.在子组件中显式地用props选项声明它预期的数据 ...

  2. 关于React的父子组件通信等等

    //==================================================此处为父子组件通信 1.子组件调用父组件: 父组件将子组件需要调用方法存入props属性内,子组 ...

  3. Vue 非父子组件通信

    组件是Vue核心的一块内容,组件之间的通信也是很基本的开发需求.组件通信又包括父组件向子组件传数据,子组件向父组件传数据,非父子组件间的通信.前两种通信Vue的文档都说的很清楚,但是第三种文档上确只有 ...

  4. Vue 非父子组件通信方案

    Vue 非父子组件通信方案 概述 在 Vue 中模块间的通信很普遍 如果是单纯的父子组件间传递信息,父组件可以使用 props 将数据向下传递到子组件,而在子组件中可以使用 events (父组件需要 ...

  5. vue父子组件及非父子组件通信

    1.父组件传递数据给子组件 父组件数据如何传递给子组件呢?可以通过props属性来实现 父组件: <parent> <child :child-msg="msg" ...

  6. Vue(二十六)父子组件通信

    今天写了一个分页公共组件,就出现了父子组件通信的问题,今天来总结下我遇到的父子组件通信问题 一.子组件调取父组件的数据或方法 (1)props 想要把父组件的值,传到子组件中,使用props 比如你在 ...

  7. 三大前端框架(react、vue、angular2+)父子组件通信总结

    公司业务需要,react.vue.angular都有接触[\无奈脸].虽然说可以拓展知识广度,但是在深度上很让人头疼.最近没事的时候回忆各框架父子组件通信,发现很模糊,于是乎稍微做了一下功课,记录于此 ...

  8. Vuejs——(10)组件——父子组件通信

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   本篇资料来于官方文档: http://cn.vuejs.org/guide/components ...

  9. 从$emit 到 父子组件通信 再到 eventBus

    故事还是得从$emit说起,某一天翻文档的时候看到$emit的说明 触发当前实例上的事件?就是自身组件上的事件呗,在父子组件通信中,父组件通过props传递给子组件数据(高阶组件可以用provide和 ...

  10. vue2.0父子组件以及非父子组件通信

    官网API: https://cn.vuejs.org/v2/guide/components.html#Prop 一.父子组件通信 1.父组件传递数据给子组件,使用props属性来实现 传递普通字符 ...

随机推荐

  1. Android性能优化(一)—— 启动优化,冷启动,热启动,温启动

    APP启动方式 App启动方式分三种:冷启动(cold start).热启动(hot start).温启动(warm start) ▲ 冷启动 系统不存在App进程(APP首次启动或APP被完全杀死) ...

  2. linux中透明巨页与巨页的区别

    在Linux中,透明巨页(Transparent HugePage)和巨页(HugePage)是两种不同的内存管理技术. 透明巨页是Linux内核中的一项特性,旨在提高内存的利用率和性能.它通过将内存 ...

  3. 【PyTorch】state_dict详解

    这篇博客来自csdn,完全用于学习. Introduce 在pytorch中,torch.nn.Module模块中的state_dict变量存放训练过程中需要学习的权重和偏执系数,state_dict ...

  4. Android复习(三)清单文件中的元素——>supports-gl-texture、supports-screens

    <supports-gl-texture> 注意:Google Play 会根据应用支持的纹理压缩格式对其进行过滤,以确保应用只能安装在可正确处理其纹理的设备上.您可以将纹理压缩过滤用作定 ...

  5. JOI Open 2017(口胡)

    T1 Amusement Park 题意:通信题.给定一张 \(n\) 个点 \(m\) 条边的无向连通图.Alice 会得到一个 \([0, 2^{60})\) 中的数 \(x\),并且她需要给这张 ...

  6. [快速阅读八] Matlab中bwlookup的实现及其在计算二值图像的欧拉数、面积及其他morph变形中的应用。

    以前看过matlab的bwlookup函数,但是总感觉有点神秘,一直没有去仔细分析,最近在分析计算二值图像的欧拉数时,发现自己写的代码和matlab的总是对不少,于是又去翻了下matlab的源代码,看 ...

  7. MongoDB聚合类操作

    MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*) 语法:db.tablename.aggregat ...

  8. Httprunner生成Allure格式HTML报告

    一.httprunner v2.x版本的报告 最近组内其他同学使用httprunner做接口自动化,之前没有接触过httprunner,发现httprunner相比pytest和unittest有自己 ...

  9. shell脚本安装卸载统一脚本

    #!/bin/bash set -e OUT_DIR=out function usage() { cat - <<-EOF SlightShift-SPB Kit Usage: $0 & ...

  10. OSG开发笔记(三十一):OSG中LOD层次细节模型介绍和使用

    前言   模型较大的时候,出现卡顿,那么使用LOD(细节层次)进行层次细节调整,可以让原本卡顿的模型变得不卡顿.  本就是LOD介绍.   Demo      LOD 概述   LOD也称为层次细节模 ...