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. DataWorks 功能实践速览

    ​简介: DataWorks功能实践系列,帮助您解析业务实现过程中的痛点,提高业务功能使用效率! 功能推荐:独享数据集成资源组 如上期数据同步解决方案介绍,数据集成的批数据同步任务运行时,需要占用一定 ...

  2. visual studio 2008的试用版评估期已结束(附无法使用产品密钥)的解决方法

    visual studio 2008过了试用期后再次启动提示对话框:visual studio的试用版评估期已结束. 解决办法: 第一步,在"控制面板"中启动"添加删除程 ...

  3. vue使用websoket(非封装)

    上代码: create:function(){ this.initWebpack() } methods:{ initWebpack(){//初始化websocket           let us ...

  4. [ABC345D] Tiling 位运算の极致运用

    [ABC345D] Tiling 原题解地址:Editorial by Kiri8128 神写法. 将 \(H \times W\) 的网格展开为 \(H \times (W + 1)\) 的序列, ...

  5. Educational Codeforces Round 162 (Rated for Div. 2) E

    E:Link 枚举路径两端的颜色 \(k\). 令 \(g[x]\) 表示满足以下条件的点 \(y\) 数量. $ y \in subtree[x]$ \(col[y] = k\) \(y\) 到 \ ...

  6. Solution Set - CDQ分治&整体二分

    A[洛谷P2163].给定平面上若干个点,多次询问给定矩形内的点数. B[洛谷P3810].给定若干个三元组,对所有\(k\),求这样三元组的个数:恰有\(k\)个三元组,满足其每个分量都不超过它的相 ...

  7. windows10 64 解决 exec:"gcc" executable file not found in %PATH%报错问题

    1.下载编译好的包 https://sourceforge.net/projects/mingw-w64/files/mingw-w64/ 这里我选择64位的 2.解压完之后,配置环境变量. 解压完到 ...

  8. ansible(5)--ansible的script模块

    1. script模块 作用:在远程主机运行本地的脚本: 调用格式: -m script -a "/PATH/TO/SCRIPT_FILE": 参数: creates:如果其后跟的 ...

  9. .NET C#导出解决方案的NuGet依赖关系

    前言 公司项目需要写DS设计文档,文档需要标识出来你的解决方案文件下的所有项目都使用了NuGet哪些第三方依赖,我们都知道sln下面的所有.csproj文件中的节点下会标识出对应的依赖,但一个一个对比 ...

  10. mini-centos7 环境安装部署,各种踩坑。。。

    最小Linux系统,安装Java环境 想想就生气,去面试个运维,面试官让我上机装个centos7,还是个最小安装包连界面都没有,只有命令行模式,我都哭了,然后让把一些环境装一下,然后再部署个sprin ...