vuex使用

在store下创建modules文件夹,在里面创建对应要存储的文件

// import api from '../../api/user'
const user = {
  namespaced: true, // 开启命名空间
  state: {
    img: '', // 用户头像
    id: '',
    account: '',
    nackname: '',
    email: '',
    region_id: '',
    realname: '',
    age: '',
    sex: '',
    address: '', // 与用户相关的活动
    role: ''
  },
  getters: {
    img: state => state.img,
    id: state => state.id,
    account: state => state.account,
    nackname: state => state.nackname,
    email: state => state.email,
    region_id: state => state.region_id,
    age: state => state.age,
    sex: state => state.sex,
    realname: state => state.realname,
    address: state => state.address,
    role: state => state.role
    // 是否已经报名该活动
    // isJoin: state => (activityId) => {
    //   const activities = state.activities.applyActivities
    //   let isJoin = false
    //   activities.forEach(element => {
    //     if (element.activity_id == activityId) {
    //       isJoin = true
    //     }
    //   })
    //   return isJoin
    // }

  },
  mutations: {
    SET_IMG: (state, img) => {
      state.img = img
    },
    SET_USERID: (state, id) => {
      state.id = id
    },
    SET_ACCOUNT: (state, account) => {
      state.account = account
    },
    SET_NACKNAME: (state, nackname) => {
      state.nackname = nackname
    },
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_AGE: (state, age) => {
      state.age = age
    },
    SET_SEX: (state, sex) => {
      state.sex = sex
    },
    SET_ADDRESS: (state, address) => {
      state.address = address
    },
    SET_ROLE: (state, role) => {
      state.role = role
    }
  },
  actions: {
    async logOut () {
      localStorage.removeItem('Token')
    }
    // 刷新活动
    // async updateActivities (context) {
    //   api.user.getUserActivities()
    //     .then(data => {
    //       delete data.ID
    //       delete data.user_id
    //       delete data.user_name
    //       context.commit('SET_ACTIVITIES', data)
    //     })
    // }
  }
}

export default user

然后在store的index文件里添加进去

import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
  },
  mutations: {
  },
  actions: {
  },
  modules: {
    user
  }
})

使用:
permission.js中使用案例

// 路由守卫
import router from './router'
import user from './api/user'
import store from './store'
// 白名单
const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  if (localStorage.getItem('Token')) {
    if (to.path === '/login') {
      next({
        path: '/'
      })
    } else {
      Promise.all([user.getUserInfo()]).then(res => {
        // console.log('wwwwee')
        console.log(res)
        store.commit('user/SET_IMG', res[0].data.img)
        store.commit('user/SET_USERID', res[0].data.id)
        store.commit('user/SET_NACKNAME', res[0].data.nackname)
        store.commit('user/SET_ADDRESS', res[0].data.address)
        store.commit('user/SET_AGE', res[0].data.age)
        store.commit('user/SET_SEX', res[0].data.sex)
        store.commit('user/SET_EMAIL', res[0].data.email)
        store.commit('user/SET_ROLE', res[0].data.role)

        next()
      })
    //   Promise.all([
    //     api.common.getOrganizations(),
    //     api.common.getCategories(),
    //     api.user.getUserInfo()
    //   ])
    //     .then(res => {
    //       // 更改 Vuex 的 store 中的状态的唯一方法是提交 mutation
    //       // 要唤醒一个 mutation handler,你需要以相应的 type 调用 store.commit 方法
    //       store.commit('common/SET_ORGANIZATIONS', res[0])
    //       store.commit('common/SET_CATEGORIES', res[1])
    //       store.commit('user/SET_USERID', res[2].user_id)
    //       store.commit('user/SET_USERNAME', res[2].user_name)
    //       store.commit('user/SET_ROLE', res[2].role)
    //       store.commit('user/SET_USERICON', res[2].icon)
    //       if (res.organization) {
    //         store.commit('user/SET_ORGANIZATION', res[2].organization)
    //       }
    //       next()
    //     })
    //     .catch(error => {
    //       console.log(error)
    //       store.dispatch('user/logOut').then(() => {
    //         next({ path: '/' })
    //       })
    //     })
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})

在main.js中添加

import store from './store'

目录结构如下:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值