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 组件生命周期

相关推荐
小徐_233316 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
iOS阿玮21 小时前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
xw51 天前
uni-app中v-if使用”异常”
前端·uni-app
!win !1 天前
uni-app中v-if使用”异常”
前端·uni-app
Emma歌小白1 天前
如何首次运行小程序后端
微信小程序
赣州云智科技的技术铺子1 天前
【一步步开发AI运动APP】十二、自定义扩展新运动项目1
微信小程序·小程序·云开发·智能小程序
2501_915918411 天前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview
00后程序员张1 天前
iOS App 混淆与加固对比 源码混淆与ipa文件混淆的区别、iOS代码保护与应用安全场景最佳实践
android·安全·ios·小程序·uni-app·iphone·webview
00后程序员张2 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
海绵宝宝不喜欢侬2 天前
uniapp-微信小程序分享功能-onShareAppMessage
微信小程序·小程序·uni-app