import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const state = {
  userInfo: null || JSON.parse( localStorage.getItem('userInfo') )
}

const actions = {}

const mutations = {
  SET_USERINFO: ( state, userInfo ) => {
    state.userInfo = userInfo;
    // 防止页面刷新，导致Vuex重启
    localStorage.setItem('userInfo', JSON.stringify(userInfo));
  }
}

const getters = {
  userInfo: ( state ) => {
    return state.userInfo
  }
}

const store = new Vuex.Store({
  actions,
  mutations,
  state,
  getters
})

export default store;
