开启严格模式(!!!不要在发布环境下启用严格模式)
开启严格模式,store 的 strict 属性设置为 true: const store = new Vuex.Store({ // ... strict: true}); 严格模式下的状态发生变化且不是由mutation触发,将会抛出错误。保障所有状态的改变会被调试工具检测到。仅在开发环境下使用严格模式,生产环境下应关闭严格模式,减少性能的消耗。 使用构建工具如下配置(开发环境下使用严格模式,生产环境下切换到非严格模式):
const store = new Vuex.Store({ // ... strict:process.env.NODE_ENV !== 'production' });
Action
1、Action 用于提交 mutation,修改state的值,而非直接修改;2、Action 用于处理各种异步操作的方法 const store = new Vuex.Store({
state: { list: [], name:'', }, mutations: { editList(state , params) { state.list = params; }, editName(state , params){ state.name = params; } }, actions: { actEditList(context) { context.commit('eidtList' , [1,2,3]); }, getInfo({ commit },{ name }){ commit('eidtName',name); } } }); 3、Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象( store 的镜像,而非 store 本身),可以调用 context.commit 提交一个 mutation,或者通过 context.state 和 context.getters 来获取 state 和 getters。 4、Action 函数的参数可使用 ES2015 的解构赋值语法:
actions: { actEditList( {commit} ) { commit('eidtList' , [1,2,3]); } } 5、组件中使用 Action
import { mapActions } from 'vuex'
export default{ methods:{ ...mapActions([ 'actEditList', // 在此组件中可使用 this.actEditList() 调用; ]), ...mapActions({ get:'getInfo', // 在此组件中可使用 this.get() 调用; }), }, }