uniapp子组件向父组件传值

本文详细描述了在Vue应用中,如何实现子组件向父组件传递数据,以及如何通过`$emit`事件进行通信。以子组件1和子组件2分别展示水果和电影列表为例,展示了如何在父组件间切换并接收子组件的数据更新。
摘要由CSDN通过智能技术生成

子组件向父组件传值

子组件1

<template>
	<view class="content">
		<view v-for="(item,index) in list" :key="index">{{item}}</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				list: []
			}
		},
		components: {
		},
		onLoad() {},
		created() {
			var data = require('../../static/res.json')
			this.list = data.fruitData
			this.$emit("getChild1",this.list);
		},
		methods: {
		}
	}
</script>
 
<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
 
	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 200upx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50upx;
	}
 
	.text-area {
		display: flex;
		justify-content: center;
	}
 
	.title {
		font-size: 36upx;
		color: #8f8f94;
	}
</style>

子组件2

<template>
	<view class="content">
		<view v-for="(item,index) in list" :key="index">{{item}}</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				list: []
			}
		},
		components: {
		},
		onLoad() {},
		created() {
			var data = require('../../static/res.json')
			this.list = data.filmData
			this.$emit("getChild2",this.list);
		},
		methods: {
		}
	}
</script>
 
<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
 
	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 200upx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50upx;
	}
 
	.text-area {
		display: flex;
		justify-content: center;
	}
 
	.title {
		font-size: 36upx;
		color: #8f8f94;
	}
</style>

父组件

<template>
	<view class="content">
		<view class="btn-container">
			<button @click="getData(index)" v-for="(item,index) in list" :key="index">{{item}}</button>
		</view>
		<child1 @getChild1 = "getChild1" v-if="current==0"></child1>
		<child2 @getChild2 = "getChild2" v-if="current==1"></child2>
	</view>
</template>
 
<script>
	import child1 from './child1'
	import child2 from './child2'
	
	export default {
		data() {
			return {
				list:[
					'水果',
					'电影'
				],
				current:0
			}
		},
		onLoad() {
 
		},
		components: {
			child1,
			child2
		},
		methods: {
			getData(index){
				this.current = index
			},
			getChild1(e){
				console.log(e)
			},
			getChild2(e){
				console.log(e)
			}
		}
	}
</script>
 
<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.btn-container{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
</style>

最后

感觉文章好的话记得点个心心和关注和收藏,有错的地方麻烦指正一下,如果需要转载,请标明出处,多谢!!!

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值