中文文檔:vuex官方中文網站
一、vuex里面都有些什么內容?
const store = new Vuex.Store({ state: { name: 'weish', age: 22 }, getters: { personInfo(state) { return `My name is ${state.name}, I am ${state.age}`; } } mutations: { SET_AGE(state, age) { commit(age, age); } }, actions: { nameAsyn({commit}) { setTimeout(() => { commit('SET_AGE', 18); }, 1000); } }, modules: { a: modulesA } }
這個就是最基本也是完整的vuex代碼:
vuex 包含有五個基本的對象:
1、state:存儲狀態,也就是全局變量;
2、getters:派生狀態。也就是set、get中的get,有兩個可選參數:state、getters,分別可以獲取state中的變量和其他的getters。外部調用方式:store.getters.personInfo(),就和vue的computed差不多;
3、mutations:提交狀態修改。也就是set、get中的set,這是vuex中唯一修改state的方式,但不支持異步操作。第一個參數默認是state,外部調用方式:store.commit('SET_AGE', 18),和vue中的methods類似。
4、actions:和mutations類似,不過actions支持異步操作。第一個參數默認是和store具有相同參數屬性的對象。外部調用方式:store.dispatch('nameAsyn')。
5、modules:store的子模塊,內容就相當於是store的一個實例。調用方式和前面介紹的相似,只是要加上當前子模塊名,如:store.a.getters.xxx()。
二、vue-cli中使用vuex的方式
一般來講,我們都會采用vue-cli來進行實際的開發,在vue-cli中,開發和調用方式稍微不同。
├── index.html ├── main.js ├── components └── store ├── index.js # 我們組裝模塊並導出 store 的地方 ├── state.js # 根級別的 state ├── getters.js # 根級別的 getter ├── mutation-types.js # 根級別的mutations名稱(官方推薦mutions方法名使用大寫) ├── mutations.js # 根級別的 mutation ├── actions.js # 根級別的 action └── modules ├── m1.js # 模塊1 └── m2.js # 模塊2
state.js示例:
const state = { name: 'weish', age: 22 }; export default state;
getters.js示例(我們一般使用getters來獲取state的狀態,而不是直接使用state):
export const name = (state) => { return state.name; } export const age = (state) => { return state.age } export const other = (state) => { return `My name is ${state.name}, I am ${state.age}.`; }
mutation-type.js示例(我們會將所有mutations的函數名放在這個文件里):
export const SET_NAME = 'SET_NAME'; export const SET_AGE = 'SET_AGE';
mutations.js示例:
import * as types from './mutation-type.js'; export default { [types.SET_NAME](state, name) { state.name = name; }, [types.SET_AGE](state, age) { state.age = age; } };
actions.js示例(異步操作、多個commit時):
import * as types from './mutation-type.js'; export default { nameAsyn({commit}, {age, name}) { commit(types.SET_NAME, name); commit(types.SET_AGE, age); } };
modules--m1.js示例(如果不是很復雜的應用,一般來講是不會分模塊的):
export default { state: {}, getters: {}, mutations: {}, actions: {} };
index.js示例(組裝vuex):
import vue from 'vue'; import vuex from 'vuex'; import state from './state.js'; import * as getters from './getters.js'; import mutations from './mutations.js'; import actions from './actions.js'; import m1 from './modules/m1.js'; import m2 from './modules/m2.js'; import createLogger from 'vuex/dist/logger'; // 修改日志
vue.use(vuex); const debug = process.env.NODE_ENV !== 'production'; // 開發環境中為true,否則為false
export default new vuex.Store({ state, getters, mutations, actions, modules: { m1, m2 }, plugins: debug ? [createLogger()] : [] // 開發環境下顯示vuex的狀態修改
});
最后將store實例掛載到main.js里面的vue上去就行了
import store from './store/index.js'; new Vue({ el: '#app', store, render: h => h(App) });
在vue組件中使用時,我們通常會使用mapGetters、mapActions、mapMutations,然后就可以按照vue調用methods和computed的方式去調用這些變量或函數,示例如下:
import {mapGetters, mapMutations, mapActions} from 'vuex'; /* 只寫組件中的script部分 */ export default { computed: { ...mapGetters([ name, age ]) }, methods: { ...mapMutations({ setName: 'SET_NAME', setAge: 'SET_AGE' }), ...mapActions([ nameAsyn ]) } };
以上就是 vuex 的相關知識,其實vuex很簡單,多用幾次就會熟悉了。