Life cycle function of uni-app

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); // 页面加载 undefined 01
			},
			onReady(){
    
    
				console.log("页面dom节点加载完毕",this.$refs.domRef); // 页面dom节点加载完毕 相关dom
			},
			onShow(){
    
    
				console.log("onShow-监听页面显示");// onShow-监听页面显示 02
			},
			onHide(){
    
    
				console.log("onHide-监听页面隐藏");// onHide-监听页面隐藏 切换路由的时候,会隐藏当前组件
			},
			onUnload(){
    
    
				console.log("onUnload-监听页面卸载");// onUnload-监听页面卸载 切换路由的时候,会隐藏当前组件
			},
			btn(){
    
    
				this.flag = !this.flag;
			}
		}
	}
</script>

<style lang="scss">

</style>

Guess you like

Origin blog.csdn.net/weixin_43845137/article/details/123976987