uni-app 父组件引用子组件时怎么调用子组件的方法

本文介绍了在uni-app中如何从父组件引用并调用子组件的方法。通过在子组件定义一个childMethod方法,并在父组件中使用ref属性为子组件设置标识,然后通过this.$refs.childRefName.methodName()来实现调用。

1.写一个简单的子组件main/index.vue:

<template>
    <view>
        
    </view>
</template>

<script>
    export default {
        data(){
            return {
                
            }
        },
        onLoad(){
            
        },
        methods:{
            childMethod() {
                console.log('childMethod do...')
            }
        }
    }
</script>

<style>

</style>

在子组件中有一个childMethod方法

2.在父组件中引用这个子组件的childMethod方法:

<template>
    <view class="content">
        <mian-index ref="mainindex"></mian-index>
        <view @tap="dataAction">button</view>
    </view>
</template>
<script>
    import mainindex from '@/pages/main/index/index.vue'
    export default {
        data() {
            return {

            };
        },
        components:{
            'mian-index':mainindex
        },
        onLoad(e) {

        },
        methods:{
            dataAction:function(){
                this.$refs.mainindex.childMethod();
            }
        }
    }
</script>

<style scoped>
.content{
    width:100%;
    box-sizing: border-box;
}
</style>

首先,引入子组件文件,然后用ref给子组件一个id标识,然后通过this.$refs.mainindex.childMethod();调用子组件方法

 转载时请注明出处及相应链接,本文永久地址:https://www.cnblogs.com/wangxiaoling/p/10250903.html,谢谢!

转载于:https://www.cnblogs.com/wangxiaoling/p/10250903.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值