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

feat: globalData & globalProperties

上级 8ee7cc84
<script lang="uts">
import { state, setLifeCycleNum, setAppLaunchPath, setAppShowPath } from './store/index.uts'
let firstBackTime = 0
export default {
onLaunch: function (options) {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1000)
setAppLaunchPath(options.path)
console.log('App Launch')
// #ifdef UNI-APP-X && APP-ANDROID
const performance = uni.getPerformance()
const observer : PerformanceObserver = performance.createObserver((entryList : PerformanceObserverEntryList) => {
console.log('observer:entryList.getEntries()')
console.log(entryList.getEntries())
})
observer.observe({
entryTypes: ['render', 'navigation'],
} as PerformanceObserverOptions)
// #endif
},
onShow: function (options) {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 100)
setAppShowPath(options.path)
console.log('App Show')
},
onHide: function () {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 100)
console.log('App Hide')
},
// #ifdef APP-ANDROID
onLastPageBackPress: function () {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1000)
console.log('App LastPageBackPress')
if (firstBackTime == 0) {
uni.showToast({
title: '再按一次退出应用',
position: 'bottom',
})
firstBackTime = Date.now()
setTimeout(() => {
firstBackTime = 0
}, 2000)
} else if (Date.now() - firstBackTime < 2000) {
firstBackTime = Date.now()
uni.exit()
}
},
// #endif
onExit() {
console.log('App Exit')
},
methods: {
checkLaunchPath() : boolean {
const HOME_PATH = '/pages/index'
if (state.appLaunchPath != HOME_PATH) {
return false
}
if (state.appShowPath != HOME_PATH) {
return false
}
return true
}
}
}
import { state, setLifeCycleNum, setAppLaunchPath, setAppShowPath } from './store/index.uts'
let firstBackTime = 0
export default {
globalData: {
str: 'default globalData str',
num: 0,
bool: false,
obj: {
str: 'default globalData obj str',
num: 0,
bool: false,
},
null: null as string | null,
arr: [] as number[],
mySet: new Set<string>(),
myMap: new Map<string, any>(),
func: () : string => {
return 'globalData func'
}
},
onLaunch: function (options) {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1000)
setAppLaunchPath(options.path)
console.log('App Launch')
// #ifdef UNI-APP-X && APP-ANDROID
const performance = uni.getPerformance()
const observer : PerformanceObserver = performance.createObserver((entryList : PerformanceObserverEntryList) => {
console.log('observer:entryList.getEntries()')
console.log(entryList.getEntries())
})
observer.observe({
entryTypes: ['render', 'navigation'],
} as PerformanceObserverOptions)
// #endif
},
onShow: function (options) {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 100)
setAppShowPath(options.path)
console.log('App Show')
},
onHide: function () {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 100)
console.log('App Hide')
},
// #ifdef APP-ANDROID
onLastPageBackPress: function () {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1000)
console.log('App LastPageBackPress')
if (firstBackTime == 0) {
uni.showToast({
title: '再按一次退出应用',
position: 'bottom',
})
firstBackTime = Date.now()
setTimeout(() => {
firstBackTime = 0
}, 2000)
} else if (Date.now() - firstBackTime < 2000) {
firstBackTime = Date.now()
uni.exit()
}
},
// #endif
onExit() {
console.log('App Exit')
},
methods: {
checkLaunchPath() : boolean {
const HOME_PATH = '/pages/index'
if (state.appLaunchPath != HOME_PATH) {
return false
}
if (state.appShowPath != HOME_PATH) {
return false
}
return true
}
}
}
</script>
<style>
@import "./common/uni.css";
/* @font-face {
@import "./common/uni.css";
/* @font-face {
font-family: uniicons;
font-weight: normal;
font-style: normal;
src: url('./static/fonts/uni.ttf') format('truetype');
} */
.page {
padding: 15px;
}
.uni-panel {
margin-bottom: 12px;
}
.uni-panel-h {
background-color: #ffffff;
flex-direction: row;
align-items: center;
padding: 12px;
}
.uni-panel-h-on {
background-color: #f0f0f0;
}
.uni-panel-text {
color: #000000;
font-size: 14px;
font-weight: normal;
}
.uni-panel-icon-on {
transform: rotate(180deg);
}
.uni-navigate-item {
flex-direction: row;
align-items: center;
background-color: #ffffff;
border-top-style: solid;
border-top-color: #f0f0f0;
border-top-width: 1px;
padding: 12px;
justify-content: space-between;
}
.uni-navigate-item:active {
background-color: #f8f8f8;
}
.uni-navigate-text {
color: #000000;
font-size: 12px;
opacity: 0.8;
}
.uni-navigate-icon {
margin-left: 15px;
color: #999999;
font-size: 14px;
font-weight: normal;
}
.text-disabled {
color: #a0a0a0;
}
.list-item-text {
line-height: 36px;
}
.split-title {
margin: 20px 0 5px;
padding: 5px 0;
border-bottom: 1px solid #dfdfdf;
}
.btn-view {
margin: 10px 0;
padding: 10px;
border: 1px solid #dfdfdf;
border-radius: 3px;
}
.page {
padding: 15px;
}
.uni-panel {
margin-bottom: 12px;
}
.uni-panel-h {
background-color: #ffffff;
flex-direction: row;
align-items: center;
padding: 12px;
}
.uni-panel-h-on {
background-color: #f0f0f0;
}
.uni-panel-text {
color: #000000;
font-size: 14px;
font-weight: normal;
}
.uni-panel-icon-on {
transform: rotate(180deg);
}
.uni-navigate-item {
flex-direction: row;
align-items: center;
background-color: #ffffff;
border-top-style: solid;
border-top-color: #f0f0f0;
border-top-width: 1px;
padding: 12px;
justify-content: space-between;
}
.uni-navigate-item:active {
background-color: #f8f8f8;
}
.uni-navigate-text {
color: #000000;
font-size: 12px;
opacity: 0.8;
}
.uni-navigate-icon {
margin-left: 15px;
color: #999999;
font-size: 14px;
font-weight: normal;
}
.text-disabled {
color: #a0a0a0;
}
.list-item-text {
line-height: 36px;
}
.split-title {
margin: 20px 0 5px;
padding: 5px 0;
border-bottom: 1px solid #dfdfdf;
}
.btn-view {
margin: 10px 0;
padding: 10px;
border: 1px solid #dfdfdf;
border-radius: 3px;
}
</style>
\ No newline at end of file
......@@ -276,6 +276,28 @@ export function createApp() {
}, 'CompForPlugin', CompForPlugin)
// #endif
app.config.globalProperties.globalPropertiesStr = 'default string'
app.config.globalProperties.globalPropertiesNum = 0
app.config.globalProperties.globalPropertiesBool = false
app.config.globalProperties.globalPropertiesObj = {
str: 'default globalProperties obj string',
num: 0,
bool: false,
}
app.config.globalProperties.globalPropertiesNull = null as string | null
app.config.globalProperties.globalPropertiesArr = [] as number[]
app.config.globalProperties.globalPropertiesSet = new Set<string>()
app.config.globalProperties.globalPropertiesMap = new Map<string, number>()
app.config.globalProperties.globalPropertiesReactiveObj = reactive({
str: 'default reactive string',
num: 0,
bool: false,
} as UTSJSONObject)
app.config.globalProperties.globalPropertiesFn = function (): string {
console.log('this.globalPropertiesStr', this.globalPropertiesStr)
console.log('this.globalPropertiesNum', this.globalPropertiesNum)
return `globalPropertiesStr: ${this.globalPropertiesStr}, globalPropertiesNum: ${this.globalPropertiesNum}`
}
return {
app
}
......
......@@ -12,6 +12,18 @@
"navigationBarTitleText": "app instance"
}
},
{
"path": "pages/app-instance/globalData/globalData",
"style": {
"navigationBarTitleText": "globalData"
}
},
{
"path": "pages/app-instance/globalProperties/globalProperties",
"style": {
"navigationBarTitleText": "globalProperties"
}
},
{
"path": "pages/built-in-component/keep-alive/keep-alive",
"style": {
......
const PAGE_PATH = '/pages/app-instance/globalData/globalData'
describe('globalData', () => {
let page = null
beforeAll(async () => {
page = await program.navigateTo(PAGE_PATH)
await page.waitFor('view')
})
it('globalData', async () => {
await page.callMethod('getGlobalData')
let data = await page.data()
expect(data.originGlobalData.str).toBe('default globalData str')
expect(data.originGlobalData.num).toBe(0)
expect(data.originGlobalData.bool).toBe(false)
expect(data.originGlobalData.obj).toEqual({
bool: false,
num: 0,
str: 'default globalData obj str',
})
expect(data.originGlobalData.arr).toEqual([])
expect(data.originGlobalData.mySet).toEqual([])
expect(data.originGlobalData.myMap).toEqual({})
expect(data.originGlobalDataFuncRes).toBe('globalData func')
await page.callMethod('setGlobalData')
data = await page.data()
expect(data.newGlobalData.str).toBe('new globalData str')
expect(data.newGlobalData.num).toBe(100)
expect(data.newGlobalData.bool).toBe(true)
expect(data.newGlobalData.obj).toEqual({
bool: true,
num: 200,
str: 'new globalData obj str',
})
expect(data.newGlobalData.arr).toEqual([1, 2, 3])
expect(data.newGlobalData.mySet).toEqual(['a', 'b', 'c'])
expect(data.newGlobalData.myMap).toEqual({a: 1, b: 2, c: 3})
expect(data.newGlobalDataFuncRes).toBe('new globalData func')
})
})
<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1;padding-bottom: 20px;">
<!-- #endif -->
<view>
<view class="uni-padding-wrap">
<button @click="getGlobalData">get globalData</button>
<template v-if="originGlobalData.str.length">
<text class="uni-common-mt bold">初始的 globalData:</text>
<text class="uni-common-mt">globalData string: {{ originGlobalData.str }}</text>
<text class="uni-common-mt">globalData number: {{ originGlobalData.num }}</text>
<text class="uni-common-mt">globalData boolean: {{ originGlobalData.bool }}</text>
<text class="uni-common-mt">globalData object: {{ originGlobalData.obj }}</text>
<text class="uni-common-mt">globalData null: {{ originGlobalData.null }}</text>
<text class="uni-common-mt">globalData array: {{ originGlobalData.arr }}</text>
<text class="uni-common-mt">globalData Set: {{ originGlobalData.mySet }}</text>
<text class="uni-common-mt">globalData Map: {{ originGlobalData.myMap }}</text>
<text class="uni-common-mt">globalData func 返回值: {{ originGlobalDataFuncRes }}</text>
</template>
<button @click="setGlobalData" class="uni-common-mt">set globalData</button>
<template v-if="newGlobalData.bool">
<text class="uni-common-mt bold">更新后的 globalData:</text>
<text class="uni-common-mt">globalData string: {{ newGlobalData.str }}</text>
<text class="uni-common-mt">globalData number: {{ newGlobalData.num }}</text>
<text class="uni-common-mt">globalData boolean: {{ newGlobalData.bool }}</text>
<text class="uni-common-mt">globalData object: {{ newGlobalData.obj }}</text>
<text class="uni-common-mt">globalData null: {{ newGlobalData.null }}</text>
<text class="uni-common-mt">globalData array: {{ newGlobalData.arr }}</text>
<text class="uni-common-mt">globalData Set: {{ newGlobalData.mySet }}</text>
<text class="uni-common-mt">globalData Map: {{ newGlobalData.myMap }}</text>
<text class="uni-common-mt">globalData func 返回值: {{ newGlobalDataFuncRes }}</text>
</template>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script lang="uts">
type MyGlobalData = {
str : string,
num : number,
bool : boolean,
obj : UTSJSONObject,
null : string | null,
arr : number[],
mySet : string[],
myMap : UTSJSONObject,
func : () => string
}
import { state, setLifeCycleNum } from '@/store/index.uts'
export default {
data() {
return {
originGlobalData: {
str: '',
num: 0,
bool: false,
obj: {
str: '',
num: 0,
bool: false
} as UTSJSONObject,
null: null,
arr: [] as number[],
mySet: [] as string[],
myMap: {},
func: () : string => ''
} as MyGlobalData,
originGlobalDataFuncRes: '',
newGlobalData: {
str: '',
num: 0,
bool: false,
obj: {
str: '',
num: 0,
bool: false
} as UTSJSONObject,
null: null,
arr: [] as number[],
mySet: [] as string[],
myMap: {},
func: () : string => ''
} as MyGlobalData,
newGlobalDataFuncRes: '',
lifeCycleNum: 0,
}
},
onReady() {
this.lifeCycleNum = state.lifeCycleNum
},
methods: {
getGlobalData() {
const app = getApp()
this.originGlobalData.str = app.globalData.str
this.originGlobalData.num = app.globalData.num
this.originGlobalData.bool = app.globalData.bool
this.originGlobalData.obj = app.globalData.obj
this.originGlobalData.null = app.globalData.null
this.originGlobalData.arr = app.globalData.arr
app.globalData.mySet.forEach(value => {
this.originGlobalData.mySet.push(value)
})
app.globalData.myMap.forEach((value, key) => {
this.originGlobalData.myMap[key] = value
})
this.originGlobalData.func = app.globalData.func
this.originGlobalDataFuncRes = this.originGlobalData.func()
},
setGlobalData() {
const app = getApp()
app.globalData.str = 'new globalData str'
app.globalData.num = 100
app.globalData.bool = true
app.globalData.obj = {
str: 'new globalData obj str',
num: 200,
bool: true
}
app.globalData.null = 'not null'
app.globalData.arr = [1, 2, 3]
app.globalData.mySet = new Set(['a', 'b', 'c'])
app.globalData.myMap = new Map([
['a', 1],
['b', 2],
['c', 3]
])
app.globalData.func = () : string => {
return 'new globalData func'
}
this.newGlobalData.str = app.globalData.str
this.newGlobalData.num = app.globalData.num
this.newGlobalData.bool = app.globalData.bool
this.newGlobalData.obj = app.globalData.obj
this.newGlobalData.null = app.globalData.null
this.newGlobalData.arr = app.globalData.arr
app.globalData.mySet.forEach(value => {
this.newGlobalData.mySet.push(value)
})
app.globalData.myMap.forEach((value, key) => {
this.newGlobalData.myMap[key] = value
})
this.newGlobalData.func = app.globalData.func
this.newGlobalDataFuncRes = this.newGlobalData.func()
},
},
}
</script>
<style>
.uni-padding-wrap {
padding: 10px;
}
.bold {
font-weight: bold;
}
.hr {
border-bottom: 1px solid #ccc;
}
</style>
\ No newline at end of file
const PAGE_PATH = '/pages/app-instance/globalProperties/globalProperties'
describe('globalProperties', () => {
let page = null
beforeAll(async () => {
page = await program.navigateTo(PAGE_PATH)
await page.waitFor(500)
})
it('globalProperties', async () => {
let data = await page.data()
expect(data.myGlobalProperties.str).toBe('default string')
expect(data.myGlobalProperties.num).toBe(0)
expect(data.myGlobalProperties.bool).toBe(false)
expect(data.myGlobalProperties.obj).toEqual({
bool: false,
num: 0,
str: 'default globalProperties obj string'
})
expect(data.myGlobalProperties.arr).toEqual([])
expect(data.myGlobalProperties.set).toEqual([])
expect(data.myGlobalProperties.map).toEqual({})
expect(data.myGlobalProperties.reactiveObj).toEqual({
str: 'default reactive string',
num: 0,
bool: false
})
expect(data.globalPropertiesFnRes).toBe('globalPropertiesStr: default string, globalPropertiesNum: 0')
await page.callMethod('updateGlobalProperties')
data = await page.data()
expect(data.myGlobalProperties.str).toBe('new string')
expect(data.myGlobalProperties.num).toBe(100)
expect(data.myGlobalProperties.bool).toBe(true)
expect(data.myGlobalProperties.obj).toEqual({
bool: true,
num: 100,
str: 'new globalProperties obj string'
})
expect(data.myGlobalProperties.arr).toEqual([1, 2, 3])
expect(data.myGlobalProperties.set).toEqual(['a', 'b', 'c'])
expect(data.myGlobalProperties.map).toEqual({
'a': 1,
'b': 2,
'c': 3
})
expect(data.myGlobalProperties.reactiveObj).toEqual({
str: 'new reactive string',
num: 200,
bool: true
})
expect(data.globalPropertiesFnRes).toBe('globalPropertiesStr: new string, globalPropertiesNum: 100')
})
it('screenshot', async () => {
await page.waitFor(500)
const image = await program.screenshot({
fullPage: true
});
expect(image).toMatchImageSnapshot();
})
})
\ No newline at end of file
<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1;padding-bottom: 20px;">
<!-- #endif -->
<view class="uni-padding-wrap">
<view class="uni-padding-wrap">
<text class="uni-common-mt">globalProperties string: {{ globalPropertiesStr }}</text>
<text class="uni-common-mt">globalProperties number: {{ globalPropertiesNum }}</text>
<text class="uni-common-mt">globalProperties boolean: {{ globalPropertiesBool }}</text>
<text class="uni-common-mt">globalProperties object: {{ globalPropertiesObj }}</text>
<text class="uni-common-mt">globalProperties null: {{ globalPropertiesNull }}</text>
<text class="uni-common-mt">globalProperties array: {{ globalPropertiesArr }}</text>
<text class="uni-common-mt">globalProperties set: {{ globalPropertiesSet }}</text>
<text class="uni-common-mt">globalProperties map: {{ globalPropertiesMap }}</text>
<text class="uni-common-mt">globalProperties reactiveObj.str: {{ globalPropertiesReactiveObj['str'] }}</text>
<text class="uni-common-mt">globalProperties reactiveObj.num: {{ globalPropertiesReactiveObj['num'] }}</text>
<text class="uni-common-mt">globalProperties reactiveObj.boolean:
{{ globalPropertiesReactiveObj['bool'] }}</text>
<text class="uni-common-mt">globalProperties fun 返回值: {{ globalPropertiesFn() }}</text>
<button @click="updateGlobalProperties" class="uni-common-mt">update globalProperties</button>
</view>
</view>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script lang="uts">
type MyGlobalProperties = {
str : string;
num : number;
bool : boolean;
obj : UTSJSONObject;
null : string | null;
arr : number[];
set : string[];
map : UTSJSONObject;
reactiveObj : UTSJSONObject;
}
export default {
data() {
return {
myGlobalProperties: {
str: '',
num: 0,
bool: false,
obj: {},
null: null,
arr: [],
set: [],
map: {},
reactiveObj: {
str: '',
num: 0,
bool: false,
} as UTSJSONObject,
} as MyGlobalProperties,
globalPropertiesFnRes: '',
bbb: 'my bbb'
}
},
onLoad() {
this.getglobalProperties()
},
methods: {
getglobalProperties() {
this.myGlobalProperties.str = this.globalPropertiesStr
this.myGlobalProperties.num = this.globalPropertiesNum
this.myGlobalProperties.bool = this.globalPropertiesBool
this.myGlobalProperties.obj = this.globalPropertiesObj
this.myGlobalProperties.null = this.globalPropertiesNull
this.myGlobalProperties.arr = this.globalPropertiesArr
this.myGlobalProperties.set = []
this.globalPropertiesSet.forEach(item => {
this.myGlobalProperties.set.push(item)
})
this.myGlobalProperties.map = {}
this.globalPropertiesMap.forEach((value, key) => {
this.myGlobalProperties.map[key!] = value
})
this.myGlobalProperties.reactiveObj = this.globalPropertiesReactiveObj
this.globalPropertiesFnRes = this.globalPropertiesFn()
},
updateGlobalProperties() {
this.globalPropertiesStr = 'new string'
this.globalPropertiesNum = 100
this.globalPropertiesBool = true
this.globalPropertiesObj = {
str: 'new globalProperties obj string',
num: 100,
bool: true,
}
this.globalPropertiesNull = 'not null'
this.globalPropertiesArr = [1, 2, 3]
this.globalPropertiesSet = new Set(['a', 'b', 'c'])
this.globalPropertiesMap = new Map([['a', 1], ['b', 2], ['c', 3]])
this.globalPropertiesReactiveObj['str'] = 'new reactive string'
this.globalPropertiesReactiveObj['num'] = 200
this.globalPropertiesReactiveObj['bool'] = true
this.getglobalProperties()
}
},
}
</script>
<style>
.uni-padding-wrap {
padding: 10px;
}
</style>
\ No newline at end of file
......@@ -61,6 +61,16 @@
url: 'index',
enable: true,
},
{
name: 'globalData',
url: 'globalData',
enable: true,
},
{
name: 'globalProperties',
url: 'globalProperties',
enable: true,
},
] as PageItem[],
},
{
......@@ -415,11 +425,11 @@
enable: false,
// #endif
},
{
name: 'extends',
url: 'extends',
enable: false,
},
{
name: 'extends',
url: 'extends',
enable: false,
}
] as PageItem[],
},
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册