store里的几个文件
假如想保存用户vip状态,
- actions.js
const actions = {
changeIsVip(ctx,isVip){
ctx.commit('changeIsVip',isVip)
}
}
export default actions
- mutations.js
const mutations = {
changeIsVip(state,isVip){
state.isVip = isVip
}
}
export default mutations
- index.js
import Vue from 'vue'
import Vuex from 'vuex'
import actions from './actions'
import getters from './getters'
import state from './state'
import mutations from './mutations'
import createLogger from 'vuex/dist/logger'
Vue.use(Vuex)
const debug = process.env.NODE_ENV !== 'production'
export default new Vuex.Store({
actions,
getters,
state,
mutations,
strict: debug,
plugins: debug ? [createLogger()] : []
})
- getters.js
const getters = {
userInfo(state){
return JSON.parse(localStorage.getItem('userInfo'))
}
js中
this.$store.dispatch('changeIsVip',res.obj.isVip)