Browse Source

添加vuex

master
spirit2020 3 years ago
parent
commit
e7b534cc6d
  1. 4
      App.vue
  2. 7
      main.js
  3. 11
      store/index.js
  4. 1
      store/modules/user/actions.js
  5. 17
      store/modules/user/index.js
  6. 30
      store/modules/user/mutations.js

4
App.vue

@ -1,6 +1,8 @@
<script>
export default {
onLaunch: function() {},
onLaunch: function() {
console.log(this.$store);
},
onShow: function() {},
onHide: function() {}
}

7
main.js

@ -4,14 +4,17 @@ import uView from 'uview-ui'
Vue.use(uView)
import Expands from '@/common/expands/index.js'
Vue.use(Expands)
Vue.use(Expands)
import store from '@/store/index.js'
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
...App
...App,
store
})
app.$mount()
// #endif

11
store/index.js

@ -0,0 +1,11 @@
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user/index.js'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
user
}
})

1
store/modules/user/actions.js

@ -0,0 +1 @@
export default {}

17
store/modules/user/index.js

@ -0,0 +1,17 @@
import actions from './actions.js'
import mutations from './mutations.js'
export default {
namespaced: true,
state: {
token: null,
userInfo: null,
config: null
},
getters: {
token: state => state.token || uni.getStorageSync('token'),
userInfo: state => state.userInfo || uni.getStorageSync('userInfo') || {},
config: state => state.config || uni.getStorageSync('config') || {}
},
actions,
mutations
}

30
store/modules/user/mutations.js

@ -0,0 +1,30 @@
export default {
SET_TOKEN(state, token) {
state.token = token
uni.setStorageSync('token', token)
},
REMOVE_TOKEN(state) {
state.token = null
uni.removeStorageSync('token')
},
SET_USER_INFO(state, userInfo) {
state.userInfo = userInfo
uni.setStorageSync('userInfo')
},
REMOVE_USER_INFO(state) {
state.userInfo = null
uni.removeStorageSync('userInfo')
},
SET_CONFIG(state, config) {
state.config = config
uni.setStorageSync('config')
},
GET_CONFIG(state) {
state.config = null
uni.removeStorageSync('config')
},
LOGOUT() {
this.commit('REMOVE_TOKEN')
this.commit('REMOVE_USER_INFO')
}
}
Loading…
Cancel
Save