写的一手烂笔记之后发现 其实vuex 官网写得真的挺好的, 感觉相比其他官网,写得还算是人话。 vuex官网
除了引用 store.js 用 $store.state 和 $store.commit() 这两种以外
还可以有另外一种引用方式. 可读性更好一点
先记下来…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import vue from "vue";
import Vuex from "vuex";
vue.use(Vuex);
export default new Vuex.Store({
state : {
info : "欢迎来到课堂"
},
mutations : {
changeInfo (state) {
state.info = "welcome to class"
}
}
});
App.vue
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
<template>
<div id="app">
<div class="wrapper">
<div></div>
</div>
<button @click="change">change</button>
</div>
</template>
<script>
import {mapState,mapMutations} from "vuex"
export default {
name : "App",
computed : {
...mapState({
myInfo : state => state.info
})
},
methods : {
...mapMutations({
change : 'changeInfo'
})
}
}
</script>
<style>
</style>