uni-app基本使用02
uni-app基本使用02
uni-app基本使用02

基本常见

生命周期

uniapp 使用Vue3 setup组合式API 引入 uniapp 的 页面生命周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<template>
<view class="content">
<image class="logo" @click="handleFei" src="/static/logo.png"></image>
<view class="text-area">
<text class="title" :class="title">{{ title }}</text>
</view>

<view>
<text class="title"> 启动遇到报错,就是你的版本有问题,或者少依赖 </text>
</view>
</view>
</template>

<script setup>
import {ref} from 'vue'
import {onLaunch, onShow, onHide, onLoad} from '@dcloudio/uni-app'

const title = ref('大飞Vue3,uni-appddd启动成功了');

const handleFei = () => {
console.log('handleFei点击事件=====');
}

onLaunch(()=>{
console.log('App Launch ========')
})

onShow(()=>{
console.log('App Show =====');
})

onHide(()=>{
console.log('App Hide =======')
})

onLoad(()=>{
setTimeout(()=>{
console.log('App onload =======')
},500)
})
</script>


<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}

.text-area {
display: flex;
justify-content: center;
}

.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>

uniapp 生命周期

生命周期分类
  1. 应用生命周期

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <script setup>
    import {onLaunch, onShow, onHide} from '@dcloudio/uni-app'

    onLaunch(()=>{
    console.log('App Launch ========应用初始化完成执行,全局只执行一次')
    })

    onShow(()=>{
    console.log('App Show =====, 应用显示的时候执行,或者从后台进入前台');
    })

    onHide(()=>{
    console.log('App Hide =======, 应用隐藏的时候执行,或者从前台进入后台')
    })
    </script>
  1. 页面生命周期
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    import {onLoad, onShow, onReady} from '@dcloudio/uni-app'

    onLoad(()=>{
    setTimeout(()=>{
    console.log('App onload =======,页面加载的时候触发')
    },500)
    })

    onReady(()=>{
    console.log('App onReady =====,页面初次渲染完成触发 ');
    })
    onShow(()=>{
    console.log('App Show =====, 页面显示的时候触发');
    })
  1. 组件生命周期
    这里的生命周期和Vue3中一样

    1
    2
    3
    4
    5
    6
    7
    beforeCreate(()=>{
    console.log('App beforeCreate=====, 在实例初始化之后,数据观测');
    })

    mounted(()=>{

    })

其他

1
2


Vue3 和 uniapp