vue路由守卫

创建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 '@/permission'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值