uniapp 微信小程序:页面+组件的生命周期顺序

uniapp 微信小程序:页面+组件的生命周期顺序

这个uniapp的微信小程序项目使用的是 VUE2

首页

首页只提供了一个跳转按钮。

html 复制代码
<template>
	<view>
		<navigator url="/pages/myPage/myPage?name=jerry" hover-class="navigator-hover">
			<button type="default">跳转到新页面</button>
		</navigator>
	</view>
</template>

<script>
	export default {
		data() {return { }},
	}
</script>

<style>
</style>

页面

html 复制代码
<template>
	<view> 页面 <parent-component>parent-component</parent-component> </view>
</template>

<script>
	export default {
		data() {return { }},
		onInit(option){console.log(`页面 onInit: ${JSON.stringify(option)}`)},
		onLoad(option){console.log(`页面 onLoad: ${JSON.stringify(option)}`)},
		onShow(option){console.log(`页面 onShow: ${JSON.stringify(option)}`)},
		onReady(option){console.log(`页面 onReady: ${JSON.stringify(option)}`)},
		onHide(option){console.log(`页面 onHide: ${JSON.stringify(option)}`)},
		onUnload(option){console.log(`页面 onUnload: ${JSON.stringify(option)}`)},
		onResize(option){console.log(`页面 onResize: ${JSON.stringify(option)}`)},
		onPullDownRefresh(option){console.log(`页面 onPullDownRefresh: ${JSON.stringify(option)}`)},
		onReachBottom(option){console.log(`页面 onReachBottom: ${JSON.stringify(option)}`)},
		onTabItemTap(option){console.log(`页面 onTabItemTap: ${JSON.stringify(option)}`)},
		onShareAppMessage(option){console.log(`页面 onShareAppMessage: ${JSON.stringify(option)}`)},
		onPageScroll(option){console.log(`页面 onPageScroll: ${JSON.stringify(option)}`)},
		onShareTimeline(option){console.log(`页面 onShareTimeline: ${JSON.stringify(option)}`)},
		onAddToFavorites(option){console.log(`页面 onAddToFavorites: ${JSON.stringify(option)}`)},
	}
</script>

<style>
</style>

父组件

html 复制代码
<template>
	<view> 父组件 <child-component>child-component</child-component> </view>
</template>

<script>
	export default {
		data() {return {};},
		beforeCreate(option){console.log(`父组件 beforeCreate: ${option}`)},
		created(option){console.log(`父组件 created: ${option}`)},
		beforeMount(option){console.log(`父组件 beforeMount: ${option}`)},
		mounted(option){console.log(`父组件 mounted: ${option}`)},
		beforeDestroy(option){console.log(`父组件 beforeDestroy: ${option}`)},
		destroyed(option){console.log(`父组件 destroyed: ${option}`)},
	}
</script>

<style>
</style>

子组件

html 复制代码
<template>
	<view> 子组件 </view>
</template>

<script>
	export default {
		data() {return {};},
		beforeCreate(option){console.log(`子组件 beforeCreate: ${option}`)},
		created(option){console.log(`子组件 created: ${option}`)},
		beforeMount(option){console.log(`子组件 beforeMount: ${option}`)},
		mounted(option){console.log(`子组件 mounted: ${option}`)},
		beforeDestroy(option){console.log(`子组件 beforeDestroy: ${option}`)},
		destroyed(option){console.log(`子组件 destroyed: ${option}`)},
	}
</script>

<style>
</style>

完整顺序

html 复制代码
// 进入
父组件 beforeCreate: undefined
父组件 created: undefined
父组件 beforeMount: undefined
子组件 beforeCreate: undefined
子组件 created: undefined
子组件 beforeMount: undefined
页面 onLoad: {"name":"jerry"}
页面 onShow: undefined
子组件 mounted: undefined
父组件 mounted: undefined
页面 onReady: undefined

// 退出
页面 onUnload: undefined
子组件 beforeDestroy: undefined
子组件 destroyed: undefined
父组件 beforeDestroy: undefined
父组件 destroyed: undefined

参考资料

uniap 页面生命周期
uniapp 组件生命周期

相关推荐
^Rocky8 小时前
uniapp 实现腾讯云IM群文件上传下载功能
uni-app·腾讯云
moxiaoran575310 小时前
uni-app学习笔记三十四--刷新和回到顶部的实现
笔记·学习·uni-app
y东施效颦10 小时前
uni-app页面发布测试环境出现连接服务器超时,点击屏幕重试解决方案
前端·javascript·vue.js·uni-app·vue
甜甜的资料库11 小时前
基于微信小程序的睡眠宝系统源码数据库文档
数据库·微信小程序·小程序
华子w90892585911 小时前
SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现
spring boot·微信小程序·uni-app
恰薯条的屑海鸥13 小时前
关于我对各开发语言的看法与接下来的文章内容
开发语言·学习·微信小程序·网站开发·全栈开发
性野喜悲13 小时前
uniapp+<script setup lang=“ts“>解决有数据与暂无数据切换显示,有数据加载时暂无数据闪现(先加载空数据)问题
uni-app
Stanford_110614 小时前
关于大数据的基础知识(二)——国内大数据产业链分布结构
大数据·开发语言·物联网·微信小程序·微信公众平台·twitter·微信开放平台
假客套16 小时前
2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面
微信·uni-app·旅游
邹荣乐17 小时前
uni-app开发微信小程序的报错[渲染层错误]排查及解决
前端·微信小程序·uni-app