uniapp子组件向父组件传值

子组件向父组件传值

子组件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>

最后

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

相关推荐

  1. uniapp组件组件

    2024-01-05 14:50:07       42 阅读
  2. React组件组件

    2024-01-05 14:50:07       35 阅读
  3. Vue实现组件组件

    2024-01-05 14:50:07       16 阅读
  4. VUE中组件组件进行

    2024-01-05 14:50:07       10 阅读
  5. 请说明Vue组件组件的方法

    2024-01-05 14:50:07       17 阅读
  6. 组件组件参的方式?

    2024-01-05 14:50:07       19 阅读
  7. vue3 组件组件

    2024-01-05 14:50:07       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-05 14:50:07       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-05 14:50:07       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-05 14:50:07       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-05 14:50:07       18 阅读

热门阅读

  1. 多汗症对患者的生活有哪些影响?

    2024-01-05 14:50:07       43 阅读
  2. 整理composer安装版本的python脚本

    2024-01-05 14:50:07       37 阅读
  3. LeetCode 33 搜索旋转排序数组

    2024-01-05 14:50:07       38 阅读
  4. 举例说明自然语言处理(NLP)技术

    2024-01-05 14:50:07       33 阅读
  5. hive问题记录

    2024-01-05 14:50:07       34 阅读
  6. React格式化规范

    2024-01-05 14:50:07       35 阅读
  7. React监听窗口宽度变化 触发resize(Hooks版本)

    2024-01-05 14:50:07       33 阅读
  8. 微信小程序:selectComponent返回null的问题

    2024-01-05 14:50:07       37 阅读