uniapp状态管理
侧边栏壁纸
博主昵称
404

  • 累计撰写 17 篇文章
  • 累计收到 0 条评论
Vue

uniapp状态管理

404
404
2022-07-29 / 0 评论 / 23 阅读 / 正在检测是否收录...
/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

评论 (0)

取消