uni-app父子组件间的方法调用及传值


uni-app父子组件间的方法调用及传值
方法调用:

一、父组件调用子组件里的方法
1. 先准备一个子组件
<template>
    <view></view>
</template>

<script>
    export default {
        data(){
            return {}
        },
        methods:{
            childMethod() {  // 子组件中有一个childMethod方法
                console.log('childMethod do...')
            }
        }
    }
</script>

2. 准备一个父组件
<template>
    <view class="content">
        <mian-index ref="mainindex"></mian-index> //使用子组件,ref给子组件一个id标识
    </view>
</template>
<script>
    import mainindex from '@/pages/main/index/index.vue'  //引入子组件
    export default {
        data() {
            return {

            };
        },
        components:{
            'mian-index':mainindex
        },
        onLoad(e) {
            this.$refs.mainindex.childMethod();  //页面加载时就调用子组件里的childMethod方法
        }
    }
</script>

参考地址:https://www.cnblogs.com/cap-rq/p/11026881.html

二、子组件调用父组件里的方法
2.1. 准备一个父组件
<template>
   <view class="father">
   	 <child v-on:ToChange="change"></child>  // 在使用子组件时,通过v-on绑定在父组件内定义好的方法
   </view>
</template>

<script>
   import child from '../../components/child'  //引入子组件
   export default { 
   	...
   	methods: {
   		change(){  // 在父组件内定义好的方法
   			console.log("触发了父页面内的方法");
   		},
   	},
   	components:{
   		child
   	}
   }
</script>

2.2. 在准备一个子组件
<template>
   <view @tap="changeC()">  // 在子组件内绑定触发子组件内定义的方法
       子组件
   </view>
</template>

<script>
   export default {
   ...
   	methods:{
   		changeC(type){
   			this.$emit("Tochange");  // 通过this.$emit触发父组件内绑定的方法
   			console.log("触发了子组件内的方法");
   		}
   	}
   }
</script>

参考地址:https://blog.csdn.net/Sport_18/article/details/102833209

传值:

三、父组件给子组件传值
3.1. 父组件:
<template>
	<view>
		<test :msg="msg"></test>  // :msg="msg"绑定传值
	</view>
</template>

<script>
	import test from "@/components/test/test.vue"  // 引入子组件
	export default {
		data () {
			return {
				msg: 'hello'
			}
		},
		components: {test}
	}
</script>

3.2. 子组件:
<template>
	<view>
		子组件 {{msg}}  //接收后使用
	</view>
</template>

<script>
	export default {
		props: ['msg']  //通过props接收数据
	}
</script>

<style>
</style>

四、子组件给父组件传值
4.1. 父组件
<template>
	<view>
		<test @myEvent="getMsg"></test>
	</view>
</template>
<script>
	import test from "@/components/test/test.vue"   // 引入子组件
	export default {
		methods: {
			getMsg (res) {
				console.log(res)
			}
		},
		components: {test}
	}
</script>

4.2.子组件
<template>
	<view>
		<button type="primary" @click="sendMsg">给父组件传值</button>
	</view>
</template>

<script>
	export default {
		data () {
			return {
				status: 'hello uni-app'
			}
		},
		methods: {
			sendMsg () {
				this.$emit('myEvent',this.status)  // 通过$emit触发事件,第二个参数就是传递的参数
			}
		}
	}
</script>

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gblfy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值