/common/store.js
//状态管理
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{
user_id: uni.getStorageInfoSync('user_id'),
username: uni.getStorageInfoSync('username'),
token: uni.getStorageInfoSync('token'),
},
mutations: {
SETUSER_ID(state, data){
state.user_id = data
uni.setStorageSync('user_id', data)
},
SETUSERNAME(state, data){
state.username = data
uni.setStorageSync('username', data)
},
SETTOKEN(state, data){
state.token = data
uni.setStorageSync('token', data)
},
LOGOUT(state){
state.user_id = null
state.username = null
state.token = null
uni.removeStorageSync('user_id')
uni.removeStorageSync('username')
uni.removeStorageSync('token')
}
}
})
/main.js
import store from './common/store.js'
Vue.prototype.$store = store
使用
this.$store.commit('SETUSERNAME',this.phoneData)
this.$store.commit('SETTOKEN',res.data.data)
this.$store.commit('LOGOUT')
取值
this.$store.state.username
评论 (0)