父传子

说明:父组件将数据绑定在组件标签上;子组件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. SuperMap流数据应用技术方案

    流数据应用技术方案针对流数据应用场景,针对流数据的海量.多源.持续等特征,进行持续地获取相关的动态位置,以及持续地分析.处理和挖掘. 本章沿用基于单机SuperMap iServer环境,介绍流数据处 ...

  2. OCR+PDF解析配套前端工具开源详解!

    面对日常生活和工作中常见的OCR识别.PDF解析.翻译.校对等场景,配套的可视化工具能够极大地提升我们的使用体验和工作效率. 通过可视化界面,我们可以直观地看到文本识别.解析和翻译的结果,便捷评估产品 ...

  3. Vnode 是什么 ,什么是虚拟DOM ?

    Vnode 是 JavaScript 对象,就是把标签结构的信息描述成js对象 : Vnode 的作用:通过 render 函数 将 template 描述成 Vnode ,然后通过一系列操作转换真实 ...

  4. NlogN 求最长不下降子序列(LIS)

    最长不下降子序列是一道非常经典的题目,我们假设题目如下: 有一个数组 $ a $,现在要从中抽取一个严格上升的子序列(子序列就是你可以从原序列里删除一些数,保留一部分数得到的新数列)(严格上升也就是不 ...

  5. mongodb副本集群使用总结

    一.该文档仅为了总结经验,方便下次部署时跳过踩过的坑.其中MongoDB的复制原理.特点等参照的菜鸟教程,附地址:https://www.runoob.com/mongodb/mongodb-repl ...

  6. MIT 6.002 Circuits and Electronics by Prof. Anant Agarwal

    官网:MIT 2007 Circuits and Electronics 参考资料,课程讲义,课程PPT. 国内的电路像石群老师,罗先觉老师,讲的很细致,也许跟教材有关系,像电阻串并联的等效电阻就用了 ...

  7. house of cat

    调用方法 调用链1 house of cat调用链 __malloc_assert 在 2.35 的 glibc 中源码如下 static void __malloc_assert (const ch ...

  8. react+eslint+prettier 项目配置

    项目地址 https://gitee.com/zhudachangs/react-eslint-prettierrc-demo 项目地址gitee 项目配置eslint(验证) + prettierr ...

  9. Context的典型使用场景

    获取应用文件路径 基类Context提供了获取应用文件路径的能力,ApplicationContext.AbilityStageContext.UIAbilityContext和ExtensionCo ...

  10. 2-6 C/C++ 编写头文件

    目录 头文件怎么起作用 避免头文件被重复引用 避免头文件被重复引用的方法:条件编译 1. 给每个头文件添加一个预编译变量(preprocessor variable)作为标记(Label) 2. 使用 ...