Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
3331cf02
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
6067
Star
93
Fork
166
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
3331cf02
编写于
11月 23, 2023
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: globalData 补充数据类型
上级
da9ae769
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
169 addition
and
40 deletion
+169
-40
App.uvue
App.uvue
+18
-6
pages/API/get-app/get-app.test.js
pages/API/get-app/get-app.test.js
+16
-7
pages/API/get-app/get-app.uvue
pages/API/get-app/get-app.uvue
+135
-27
未找到文件。
App.uvue
浏览文件 @
3331cf02
...
...
@@ -2,11 +2,23 @@
import { state, setLifeCycleNum } from '@/store/index.uts'
let firstBackTime = 0
export default {
globalData: {
str: 'global data str',
num: 123,
bool: true,
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[],
set: new Set<string>(),
map: new Map<string, any>(),
func: (): string => {
return 'globalData func'
}
},
onLaunch: function () {
// 自动化测试
...
...
@@ -67,4 +79,4 @@
<style>
/*每个页面公共css */
@import "./common/uni.css";
</style>
\ No newline at end of file
</style>
pages/API/get-app/get-app.test.js
浏览文件 @
3331cf02
...
...
@@ -9,15 +9,24 @@ describe('getApp', () => {
it
(
'
globalData
'
,
async
()
=>
{
await
page
.
callMethod
(
'
getGlobalData
'
)
let
data
=
await
page
.
data
()
expect
(
data
.
globalDataStr
).
toBe
(
'
global data str
'
)
expect
(
data
.
globalDataNum
).
toBe
(
123
)
expect
(
data
.
globalDataBool
).
toBe
(
true
)
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
.
set
).
toEqual
([])
expect
(
data
.
originGlobalData
.
map
).
toEqual
({})
expect
(
data
.
originGlobalDataFuncRes
).
toBe
(
'
globalData func
'
)
await
page
.
callMethod
(
'
setGlobalData
'
)
await
page
.
callMethod
(
'
getGlobalData
'
)
data
=
await
page
.
data
()
expect
(
data
.
globalDataStr
).
toBe
(
'
new global data str
'
)
expect
(
data
.
globalDataNum
).
toBe
(
456
)
expect
(
data
.
globalDataBool
).
toBe
(
false
)
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
.
set
).
toEqual
([
'
a
'
,
'
b
'
,
'
c
'
])
expect
(
data
.
newGlobalData
.
map
).
toEqual
({
'
a
'
:
1
,
'
b
'
:
2
,
'
c
'
:
3
})
expect
(
data
.
newGlobalDataFuncRes
).
toBe
(
'
new globalData func
'
)
})
it
(
'
method
'
,
async
()
=>
{
const
oldLifeCycleNum
=
await
page
.
data
(
'
lifeCycleNum
'
)
...
...
pages/API/get-app/get-app.uvue
浏览文件 @
3331cf02
<template>
<view>
<page-head title="getApp"></page-head>
<view class="uni-padding-wrap">
<button @click="getGlobalData">get globalData</button>
<button @click="setGlobalData" class="uni-common-mt">set globalData</button>
<template v-if="globalDataStr">
<text class="uni-common-mt">globalData str: {{ globalDataStr }}</text>
<text class="uni-common-mt">globalData num: {{ globalDataNum }}</text>
<text class="uni-common-mt">globalData boolean: {{ globalDataBool }}</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">
increase lifeCycleNum
</button>
<text class="uni-common-mt">lifeCycleNum: {{ lifeCycleNum }}</text>
<!-- #ifdef APP -->
<scroll-view style="flex:1;padding-bottom: 20px;">
<!-- #endif -->
<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.set }}</text>
<text class="uni-common-mt">globalData Map: {{ originGlobalData.map }}</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.set }}</text>
<text class="uni-common-mt">globalData Map: {{ newGlobalData.map }}</text>
<text class="uni-common-mt">globalData func 返回值: {{ newGlobalDataFuncRes }}</text>
</template>
<view class="uni-common-mt hr"></view>
<text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
<text class="margin-top:6px;">increasetLifeCycleNum 方法</text>
<button class="uni-common-mt" @click="_increasetLifeCycleNum">
increase lifeCycleNum
</button>
<text class="uni-common-mt">lifeCycleNum: {{ lifeCycleNum }}</text>
</view>
</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[],
set : Set<string>,
map : Map<string, any>,
func : () => string
}
import { state, setLifeCycleNum } from '@/store/index.uts'
export default {
data() {
return {
globalDataStr: '',
globalDataNum: 0,
globalDataBool: false,
originGlobalData: {
str: '',
num: 0,
bool: false,
obj: {
str: '',
num: 0,
bool: false
} as UTSJSONObject,
null: null,
arr: [] as number[],
set: new Set<string>(),
map: new Map<string, any>(),
func: () : string => ''
} as MyGlobalData,
originGlobalDataFuncRes: '',
newGlobalData: {
str: '',
num: 0,
bool: false,
obj: {
str: '',
num: 0,
bool: false
} as UTSJSONObject,
null: null,
arr: [] as number[],
set: new Set<string>(),
map: new Map<string, any>(),
func: () : string => ''
} as MyGlobalData,
newGlobalDataFuncRes: '',
lifeCycleNum: 0,
}
},
...
...
@@ -37,15 +103,50 @@
methods: {
getGlobalData() {
const app = getApp()
this.globalDataStr = app.globalData.str
this.globalDataNum = app.globalData.num
this.globalDataBool = app.globalData.bool
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
this.originGlobalData.set = app.globalData.set
this.originGlobalData.map = app.globalData.map
this.originGlobalData.func = app.globalData.func
this.originGlobalDataFuncRes = this.originGlobalData.func()
},
setGlobalData() {
const app = getApp()
app.globalData.str = 'new global data str'
app.globalData.num = 456
app.globalData.bool = false
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.set = new Set(['a', 'b', 'c'])
app.globalData.map = 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
this.newGlobalData.set = app.globalData.set
this.newGlobalData.map = app.globalData.map
this.newGlobalData.func = app.globalData.func
this.newGlobalDataFuncRes = this.newGlobalData.func()
},
_increasetLifeCycleNum: function () {
const app = getApp()
...
...
@@ -58,4 +159,11 @@
}
},
}
</script>
</script>
<style>
.bold {
font-weight: bold;
}
.hr{border-bottom: 1px solid #ccc;}
</style>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录