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

feat: globalProperties

上级 2aa0c9f4
import App from './App.uvue'
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
import App from './App.uvue'
import { createSSRApp, reactive } from 'vue'
export function createApp() {
const app = createSSRApp(App)
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,
})
app.config.globalProperties.globalPropertiesFn = () : string => {
console.log('this.globalPropertiesStr', this.globalPropertiesStr)
console.log('this.globalPropertiesNum', this.globalPropertiesNum)
return `globalPropertiesStr: ${this.globalPropertiesStr}, globalPropertiesNum: ${this.globalPropertiesNum}`
}
return {
app
}
}
{
"pages": [
//pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
// pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
{
"path": "pages/tabBar/component",
"style": {
......@@ -389,6 +389,173 @@
"style": {
"navigationBarTitleText": "拦截器测试页面 2"
}
},
{
"path": "pages/API/request/request",
"style": {
"navigationBarTitleText": "request"
}
},
{
"path": "pages/API/upload-file/upload-file",
"style": {
"navigationBarTitleText": "upload-file"
}
},
{
"path": "pages/API/download-file/download-file",
"style": {
"navigationBarTitleText": "download-file"
}
},
{
"path": "pages/API/websocket-socketTask/websocket-socketTask",
"style": {
"navigationBarTitleText": "websocket-socketTask"
}
},
{
"path": "pages/API/websocket-global/websocket-global",
"style": {
"navigationBarTitleText": "websocket-global"
}
},
{
"path": "pages/API/unicloud-call-function/unicloud-call-function",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/API/unicloud-import-object/unicloud-import-object",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/API/get-system-info/get-system-info",
"style": {
"navigationBarTitleText": "get-system-info"
}
},
{
"path": "pages/API/get-device-info/get-device-info",
"style": {
"navigationBarTitleText": "get-device-info"
}
},
{
"path": "pages/API/get-app-base-info/get-app-base-info",
"style": {
"navigationBarTitleText": "get-app-base-info"
}
},
{
"path": "pages/API/get-system-setting/get-system-setting",
"style": {
"navigationBarTitleText": "get-system-setting"
}
},
{
"path": "pages/API/get-app-authorize-setting/get-app-authorize-setting",
"style": {
"navigationBarTitleText": "get-app-authorize-setting"
}
},
{
"path": "pages/API/preview-image/preview-image",
"style": {
"navigationBarTitleText": "图片预览"
}
},
{
"path": "pages/API/save-image-to-photos-album/save-image-to-photos-album",
"style": {
"navigationBarTitleText": "保存图片到相册"
}
},
{
"path": "pages/API/choose-image/choose-image",
"style": {
"navigationBarTitleText": "图片"
}
},
{
"path": "pages/API/get-network-type/get-network-type",
"style": {
"navigationBarTitleText": "get-network-type"
}
},
{
"path": "pages/API/page-scroll-to/page-scroll-to",
"style": {
"navigationBarTitleText": "pageScrollTo"
}
},
{
"path": "pages/API/event-bus/event-bus",
"style": {
"navigationBarTitleText": "event-bus"
}
},
{
"path": "pages/API/unicloud-file-api/unicloud-file-api",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/API/unicloud-database/unicloud-database",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/API/get-battery-info/get-battery-info",
"style": {
"navigationBarTitleText": "电量"
}
},
{
"path": "pages/API/get-window-info/get-window-info",
"style": {
"navigationBarTitleText": "get-window-info"
}
},
{
"path": "pages/API/facial-recognition-verify/facial-recognition-verify",
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false
}
},
{
"path": "pages/API/element-takesnapshot/element-takesnapshot",
"style": {
"navigationBarTitleText": "takeSnapshot",
"enablePullDownRefresh": false
}
},
{
"path": "pages/API/element-draw/element-draw",
"style": {
"navigationBarTitleText": "getDrawableContext",
"enablePullDownRefresh": false
}
},
{
"path": "pages/API/install-apk/install-apk",
"style": {
"navigationBarTitleText": "install-apk",
"enablePullDownRefresh": false
}
},
{
"path": "pages/API/globalProperties/globalProperties",
"style": {
"navigationBarTitleText": "globalProperties",
"enablePullDownRefresh": false
}
},
{
"path": "pages/tabBar/CSS",
......@@ -733,96 +900,6 @@
"navigationBarTitleText": "Rotate"
}
},
{
"path": "pages/API/request/request",
"style": {
"navigationBarTitleText": "request"
}
},
{
"path": "pages/API/upload-file/upload-file",
"style": {
"navigationBarTitleText": "upload-file"
}
},
{
"path": "pages/API/download-file/download-file",
"style": {
"navigationBarTitleText": "download-file"
}
},
{
"path": "pages/API/websocket-socketTask/websocket-socketTask",
"style": {
"navigationBarTitleText": "websocket-socketTask"
}
},
{
"path": "pages/API/websocket-global/websocket-global",
"style": {
"navigationBarTitleText": "websocket-global"
}
},
{
"path": "pages/API/unicloud-call-function/unicloud-call-function",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/API/unicloud-import-object/unicloud-import-object",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/API/get-system-info/get-system-info",
"style": {
"navigationBarTitleText": "get-system-info"
}
},
{
"path": "pages/API/get-device-info/get-device-info",
"style": {
"navigationBarTitleText": "get-device-info"
}
},
{
"path": "pages/API/get-app-base-info/get-app-base-info",
"style": {
"navigationBarTitleText": "get-app-base-info"
}
},
{
"path": "pages/API/get-system-setting/get-system-setting",
"style": {
"navigationBarTitleText": "get-system-setting"
}
},
{
"path": "pages/API/get-app-authorize-setting/get-app-authorize-setting",
"style": {
"navigationBarTitleText": "get-app-authorize-setting"
}
},
{
"path": "pages/API/preview-image/preview-image",
"style": {
"navigationBarTitleText": "图片预览"
}
},
{
"path": "pages/API/save-image-to-photos-album/save-image-to-photos-album",
"style": {
"navigationBarTitleText": "保存图片到相册"
}
},
{
"path": "pages/API/choose-image/choose-image",
"style": {
"navigationBarTitleText": "图片"
}
},
{
"path": "pages/component/scroll-view/scroll-view-refresher",
"style": {
......@@ -841,24 +918,6 @@
"navigationBarTitleText": "详情示例"
}
},
{
"path": "pages/API/get-network-type/get-network-type",
"style": {
"navigationBarTitleText": "get-network-type"
}
},
{
"path": "pages/API/page-scroll-to/page-scroll-to",
"style": {
"navigationBarTitleText": "pageScrollTo"
}
},
{
"path": "pages/API/event-bus/event-bus",
"style": {
"navigationBarTitleText": "event-bus"
}
},
{
"path": "pages/template/drop-card/drop-card",
"style": {
......@@ -895,12 +954,6 @@
"navigationBarTitleText": "scroll-view自定义滚动吸顶"
}
},
{
"path": "pages/API/get-battery-info/get-battery-info",
"style": {
"navigationBarTitleText": "电量"
}
},
{
"path": "pages/CSS/border/complex-border/complex-border",
"style": {
......@@ -926,12 +979,6 @@
"navigationBarTitleText": "自定义下拉刷新"
}
},
{
"path": "pages/API/get-window-info/get-window-info",
"style": {
"navigationBarTitleText": "get-window-info"
}
},
{
"path": "pages/template/half-screen/half-screen",
"style": {
......@@ -982,18 +1029,6 @@
"navigationStyle": "custom"
}
},
{
"path": "pages/API/unicloud-file-api/unicloud-file-api",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/API/unicloud-database/unicloud-database",
"style": {
"navigationBarTitleText": ""
}
},
{
"path": "pages/template/navbar-lite/navbar-lite",
"style": {
......@@ -1007,34 +1042,13 @@
"navigationBarTitleText": "自定义TabBar"
}
},
{
"path": "pages/API/facial-recognition-verify/facial-recognition-verify",
"style": {
"navigationBarTitleText": "",
"enablePullDownRefresh": false
}
},
{
"path": "pages/component/sticky-header/sticky-header",
"style": {
"navigationBarTitleText": "sticky-header",
"enablePullDownRefresh": false
}
},
{
"path": "pages/API/element-takesnapshot/element-takesnapshot",
"style": {
"navigationBarTitleText": "takeSnapshot",
"enablePullDownRefresh": false
}
},
{
"path": "pages/API/element-draw/element-draw",
"style": {
"navigationBarTitleText": "getDrawableContext",
"enablePullDownRefresh": false
}
},
},
{
"path": "pages/component/general-event/transition-event",
"style": {
......@@ -1042,13 +1056,6 @@
"enablePullDownRefresh": false
}
},
{
"path": "pages/API/install-apk/install-apk",
"style": {
"navigationBarTitleText": "install-apk",
"enablePullDownRefresh": false
}
},
{
"path": "pages/template/schema/schema",
"style": {
......@@ -1084,7 +1091,6 @@
"enablePullDownRefresh" : false
}
}
],
"globalStyle": {
"pageOrientation": "portrait",
......@@ -1135,4 +1141,4 @@
"query": "" //启动参数,在页面的onLoad函数里面得到
}]
}
}
\ No newline at end of file
}
const PAGE_PATH = '/pages/API/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 () => {
const image = await program.screenshot({fullPage: true});
expect(image).toMatchImageSnapshot();
})
})
<template>
<!-- #ifdef APP -->
<scroll-view style="flex:1;padding-bottom: 20px;">
<!-- #endif -->
<view>
<page-head title="getApp"></page-head>
<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: Set<string>;
map: Map<string, number>;
reactiveObj: UTSJSONObject;
}
export default {
data() {
return {
myGlobalProperties: {
str: '',
num: 0,
bool: false,
obj: {},
null: null,
arr: [],
set: new Set<string>(),
map: new Map<string, number>(),
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
this.myGlobalProperties.map = this.globalPropertiesMap
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>
......@@ -63,9 +63,13 @@
name: 'getApp',
url: 'get-app',
},
{
name: 'getCurrentPages',
url: 'get-current-pages',
{
name: 'getCurrentPages',
url: 'get-current-pages',
},
{
name: 'globalProperties',
url: 'globalProperties',
}
] as Page[],
},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册