原文
cnpm install vuex -D
cnpm install vuex --save
单一状态树
通常使用方法
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
| import Vue from vue import Vuex from vuex Vue.use(Vuex);
const data = { state : { count : 0, msg : "hello world" }, mutations : { increment(state,n){ console.log("运行到mutations"); state.count += n; } }, actions : { increment(state){ console.log("运行到actions"); state.commit('increment',10); } }, getters : { divinstion(state){ console.log("运行到getters"); return state.count / 10; } } } export default new Vuex.Store(data);
|
1 2 3 4 5 6 7
| import store from './store' new Vue({ router, store, render: h => h(App) }).$mount('#app')
|
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
| <template> <div class="hello"> <input type="button" value="btn" @click="increment"> <p>msg:{{msg}}</p> <p>division:{{division}}</p> </div> </template> <script> import {mapState,mapActions,mapGetters} from 'vuex'
export default{ methods:{ show(){ console.log("页面打开执行一次输出"); }, ...mapActions(['increment']), ...mapGetters(['division']), }, computed:{ ...mapState({ msg(state){ return state.count+1000; } }) }, created(){ this.show(); } } </script>
|
多模块分割
每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块
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
| import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex)
const data1 = { namespaced : true, state : { count : 0 }, mutations : { increment(state,n){ state.count += n; } }, actions : { increment(state){ commit('increment',10); } }, getters : { divinstion(state){ return state.count / 10; } } } const data2 = { namespaced : true, state : { msg : "hello world!" }, mutations : { increment(state){ state = state.msg.split('').reverse().join(''); } }, actions : { increment(state){ commit('increment'); } } } export default new Vuex.Store({ m1 : data1, m2 : data2 })
|
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
| <template> <div class="hello"> <input type="button" value="btn" @click="increment"> <p>{{msg}}</p> </div> </template> <script> import {mapState, mapActions,mapGetters} from 'vuex' export default { name: 'HelloWorld', computed : { ...mapState('m1',{ msg : state => state.count + 200, }), }, methods : { show(){ console.log("首页开始执行一次"); }, ...mapActions('m1',['increment']), }, created() { this.show(); }, } </script>
|
demo2
两个非常靠谱的方法:
- mapActions //管理所有事件
- mapGetters //获取数据
流程
event actions=>mutations(state)=>getters export default new Vuex.Store()
//App.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <template> <div id="app"> <input type="button" name="" value="增加" @click='increment'> <input type="button" name="" value="减少" @click='decrement'> <div> 当前数字为: {{ count }} </div> </div> </template> <script> import {mapActions,mapGetters} from 'vuex'; export default { methods:mapActions(['increment','decrement']), computed:mapGetters(['count']) } </script>
|
1 2 3 4 5 6 7
| import store from './store.js' new Vue({ el: '#app', render: h => h(App), store })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex);
import actions from './actions'; import mutations from './mutations';
export default new Vuex.Store({ modules:{ mutations }, actions })
|
1 2 3 4 5 6 7 8 9 10 11
| import types from './types';
export default ({ increment:({commit})=>{ commit("increment"); }, decrement:({commit})=>{ commit("decrement"); } })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import * as types from './types'; import getters from './getters'; const state = { count:10 } let mutations = { increment:(state)=>{ state.count++; }, decrement:(state)=>{ state.count--; } } export default ({ state, mutations, getters })
|
1 2 3 4
| export default ({ count:(state)=>state.count })
|
1 2
| export const [INCREMENT,DECREMENT] = ["INCREMENT","DECREMENT"];
|