zoukankan      html  css  js  c++  java
  • uni-app 父组件引用子组件时怎么调用子组件的方法

    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,谢谢!

  • 相关阅读:
    linux分区-df
    day34 协程
    day33 GIL锁 线程队列 线程池
    day32 线程
    day31 管道 进程池 数据共享
    day30 锁 队列
    day29 进程
    day28 socketserver
    day27 粘包及粘包的解决方案
    day26 网络通讯的整个流程
  • 原文地址:https://www.cnblogs.com/wangxiaoling/p/10250903.html
Copyright © 2011-2022 走看看