标签:put 简单 执行 use username computed new pre cookie
简单记录一下Vuex使用
当我们的多个vue实例(页面)或者组件绑定了同一个变量时,我们应该怎么处理?
declare const _default: {
Store: typeof Store;
install: typeof install;
mapState: typeof mapState,
mapMutations: typeof mapMutations,
mapGetters: typeof mapGetters,
mapActions: typeof mapActions,
createNamespacedHelpers: typeof createNamespacedHelpers,
};
export default _default;
import Vue from ‘vue‘
import Vuex from ‘vuex‘
import createPersistedState from ‘vuex-persistedstate‘
import { login } from ‘../api/auth/login‘
import { setCookie } from ‘../libs/cookie‘
import md5 from ‘../libs/md5‘
Vue.use(Vuex)
export default new Vuex.Store({
state: {
user: {
uuid: ‘‘,
username: ‘xxx‘,
nickname: ‘developer‘,
headImage: ‘‘,
auth: []
}
},
mutations: {
setUserInfo(state, { uuid, username, nickname }) {
state.user = Object.assign(state.user, { uuid, username, nickname })
}
},
actions: {
async login({ commit, state }, { username, password }) {
const inputInfo = { username: username, password: md5(password) }
const ret = await login(inputInfo)
if (ret && ret.uuid) {
commit(‘setUserInfo‘, ret)
setCookie(‘token‘, ret.token)
return true
}
return false
}
},
plugins: [createPersistedState({ // 避免页面刷新后重新初始化state
storage: window.localStorage
})]
})
标签:put 简单 执行 use username computed new pre cookie
原文地址:https://www.cnblogs.com/xpengp/p/13280723.html