【工作学习 day04】 9. uniapp 页面和组件的生命周期

问题描述

uniapp常用的有:页面和组件,并且页面和组件各自有各自的生命周期函数,那么在页面/组件请求数据时,是用created呢,还是用onLoad呢?

先说结论:

组件使用组件的生命周期,页面使用页面的生命周期。

例如:组件使用created请求数据,页面使用onLoad请求数据。

参考文章:

uni-app子组件onLoad、onReady事件无效_uniapp 组件 onload-CSDN博客

可以看到,Header组件并没有触发onLoad和onReady函数,所以组件中不要使用页面的生命周期。

javascript 复制代码
<template>
    <view class="content">
    <Header />
        
    </view>
</template>

<script>
    import Header from '../../components/Header/Header.vue'
    export default {
        components: {
            Header
        },
        data() {
            return {
                title: 'Hello'
            }
        },
        
        mounted() {
            console.log('index mounted');
        },
        created() {
            console.log('index created');
        },
        onReady() {
            console.log('index onReady');
        },
        onInit() {
            console.log('index onInit');
        },
        onLoad() {
            console.log('index onLoad');
        },
        onShow() {
            console.log('index onShow');
        },
        methods: {

        }
    }
</script>
javascript 复制代码
<template>
    <view>
        this is header
    </view>
</template>

<script>
    export default {
        name:"Header",
        data() {
            return {
                
            };
        },
        mounted() {
            console.log('header mounted');
        },
        created() {
            console.log('header created');
        },
        onReady() {
            console.log('header onReady');
        },
        onInit() {
            console.log('header init');
        },
        onLoad() {
            console.log('header load');
        },
        onShow() {
            console.log('header show');
        }
    }
</script>

<style lang="scss">

</style>
相关推荐
徐小夕30 分钟前
被CRUD拖垮的第5年,我用Cursor 一周"复仇":pxcharts-vue开源,一个全栈老兵的AI编程账本
前端·vue.js·github
Wect2 小时前
LeetCode 39. 组合总和:DFS回溯解法详解
前端·算法·typescript
Wect2 小时前
LeetCode 46. 全排列:深度解析+代码拆解
前端·算法·typescript
IT_陈寒2 小时前
Vite 凭什么比 Webpack 快50%?揭秘闪电构建背后的黑科技
前端·人工智能·后端
hi大雄3 小时前
我的 2025 —— 名为《开始的勇气》🌱
前端·年终总结
从文处安3 小时前
「前端何去何从」一直写 Vue ,为何要在 AI 时代去学 React?
前端·react.js
aircrushin3 小时前
OpenClaw“养龙虾”现象的社会技术学分析
前端·后端
明君879973 小时前
#Flutter 的官方Skills技能库
前端·flutter
yuki_uix4 小时前
重新认识 React Hooks:从会用到理解设计
前端·react.js
林太白4 小时前
ref和reactive对比终于学会了
前端