2021-08-18

2021-10-11  本文已影响0人  上海_前端_求内推
uni-app中子组件调用父组件中的方法
1.在父组件methods中定义一个方法:

changeType(type){
    this.typeActive = type;
    alert(type);
}
2.在父组件引用子组件时绑定该方法:
<cate-top   @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"  @click="changeType(item.cate)">
                <text>{{item.name}}</text>
            </view>
        </view>
    </view>
</template>

4.在子组件 methods 中点击事件中,使用 $emit 调用父组件的changeType()方法:
changeType(type){
    this.$emit("pChangeType",type)
},
上一篇 下一篇

猜你喜欢

热点阅读