Skip to content

Commit 6fa463f

Browse files
committed
补充globalData和vuex的测试例
1 parent 158c72b commit 6fa463f

5 files changed

Lines changed: 93 additions & 9 deletions

File tree

‎App.vue‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
},
4444
onHide: function() {
4545
console.log('App Hide')
46-
}
46+
},
47+
globalData: {
48+
test: ''
49+
}
4750
}
4851
</script>
4952

‎pages.json‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"text": "\ue534",
1313
"fontSrc": "/static/uni.ttf",
1414
"fontSize": "22px",
15-
"color": "#999999"
15+
"color": "#FFFFFF"
1616
}]
1717
}
1818
}
@@ -28,7 +28,7 @@
2828
"text": "\ue534",
2929
"fontSrc": "/static/uni.ttf",
3030
"fontSize": "22px",
31-
"color": "#999999"
31+
"color": "#FFFFFF"
3232
}]
3333
}
3434
}
@@ -44,7 +44,7 @@
4444
"text": "\ue534",
4545
"fontSrc": "/static/uni.ttf",
4646
"fontSize": "22px",
47-
"color": "#999999"
47+
"color": "#FFFFFF"
4848
}]
4949
}
5050
}
@@ -60,7 +60,7 @@
6060
"text": "\ue534",
6161
"fontSrc": "/static/uni.ttf",
6262
"fontSize": "22px",
63-
"color": "#999999"
63+
"color": "#FFFFFF"
6464
}]
6565
}
6666
}
@@ -1159,14 +1159,19 @@
11591159
"navigationBarTitleText": "表单校验"
11601160
}
11611161
}
1162-
1162+
,{
1163+
"path" : "global/global",
1164+
"style" : {
1165+
"navigationBarTitleText": "GlobalData和vuex"
1166+
}
1167+
}
11631168
]
11641169
}
11651170
],
11661171
"globalStyle": {
1167-
"navigationBarTextStyle": "black",
1172+
"navigationBarTextStyle": "white",
11681173
"navigationBarTitleText": "Hello uniapp",
1169-
"navigationBarBackgroundColor": "#F8F8F8",
1174+
"navigationBarBackgroundColor": "#007AFF",
11701175
"backgroundColor": "#F8F8F8",
11711176
"backgroundColorTop": "#F4F5F6",
11721177
"backgroundColorBottom": "#F4F5F6",

‎pages/tabBar/template/template.nvue‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@
154154
}, {
155155
name: '表单验证',
156156
url: 'datachecker'
157+
}, {
158+
name: 'GlobalData和vuex',
159+
url: 'global'
157160
},
158161
// #ifdef APP-PLUS
159162
{

‎pages/template/global/global.vue‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<template>
2+
<view class="pa">
3+
<view class="uni-divider">
4+
<view class="uni-divider__content">globalData</view>
5+
<view class="uni-divider__line"></view>
6+
</view>
7+
<text class="text">globalData中text的值: {{gd.test}}</text>
8+
<button @click="setGD()" class="button">修改上述值为123</button>
9+
10+
<view class="uni-divider">
11+
<view class="uni-divider__content">vuex</view>
12+
<view class="uni-divider__line"></view>
13+
</view>
14+
<text class="text">vuex中hasLogin的值: {{testvuex}}</text>
15+
<button @click="setVUEX(true)" class="button">修改上述值为true</button>
16+
<button @click="setVUEX(false)" class="button">修改上述值为false</button>
17+
</view>
18+
</template>
19+
20+
<script>
21+
import {
22+
mapState,
23+
mapMutations
24+
} from 'vuex'
25+
export default {
26+
data() {
27+
return {
28+
gd:{}
29+
}
30+
},
31+
computed: {
32+
...mapState(['testvuex'])
33+
},
34+
methods: {
35+
...mapMutations(['setTestTrue']),
36+
...mapMutations(['setTestFalse']),
37+
setGD:function () {
38+
getApp().globalData.test="123"
39+
},
40+
setVUEX:function (isTrue) {
41+
// console.log("this.testvuex: " + this.testvuex);
42+
// this.hasLogin = true; 这样赋值不生效,必须用store/index.js里注册的mapMutations才行
43+
if(isTrue){
44+
this.setTestTrue(this.$store.state);
45+
}
46+
else{
47+
this.setTestFalse(this.$store.state);
48+
}
49+
// console.log("this.testvuex: " + this.testvuex);
50+
}
51+
},
52+
onShow() {
53+
this.gd = getApp().globalData
54+
}
55+
}
56+
</script>
57+
58+
<style>
59+
.button {
60+
margin: 30rpx;
61+
color: #007AFF;
62+
}
63+
.text{
64+
margin-left: 30rpx;
65+
}
66+
</style>

‎store/index.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const store = new Vuex.Store({
77
state: {
88
hasLogin: false,
99
loginProvider: "",
10-
openid: null
10+
openid: null,
11+
testvuex:false
1112
},
1213
mutations: {
1314
login(state, provider) {
@@ -20,6 +21,12 @@ const store = new Vuex.Store({
2021
},
2122
setOpenid(state, openid) {
2223
state.openid = openid
24+
},
25+
setTestTrue(state){
26+
state.testvuex = true
27+
},
28+
setTestFalse(state){
29+
state.testvuex = false
2330
}
2431
},
2532
actions: {

0 commit comments

Comments
 (0)