import * as types from './mutation-types' const matations={ /** * state:当前状态树 * data: 提交matations时传的参数 */ //是否有渠道 [types.TEST] (state,data) { state.TEST = data; }, } export default matations
6、使用方法
# 在 store index.js 中引入 import Vue from 'vue'; import Vuex from 'vuex'; import state from './state' import mutations from './mutations' Vue.use(Vuex); export default new Vuex.Store({ state, mutations, })
在页面中引用
7、将vuex中的数据持久化到本地 (使用vuex-persistedstate)
# 安装vuex-persistedstate $ npm install vuex-persistedstate --save
在 store index.js 引入
import Vue from 'vue'; import Vuex from 'vuex'; import state from './state' import mutations from './mutations' import createPersistedState from 'vuex-persistedstate' Vue.use(Vuex); export default new Vuex.Store({ state, mutations, plugins: [ createPersistedState({ storage: { getItem: key => wx.getStorageSync(key), setItem: (key, value) => wx.setStorageSync(key, value), removeItem: key => {} } }) ] })
demo地址 :https://github.com/ccwyn/mpvuedemo/tree/master/my-project