父组件与子组件相互传递数据 vue

子组件像父组件传递数据

1.需要触发按钮或者create事件
child.vue子组件

<template>
	<div>
		<div :data="treeDataItem" ref="treeData">子组件</div>
		<button @click="submit">触发</button>
	</div>
</template>
<script>
	//vue3x
	//emits: ['getTreeData'],
  	methods: {
		submit(){
			//this.$emit("getTreeData",this.$refs.treeData.getCheckNodes());
			this.$emit("getTreeData",this.treeDataItem);
		}
		getCheckNodes(){
		}
	}
</script>

父组件

<template>
<Child @getTreeData="'testData($even)'">父组件</Child>
</template>
<script>
	testData(data){
	//子组件的数据
		console.log(data)
	}

</script>

2.不需要在子组件中触发事件(如点击按钮,create事件等等)
Child.vue子组件


<template>
	<div>
		<div :data="treeDataItem" ref="treeData">子组件</div>
		<button @click="submit">触发</button>
	</div>
</template>
<script>
	getData(){
		this.$emit("getTreeData",this.$refs.treeData.getCheckNodes());
	}
	getCheckNodes(){
	}
</script>

父组件

<template>
<Child ref="authTree">父组件</Child>
</template>
<script>
	//调用
	console.log(this.$refs.outhTree.getData())
</script>

父组件像子组件传递数据

父组件

<template>
<Child :dataItem="dataItemData" :msg="msgData" :list="listData">父组件</Child>
</template>
<script>
	data(){
		dataItemData:{name:"1",des:"22"},
		msgData:"111",
		listData:[{name:"1",des:"22"}],
	}
	
</script>

Child.vue组件
父组件像子组件传递数据,要在子组件中props获取

<template>
	<div>
		<div>{{dataItem.name}}</div>
		<div>{{msg}}</div>
		<div v-for="(item,index) in list" :key="index">{{item.name}}</div>
	</div>
</template>
<script>
props:{
	dataItem:{
		type:Object,
		default:{
			
		}
	},
	msg:{
		type:String,
		//default:{
			//msg:'111'
		//}
	},
	list:{
		type:Array,
		//注意要用这种箭头方式
		default:()=>{

		}
		//或者
		//default:{
		//	return ({
			
		//	})
		//}
	}
}
</script>	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值