uniapp 子组件中onLoad,onShow方法不执行

发布时间 2023-09-19 11:27:56作者: 想要魔法棒

uniapp中,有应用生命周期和页面生命周期,子组件没有应用周期,所有onLoad,onShow都不存在,难怪我用两个方法没有效果。

可以用如下方法:

// 子组件
export default {
    methods: {
        childOnShow() {
            console.log('子组件 onShow');
            // 在这里执行你需要的操作
        }
    }
}

  

// 父组件
<template>
    <view>
        <child-component ref="childComponent"></child-component>
    </view>
</template>

  

// 父组件
export default {
    onShow() {
        this.$refs.childComponent.childOnShow();
    }