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

feat: globalData

上级 923ebb5b
......@@ -3,6 +3,23 @@
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 () {
// 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1000)
......
......@@ -6,6 +6,40 @@ describe('getApp', () => {
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')
})
it('method', async () => {
const oldLifeCycleNum = await page.data('lifeCycleNum')
await page.callMethod('_increasetLifeCycleNum')
......
......@@ -5,6 +5,32 @@
<view>
<page-head title="getApp"></page-head>
<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>
<text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
<text class="margin-top:6px;">increasetLifeCycleNum 方法</text>
<button class="uni-common-mt" @click="_increasetLifeCycleNum">
......@@ -21,9 +47,53 @@
<script lang="uts">
import { state, setLifeCycleNum } from '@/store/index.uts'
type MyGlobalData = {
str : string,
num : number,
bool : boolean,
obj : UTSJSONObject,
null : string | null,
arr : number[],
mySet : string[],
myMap : UTSJSONObject,
func : () => string
}
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,
}
},
......@@ -31,6 +101,62 @@
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()
},
_increasetLifeCycleNum: function () {
const app = getApp()
app.increasetLifeCycleNum()
......@@ -48,5 +174,8 @@
.bold {
font-weight: bold;
}
.hr{border-bottom: 1px solid #ccc;}
.hr {
border-bottom: 1px solid #ccc;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册