vue父子组件之间传值、父组件传子组件、子组件传父组件

先画个对应关系后面的基于这个,父main子header
在这里插入图片描述

一、父组件传给子组件、子组件获取父组件

1. props

这种方式不支持子组件更改父组件的值,想改,这个下面有个$parent用他

//main.vue
<template>
	<c-header :user="user"></c-header>//:子组件接收值="data里的值"
	//要是要传的多,又有数据又想调方法,那就直接给实例传过去
	<c-header :main="this"></c-header>//传实例
</template>
<script>
//先引进来路径想着改
import head from "./header"
export default {
	data(){
		return{
			user:{
				username:"haha",
				password:"123"
			}
		}
	},
	methods:{
		run(){
			alert("我是父")
		}
	}
	components:{
		"c-header":head
	}
}
</script>
//header.vue
<template>
	{{user.username}}
	<button @click="getUser">点我获取父组件的值</button>

	{{main.user.password}}
	<button @click="getMain">点我通过实例调方法</button>
</template>
<script>
export default {
	data(){
		return{
		}
	},
	methods:{
		getUser(){
			alert(this.user.username)
		},
		getMain(){
			this.main.run()
		}
		
	}
	props:["user","main"]
	//要是加验证就把props改成对象,也可以加默认值
	/* props:{
		title:Number
	} */
}
</script>

2.$parent

支持更改父组件值

//main.vue
<template>
	<c-header ></c-header>
	
</template>
<script>
//先引进来路径想着改
import head from "./header"
export default {
	data(){
		return{
			user:{
				username:"haha",
				password:"123"
			}
		}
	},
	methods:{
		run(){
			alert("我是父")
		}
	}
	components:{
		"c-header":head
	}
}
</script>
//header.vue
<template>
	<button @click="getParentData">点我获取父组件的值</button>
	<button @click="getParentFn">点我获取父组件的值</button>
</template>
<script>
export default {
	data(){
		return{
		}
	},
	methods:{
		getParentData(){
			alert(this.$parent.user)//获取值
			this.$parent.user.username="heihei"//修改值
		},
		getParentFn(){
			this.$parent.run()
		}
	}
}
</script>

二、子组件传父组件、父组件获取子组件

1. $refs

支持更改子组件值

//main.vue
<template>
	<c-header ref="childHeader"></c-header>
	<button @click="getChildHeader">点我获取子组件的值</button>
</template>
<script>
//先引进来路径想着改
import head from "./header"
export default {
	data(){
		return{
			
		}
	},
	methods:{
		getChildHeader(){
			this.$refs.childHeader.user;//获取数据
			this.$refs.childHeader.getUser();//调用方法
			this.$refs.childHeader.user.username="heihei";//修改值
		}
	}
	components:{
		"c-header":head
	}
}
//header.vue
<template>
	{{user.username}}


</template>
<script>
export default {
	data(){
		return{
			user:{
				username:"haha",
				password:"123"
			}
		}
	},
	methods:{
		getUser(){
			alert(this.user.username)
		},
	}
}
</script>

2.$emit

支持更改子组件值

//main.vue
<template>
	<c-header @getChild="getChild"></c-header>//@getChild自定义方法"父组件中的方法名"
	
</template>
<script>
//先引进来路径想着改
import head from "./header"
export default {
	data(){
		return{
			
		}
	},
	methods:{
		getChild(data){
			alert(data)
		}
	}
	components:{
		"c-header":head
	}
}
//header.vue
<template>

<button @click="getMain">点我执行父组件自定义方法</button>

</template>
<script>
export default {
	data(){
		return{
			user:{
				username:"haha",
				password:"123"
			}
		}
	},
	methods:{
		getMain(){
			//没有传值的 this.$emit("getChild")
			this.$emit("getChild",this.user)
		},
	}
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一颗十月橘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值