-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVuexDemo.html
71 lines (63 loc) · 1.8 KB
/
VuexDemo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Vuex demo</title>
</head>
<body>
<div id="app">
<p>{{ count }}-{{ returnVuexStoreMsg }}</p>
<p>
<button @click="increment">+</button>
<button @click="decrement">-</button>
<button @click="changeMsg">change</button>
</p>
</div>
</body>
<!-- 先引入 Vue 【https://unpkg.com/[email protected]/dist/vue.js】 -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!--<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>-->
<!--引入vuex 【https://unpkg.com/[email protected]/dist/vuex.js】-->
<script src="https://unpkg.com/vuex"></script>
<script>
//创建store
const store = new Vuex.Store({
state: {
count: 0,
vuexStoreMsg: 'LC'
},
mutations: {
increment: state => state.count++,
decrement: state => state.count--,
changeMsg: state => state.vuexStoreMsg = 'hello,LC-' + new Date()
}
})
new Vue({
el: '#app',
computed: {
count() {
//通过computed 引用store
return store.state.count
},
returnVuexStoreMsg() {
//通过computed 引用store
return store.state.vuexStoreMsg
}
},
methods: {
increment() {
//通过commit执行变更
store.commit('increment')
},
decrement() {
store.commit('decrement')
},
changeMsg() {
store.commit('changeMsg')
}
}
}).$mount('#app')
</script>
</html>