1.父转子

// 父组件

<template>
<div>
<div>
<p>{{ count }}</p>
<Son :countFa="count"/>
</div>
</div>
</template> <script setup>
import { ref } from "vue";
import Son from "./son.vue";
const count = ref("我是父组件的值----- 100");
</script>
// 子组件

<template>
<div>
<div>
<div>{{ countFa }}</div>
</div>
</div>
</template> <script setup>
import { ref } from "vue";
const count = ref(790);
const props = defineProps({
countFa: String,
})
</script>

2.子传父

// 父组件

<template>
<div>
<div>
<p>{{ count }}</p>
<Son @changeData="changeData"/>
</div>
</div>
</template> <script setup>
import { ref } from "vue";
import Son from "./son.vue";
const count = ref("我是父组件的值----- 100");
const changeData = (val) => { console.log(val.value); }
</script>
// 子组件

<template>
<div>
<div>
<div>{{ count }}</div>
<div>
<button @click="getClick">button</button>
</div>
</div>
</div>
</template> <script setup>
import { ref, defineEmits } from "vue";
const count = ref(790); const emits = defineEmits(["changeData"]); // 声明触发事件 childClick // 子组件触发父组件方法
const getClick = () => {
count.value++;
emits("changeData", count);
}; </script>

3.父组件调用子组件方法

// 父组件

<template>
<div>
<div>
<p>{{ count }}</p>
<button @click="handleClick">点击调用子组件方法</button>
<Son ref="sonRef" />
</div>
</div>
</template> <script setup>
import { ref } from "vue";
import Son from "./son.vue";
const count = ref("我是父组件的值----- 100");
const sonRef = ref(null);
const handleClick = () => {
sonRef.value.show(798);
};
</script>
// 子组件

<template>
<div>
<div>
<div>{{ count }}</div>
</div>
</div>
</template> <script setup>
import { ref, defineExpose } from "vue";
const count = ref(790); const show = (val) => {
console.log("我是父组件调用子组件的方法", val);
}; defineExpose({
// 这个要加
show,
});
</script>

  

你可以通过在生命周期钩子前面加上 “on” 来访问组件的生命周期钩子。
下表包含如何在 setup () 内部调用生命周期钩子: beforeCreate -> setup()
created -> setup()
beforeMount -> onBeforeMount
mounted -> onMounted
beforeUpdate -> onBeforeUpdate
updated -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed -> onUnmounted
activated -> onActivated
deactivated -> onDeactivated
errorCaptured -> onErrorCaptured

  

模板Refs
通过ref()函数还可以引用页面上的元素或组件

使用步骤:
1、在setup()中创建一个ref对象并返回它
2、在页面上为元素添加ref属性,并设置属性值与创建的ref对象的名称相同
3、当页面渲染完成后,可以通过该ref独享获取到页面中对应的DOM元素

 

链接:https://github.com/mtdream/learningV3

vue3 父子组件间的传值通信的更多相关文章

  1. vue组件定义方式,vue父子组件间的传值

    vue组件定义方式,vue父子组件间的传值 <!DOCTYPE html> <html lang="zh-cn"> <head> <met ...

  2. Vue—非父子组件间的传值(Bus/发布订阅模式/观察者模式/总线)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. vue bus方式解决非父子组件间的传值

    对于非父子组件之间的传值 通常使用VUEX 和总线等方式解决 这里我聊聊发布订阅模式(总线) <body> <div class="app"> <ch ...

  4. React父子组件间的传值

    父组件: import React, { Component } from 'react'; import Child from './chlid'; class parent extends Com ...

  5. 五、react中父子组件间如何传值

    1.父组件向子组件传递数据:父组件绑定属性值传给子组件,子组件通过this.props()接受. 2.子组件向父组件传递数据:子组件绑定一个方法,方法中通过this.props.父组件方法名(参数)传 ...

  6. react组件间的传值方法

    关于react的几个网站: http://react.css88.com/ 小书:http://huziketang.mangojuice.top/books/react/ http://www.re ...

  7. 第四节:Vue表单标签和组件的基本用法,父子组件间的通信

    vue表单标签和组件的基本用法,父子组件间的通信,直接看例子吧. <!DOCTYPE html> <html> <head> <meta charset=&q ...

  8. Vue的父子组件间通信及借助$emit和$on解除父子级通信的耦合度高的问题

    1.父子级间通信,父类找子类非常容易,直接在子组件上加一个ref,父组件直接通过this.$refs操作子组件的数据和方法    父 这边子组件中 就完成了父 => 子组件通信 2. 子 =&g ...

  9. React中父子组件间的通信问题

    1.https://blog.csdn.net/sinat_17775997/article/details/59103173 (React中父子组件间的通信问题)

  10. vue之父子组件间通信实例讲解(props、$ref、$emit)

       组件间如何通信,也就成为了vue中重点知识了.这篇文章将会通过props.$ref和 $emit 这几个知识点,来讲解如何实现父子组件间通信. 组件是 vue.js 最强大的功能之一,而组件实例 ...

随机推荐

  1. 龙蜥正式开源 SysOM:百万级实战经验打造!一站式运维管理平台 | 龙蜥技术

    ​简介:SysOM集监控.告警.诊断.修复.安全能力于一体的操作系统运维平台. ​ 文/系统运维 SIG 如果你被突如其来的 OOPS 和满屏奇怪的函数弄得满头问号?机器内存明明很大,却申请不出来内存 ...

  2. 技术干货 | 深度解构 Android 应用面临紧急发版时的救星方案:mPaaS 热修复——DexPatch

    简介: 关于 Android 热修复方案--DexPatch 的介绍与使用说明 方案介绍 为了解决 Native 模块上线后的问题,mPaaS 提供了热修复功能,实现不发布客户端 apk 场景下的热修 ...

  3. Hologres如何支持超高基数UV计算(基于roaringbitmap实现)

    简介: 本文将会介绍Hologres基于roaringbitmap实现超高基数的UV计算 RoaringBitmap是一种压缩位图索引,RoaringBitmap自身的数据压缩和去重特性十分适合对于大 ...

  4. 【详谈 Delta Lake 】系列技术专题 之 Streaming(流式计算)

    ​简介: 本文翻译自大数据技术公司 Databricks 针对数据湖 Delta Lake 的系列技术文章.众所周知,Databricks 主导着开源大数据社区 Apache Spark.Delta ...

  5. [ML] 工程师使用 Keras 的步骤指引

    设置 import numpy as np import tensorflow as tf from tensorflow import keras 介绍 在训练模型之前准备数据(将其转换为 NumP ...

  6. dotnet 将控制台 Console.WriteLine 内容输出到文件

    很多伙伴喜欢使用 Console.WriteLine 打日志,也许是打起来顺手.打完了之后,又想着,要是能够输出到本机文件那就更好了.既然很多伙伴都有这个想法,那 dotnet 自然就是有方便的方法让 ...

  7. 007_Orcad运用Excel表格创建复杂元器件

    007_Orcad运用Excel表格创建复杂元器件 以AD9135为例,有88个引脚,如果一个一个输入引脚名,比较慢.用Excel做出引脚列表,比较快捷. 首先需要查询datasheet,用Solid ...

  8. Codeforces Good Bye 2023

    A. 2023 正常签到. void solve() { int n, k, ok = 1; cin >> n >> k; int t = 2023; while(n --) ...

  9. 记录Notion API Authorization中的一个坑

    正文 Notion官方文档的Authorization部分提到: In your integration code, include the token in the Authorization he ...

  10. SpringBoot连接redis报错:exception is io.lettuce.core.RedisException: java.io.IOException: 远程主机强迫关闭了一个现有的连接

    一.解决思路 (1).检查redis的配置是否正确 spring redis: host: localhost port: 6379 password: 123456 database: 0 time ...