tnblog
首页
视频
资源
登录

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

5421人阅读 2021/5/26 19:11 总访问:53501 评论:0 收藏:0 手机
分类: uinapp

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();调用子组件方法

评价
走投无路 才选择了程序员
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术