【工作学习 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>
相关推荐
2分钟速写快排1 小时前
孔乙己的茴字有多少种写法?|AI 对话实时通信的三种实现
前端·javascript·github
Penge6663 小时前
JavaScript:BOM 详解
前端
Penge6664 小时前
Vite vs Webpack:前端构建工具原理与实战
前端
Penge6664 小时前
前端包管理工具:npm、Yarn、pnpm 快速入门
前端
Penge6664 小时前
Sass 了解
前端
Penge6664 小时前
Webpack 打包从 0 到 1 实战笔记
前端
Penge6665 小时前
Webpack 入门:是什么、为什么需要、怎么使用?
前端
Penge6665 小时前
Ant Design(Antd)了解
前端
wangruofeng5 小时前
同一行代码,百种渲染:Asuka、React Native 与 Flutter 跨平台渲染机制深度拆解
前端·github
Penge6666 小时前
JavaScript 核心语法速通
前端