Life cycle function of uni-app
App lifecycle App.js
- onLaunch
When the initialization of the applet is completed, onLaunch will be triggered (only triggered once globally)
- onShow
When the applet starts, or enters the foreground display from the background, onShow will be triggered
- onHide
When the applet enters the background from the foreground, onHide will be triggered
- onError
When a script error occurs in the applet, or the api call fails, onError will be triggered with an error message
<script>
export default {
onLaunch: function() {
console.log('App Launch')
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
onError:function(err){
console.log("err",err);
}
}
</script>
Lifecycle functions in pages
- onLoad
fires when the page loads
- onReady
listens for the completion of the initial rendering of the page (the page loads first - display - last rendering)
- onShow
monitor page display
- onHide
listens for page hiding (when switching to the background, trigger the onHide of the page first, and then hide the onHide of the app)
- onUnload
monitors page unloading (that is, triggered when jumping from one page to another page)
<template>
<view>
我是msg
<view class="dom" ref="domRef" >
我是dom
</view>
<son v-if="flag"></son>
<button type="default" @click="btn">我是btn</button>
</view>
</template>
<script>
import son from "./childCom/son.vue"
export default {
components:{
son
},
data(){
return {
flag:true
}
},
methods: {
onLoad(){
console.log("页面加载",this.$refs.domRef);
},
onReady(){
console.log("页面dom节点加载完毕",this.$refs.domRef);
},
onShow(){
console.log("onShow-监听页面显示");
},
onHide(){
console.log("onHide-监听页面隐藏");
},
onUnload(){
console.log("onUnload-监听页面卸载");
},
btn(){
this.flag = !this.flag;
}
}
}
</script>
<style lang="scss">
</style>