当前位置:网站首页>组件之父子传值
组件之父子传值
2022-08-07 05:12:00 【张帆X】
1.父组件传给子组件
(1)首先在子组件中把props:{ }空对象改为空数组props:[ ],里面写要接收值的变量,比如:props:[ 'text' ]
(2)在父组件模板中,引入子组件,注册组件,使用组件,<son></son>
(3)往子组件身上添加属性,v-bind:text='msg'或者 :text='msg'
<son :text='msg'></son>
<template>
<div>
son组件
<div>父组件的值{
{ text }}</div>
</div>
</template>
<script>
export default {
props: ["text"],
data() {
return {};
},
methods: {},
components: {},
};
</script>
<style scoped lang="less">
</style>
<template>
<div class="about">
<h1>This is an about page</h1>
父组件
<son :text="age"></son>
</div>
</template>
<script>
import son from "@/components/son.vue";
export default {
props: {},
data() {
return {
age: 200,
};
},
methods: {},
components: {
son,
},
};
</script>
<style scoped lang="less">
</style>2.子组件传值给父组件
1.在子组件中定义触发事件
<button @click="give">子组件给父组件传值</button>
2.在子组件的methods中写点击事件函数
give(){
this.$emit('getIt', this.sonLists)
}
//sonLists表示子组件要传给父组件的值
3.在父组件中调用子组件中定义的函数名
<son @getIt='getVal'></son>
后面代表父组件的事件名
4.在父组件methods中写事件函数
getVal(v){
this.sonVal=v
console.log(v);
}
//sonVal在父组件中定义,来接收子组件传过来的值
3.vue3的无敌传值,只要是组件的子孙后代就能轻易得到父级,爷级,祖宗级的值
provide('变量名',数据)
provide('data',oldMsg)
inject(‘变量名’)
inject('data')
边栏推荐
猜你喜欢

Sigrity PowerDC仿真

Explain in detail the big game behind Justin Sun's "Sea of Stars", space travel needs more people to participate

小 P 周刊 Vol.14

共享设备租赁小程序开发制作功能介绍

推荐抓包工具

Linear Algebra Study Notes 4-6: Null Space, Column Space, Row Space, Left Null Space, Elementary Row Transformation, Test Questions of Matrix
![[TypeScript Notes] 02 - TS Advanced Types](/img/2f/43e36480cc2eb42d09287c16fab0dc.png)
[TypeScript Notes] 02 - TS Advanced Types

乘势而上,OceanBase推动数字支付精益增长

详解孙宇晨“星辰大海”背后的一盘大棋,太空旅行需要更多人参与

Redis关闭持久化
随机推荐
字符脱敏工具
聊聊Redis内存优化的7个神技
happens-before rule and thread singleton safety exercise
CentOs通过yum在线安装Mysql
乘势而上,OceanBase推动数字支付精益增长
预约家教老师上门辅导小程序开发制作功能介绍
线性代数学习笔记5-1:正交的向量/空间、正交补(行空间和零空间正交)
Character Desensitization Tool
pymysql格式化输入的一些问题
POI操作Excel导入导出自定义工具类
Terminal data encryption
BPAAS化建设实践-基本流程篇
Redis 发布订阅操作
【毕业设计】基于STM32的自动加油站加油系统 -物联网 单片机 嵌入式
pta 天梯赛 L1-020 帅到没朋友
页面底部出现横向滚动条解决方法
洛谷P2432 zxbsmk爱查错
线性代数学习笔记4-1:线性方程组的数学和几何意义、零空间/解空间/核
洛谷P2412 查单词
Sigrity PowerDC Simulation