父组件如何使用子组件中的方法

子组件,有一个childMethod方法

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

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

<style>

</style>

父组件,调用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{
    100%;
    box-sizing: border-box;
}
</style>

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

原文地址:https://www.cnblogs.com/huihuihero/p/13356382.html