提交 97155cf5 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat: page lifecycle

上级 b496f0a3
<script> <script lang="ts">
export default { import { state, setLifeCycleNum } from './store/index.uts'
onLaunch: function() { export default {
console.log('App Launch') onLaunch: function () {
setLifeCycleNum(state.lifeCycleNum + 1000)
console.log('App Launch')
const performance: Performance = uni.getPerformance() const performance: Performance = uni.getPerformance()
const observer: PerformanceObserver = performance.createObserver((entryList: PerformanceObserverEntryList) => { const observer: PerformanceObserver = performance.createObserver((entryList: PerformanceObserverEntryList) => {
console.log("observer:entryList.getEntries()") console.log("observer:entryList.getEntries()")
console.log(entryList.getEntries()) console.log(entryList.getEntries())
}) })
observer.observe({ entryTypes: ['render', 'navigation'] } as PerformanceObserverOptions) observer.observe({ entryTypes: ['render', 'navigation'] } as PerformanceObserverOptions)
}, },
onShow: function() { onShow: function () {
console.log('App Show') setLifeCycleNum(state.lifeCycleNum + 100)
}, console.log('App Show')
onHide: function() { },
console.log('App Hide') onHide: function () {
} setLifeCycleNum(state.lifeCycleNum - 100)
} console.log('App Hide')
},
onLastPageBackPress(): boolean | null {
setLifeCycleNum(state.lifeCycleNum - 1000)
console.log('App onLastPageBackPress')
return null
},
}
</script> </script>
<style> <style>
/* @font-face { /* @font-face {
font-family: uniicons; font-family: uniicons;
font-weight: normal; font-weight: normal;
font-style: normal; font-style: normal;
src: url('./static/fonts/uni.ttf') format('truetype'); src: url('./static/fonts/uni.ttf') format('truetype');
} */ } */
.page { .page {
padding: 15px; padding: 15px;
} }
.uni-panel { .uni-panel {
margin-bottom: 12px; margin-bottom: 12px;
} }
.uni-panel-h { .uni-panel-h {
background-color: #ffffff; background-color: #ffffff;
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
padding: 12px; padding: 12px;
} }
.uni-panel-h-on { .uni-panel-h-on {
background-color: #f0f0f0; background-color: #f0f0f0;
} }
.uni-panel-text { .uni-panel-text {
color: #000000; color: #000000;
font-size: 14px; font-size: 14px;
font-weight: normal; font-weight: normal;
} }
.uni-panel-icon {} .uni-panel-icon {}
.uni-panel-icon-on { .uni-panel-icon-on {
transform: rotate(180deg); transform: rotate(180deg);
} }
.uni-navigate-item { .uni-navigate-item {
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
background-color: #FFFFFF; background-color: #FFFFFF;
border-top-style: solid; border-top-style: solid;
border-top-color: #f0f0f0; border-top-color: #f0f0f0;
border-top-width: 1px; border-top-width: 1px;
padding: 12px; padding: 12px;
justify-content: space-between; justify-content: space-between;
} }
.uni-navigate-item:active { .uni-navigate-item:active {
background-color: #f8f8f8; background-color: #f8f8f8;
} }
.uni-navigate-text { .uni-navigate-text {
color: #000000; color: #000000;
font-size: 12px; font-size: 12px;
opacity: 0.8; opacity: 0.8;
} }
.uni-navigate-icon { .uni-navigate-icon {
margin-left: 15px; margin-left: 15px;
color: #999999; color: #999999;
font-size: 14px; font-size: 14px;
font-weight: normal; font-weight: normal;
} }
.split-title { .split-title {
margin: 20px 0 5px; margin: 20px 0 5px;
padding: 5px 0; padding: 5px 0;
border-bottom: 1px solid #dfdfdf; border-bottom: 1px solid #dfdfdf;
} }
</style> </style>
\ No newline at end of file
...@@ -52,6 +52,11 @@ ...@@ -52,6 +52,11 @@
"style": { "style": {
"navigationBarTitleText": "v-slot" "navigationBarTitleText": "v-slot"
} }
},{
"path": "pages/page-lifecycle/page-lifecycle",
"style": {
"navigationBarTitleText": "page-lifecycle"
}
} }
], ],
"globalStyle": { "globalStyle": {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { state } from '../store/index.uts'
const STORAGE_KEY_PREFIX = 'INDEX-STATUS' const STORAGE_KEY_PREFIX = 'INDEX-STATUS'
let storageData : Array<string> = [] let storageData : Array<string> = []
...@@ -35,16 +36,7 @@ ...@@ -35,16 +36,7 @@
name: '生命周期', name: '生命周期',
open: false, open: false,
pages: [ pages: [
'beforeCreate', 'page-lifecycle',
'created',
'beforeMount',
'mounted',
// 'beforeUpdate',
// 'updated',
'beforeUnmount',
'unmounted',
'activated',
'deactivated',
] ]
}, },
{ {
...@@ -157,7 +149,10 @@ ...@@ -157,7 +149,10 @@
uni.navigateTo({ uni.navigateTo({
url: `/pages/${e}/${e}` url: `/pages/${e}/${e}`
}) })
} },
getLifeCycleNum(): number {
return state.lifeCycleNum
},
} }
} }
</script> </script>
......
const PAGE_PATH = '/pages/page-lifecycle/page-lifecycle'
const HOME_PATH = '/pages/index'
describe('page-lifecycle', () => {
let page
let lifeCycleNum
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor(1000)
})
it('onLoad onShow onReady', async () => {
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(1220)
})
it('onHide', async () => {
page = await program.navigateTo(HOME_PATH)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(1210)
page = await program.navigateBack()
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(1220)
})
it('onUnload', async () => {
page = await program.redirectTo(HOME_PATH)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(1120)
})
it('onBackPress', async () => {
page = await program.navigateTo(PAGE_PATH)
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(1240)
page = await program.navigateBack()
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(1130)
})
it('onLastPageBackPress', async () => {
page = await program.navigateBack()
lifeCycleNum = await page.callMethod('getLifeCycleNum')
expect(lifeCycleNum).toBe(130)
})
})
\ No newline at end of file
<template>
<view class="page">
<text>page lifecycle</text>
</view>
</template>
<script lang="ts">
import { state, setLifeCycleNum } from '../../store/index.uts'
export default {
onLoad() {
setLifeCycleNum(state.lifeCycleNum + 100)
console.log('Page onLoad')
},
onShow() {
setLifeCycleNum(state.lifeCycleNum + 10)
console.log('Page onShow')
},
onReady() {
setLifeCycleNum(state.lifeCycleNum + 10)
console.log('Page onReady')
},
onHide() {
setLifeCycleNum(state.lifeCycleNum - 10)
console.log('Page onHide')
},
onUnload() {
setLifeCycleNum(state.lifeCycleNum - 100)
console.log('Page onUnload')
},
onBackPress(options: Map<string, string>) : boolean | null {
setLifeCycleNum(state.lifeCycleNum - 10)
console.log('Page onBackPress',options)
return null
},
methods: {
getLifeCycleNum() : number {
return state.lifeCycleNum
}
},
}
</script>
\ No newline at end of file
export type State = {
lifeCycleNum: number
}
export const state = reactive({
lifeCycleNum: 0
} as State)
export const setLifeCycleNum = (num: number) => {
state.lifeCycleNum = num
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册