lifecycle.uvue 1.8 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

<template>
	<view>
		<page-head :title="title"></page-head>
		<button @tap="testGoOtherActivity">跳转选择界面</button>
		<image :src="selectImage" v-if="selectImage"></image>
		<button @tap="testUnRegLifecycle">取消注册周期函数</button>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="uni-hello-text">
				1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。
			</view>
			<view class="uni-hello-text">
				2. 手动切换其他APP再返回,可在控制台和界面观察事件日志
			</view>
		</view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="text-box" scroll-y="true">
				<text>{{text}}</text>
			</view>
		</view>
	</view>
</template>
<script>
	import { initAppLifecycle,unRegLifecycle,goOtherActivity } from '../../uni_modules/uts-advance';
	export default {
		data() {
			return {
				title: '生命周期监听',
				text: '',
				selectImage:""
			}
		},
		onLoad:function(){
			let that = this;
			initAppLifecycle(function(eventLog){
				// 展示捕捉到的声明周期日志
Y
yurj26 已提交
37 38
				that.text += eventLog;
				that.text += '\n';
杜庆泉's avatar
杜庆泉 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
			});
		},
		methods:{
			testGoOtherActivity(){
				var that = this;
				let ret = goOtherActivity(function(file){
					// 展示捕捉到的声明周期日志
					console.log(file);
					that.selectImage = "file://" + file;
				});
				
				if(!ret){
					uni.showToast({
						icon:'none',
						title:'请授予权限后重试'
					})
				}
			},
			testUnRegLifecycle(){
				// 取消注册生命周期
				unRegLifecycle();
			}
		}
	}
</script>

<style>
	.text-box {
		margin-bottom: 40rpx;
		padding: 40rpx 0;
		display: flex;
		min-height: 300rpx;
		background-color: #FFFFFF;
		justify-content: center;
		align-items: center;
		text-align: center;
		font-size: 30rpx;
		color: #353535;
		line-height: 1.8;
	}
</style>