NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
~~~ import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) // 相当于data对象的状态对象 const state = { count: 0 //指定初始化数据 } // 包含多个更新状态的方法的对象 const mutations = { INCREMENT (state) { state.count ++ }, DECREMENT (state) { state.count -- } } // 包含多个间接更新状态的方法的对象 const actions = { increment ({commit}) { commit('INCREMENT') }, decrement ({commit}) { commit('DECREMENT') }, incrementIfOdd ({commit, state}) { if(state.count%2===1){ commit('INCREMENT') } }, incrementAsync ({commit}) { setTimeout(()=>{ commit('INCREMENT') }, 1000) } } // 包含多个getters计算属性的对象 const getters = { evenOrOdd (state) { return state.count % 2 ? '偶数' : '奇数' } } export default new Vuex.Store({ state, mutations, actions, getters }) ~~~