uni-app 子組件如何調用父組件的方法


1.在父組件methods中定義一個方法:

changeType:function(type){
    this.typeActive = type;
    alert(type);
}

2.在父組件引用子組件時綁定該方法:

<cate-top :catelist="catelist"  v-on:pChangeType="changeType"></cate-top>

3.在子組件中綁定點擊事件:

<template name="cate-top">
    <view class="activity-cover">
        <view class="up-mode">
            <view class="li" v-for="(item,index) in types" :key="index"  @tap="changeType(item.cate)">
                <text>{{item.name}}</text>
            </view>
        </view>
    </view>
</template>

點擊事件為 

@tap="changeType(item.cate)"

4.在子組件 methods 中點擊事件中,使用 $emit 調用父組件的changeType()方法:

changeType:function(type){
    this.$emit("pChangeType")
},

傳參數:

changeType:function(type){
    this.$emit("pChangeType",type)
},

子組件中第一個參數是父組件中調用子組件並傳遞方法需要綁定的方法名,第二個參數是需要傳遞的參數。

轉載時請注明出處及相應鏈接,本文永久地址:https://www.cnblogs.com/wangxiaoling/p/10155056.html,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM