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属性来实现 传递普通字符 ...
随机推荐
- v-show、v-if、v-for的使用
v-if vs v-show v-if 是"真正"的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建. v-if 也是惰性的:如果在初始渲染时条件为 ...
- 使用iis设置网站php版本为7.3
内容:使用iis设置网站php版本为7.3这张图 是多少人的噩梦 早期的宝塔版本 没办法在线升级, php版本只能到7.1 默认就没有7.2以上版本 怎么办?可以在iis设置第一步: 第二步 ...
- seaborn.lmplot详解
官方文档 首先我们要知道,lmplot是用来绘制回归图的. 让我们来看看他的API: seaborn.lmplot(x, y, data, hue=None, col=None, row=None, ...
- MidJourney新手攻略
目录 MidJourney简介 MidJourney的使用 1. 加入Discord 2. 选择一个频道 3. 使用/imagine来输入提示 4. 等待一分钟左右,会输出四张图 5. 查看结果 Re ...
- kubernetes的Deployment
Deployment 在我们发布容器中的服务时,总共有一下几种方式: 将旧的pod停掉,创建新的pod并发布 创建新的pod,然后将旧的pod停掉 滚动式升级.创建一个新的pod,删除一个旧的pod, ...
- 你为什么不应该过度关注go语言的逃逸分析
逃逸分析算是go语言的特色之一,编译器自动分析变量/内存应该分配在栈上还是堆上,程序员不需要主动关心这些事情,保证了内存安全的同时也减轻了程序员的负担. 然而这个"减轻负担"的特性 ...
- Vulnhub 靶机 THE PLANETS: EARTH
0x01信息收集 1.1.nmap扫描 IP段扫描,确定靶机地址 平扫描 nmap 192.168.1.0/24 扫描结果(部分) Nmap scan report for earth.local ( ...
- CentOS8安装RabbitMQ3.8.16
之前安装过旧版的RabbitMQ和Erlang,先卸载. ①:卸载RabbitMQ /sbin/service rabbitmq-server stop yum list | grep rabbitm ...
- 如何将rust日志输出到android终端
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议.转载请注明来自 唯你 背景 在 Rust 中,使用 println!打印日志时,输出实际上是发送到标准输出(stdout),而 ...
- 构建人工智能模型基础:TFDS和Keras的完美搭配
上一篇:<数据工程师,转型人工智能岗位的理想时空通道> 序言:本节将带您深入探索 TensorFlow 提供的关键工具和方法,涵盖数据集管理和神经网络模型的构建与训练.在现代人工智能框架中 ...