uniapp 子組件向父組件傳值

使用組件可以減少代碼的重複率,提高寫代碼的效率,改起來也方便。

最近在使用uni-app做項目,一套代碼多端實現,做些簡單的項目還是可以的。廢話少說,說說子組件向父組件傳值。

子組件獲取到值的時候,使用$emit傳給父組件:

this.$emit("getChild1",this.list);

父組件首先引入子組件:

import child1 from './child1'

註冊子組件:

components: {
			child1,
			child2
		},

然後可以使用子組件:

<child1 @getChild1 = "getChild1" v-if="current==0"></child1>

注意,子組件的提交的函數名,和父組件裏使用子組件@後的名字是一樣的。

 

 

但是要注意:子組件獲取數據的函數要在created(){}裏面獲取,這個坑,要注意!!

在methods裏可以獲取到子組件的數據:

getChild1(e){
				console.log(e)
			},

效果大概就是這樣:

代碼我也貼出來好了:

首頁是,目錄結構:

res.json:

{
	"code": 100,
	"fruitData": [
		"西紅柿",
		"黃瓜",
		"蘋果"
	],
	"filmData": [
		"西紅柿首富",
		"101次求婚",
		"肖申克的救贖"
	]
}

child1:

<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>

child2:

<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>

parent:

<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>

大概就是這些。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章