uni-app 应用与页面生命周期

  1. 应用生命周期
  2. 页面生命周期
  3. 路由跳转

App.vue

<script>
	export default {
		onLaunch: function() {
			console.log('初始化完成时触发(全局只触发一次)')
		},
		onShow: function() {
			console.log('uni-app 启动,或从后台进入前台显示')
		},
		onHide: function() {
			console.log('uni-app 从前台进入后台显示')
		},
		onError: function() {
			console.log('uni-app 报错时触发')
		}
	}
</script>

<style>
	/*每个页面公共css */
	.bg-green {
		background-color: #4CD964;
	}
</style>

index.vue

<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<navigator url="../news/news">
				<text class="title">{{ title }}</text>
			</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello World'
			}
		},
		onLoad() {

		},
		methods: {

		}
	}
</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>

news.vue

<template>
	<view class="bg-green news-style">
		<navigator :url="url" open-type="navigateBack">
			<text>{{ name }}</text>
		</navigator>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: 'news page',
				url: '../index/index'
			}
		},
		onLoad() {
			console.log('页面加载')
		},
		onShow() {
			console.log('页面显示')
		},
		onReady() {
			console.log('页面初次渲染完成')
		},
		onHide() {
			console.log('页面隐藏')
		},
		onUnload() {
			console.log('页面关闭')
		},
		onShareAppMessage() {
			console.log('页面分享')
		},
		onPageScroll() {
			console.log('页面滚动')
		},
		onBackPress() {
			console.log('页面返回')
		},
		methods: {

		}
	}
</script>

<style>
	.news-style {
		width: 750rpx;
		height: 750rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #007AFF;
		color: #FFFFFF;
	}
</style>

你可能感兴趣的:(Uni-App)