vue-admin-element后台获取菜单动态渲染路由

思路:用户登录成功后获取token根据token去获取用户信息和路由信息,由于每次用户刷新界面动态存储在vuex的路由就会消失,所以我们要在路由拦截那一块处理一下;

1:通过路由钩子函数beforeEach中去判断如果用户数据丢失(及用户名不显示)去掉用户信息接口和路由接口;

 await store.dispatch('user/getInfo');

具体代码;

 // 获取异步用户信息
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getApi().then(response => {
        const { resp } = response;
        commit('SET_NAME', resp.username);
        resolve(resp);
      }).catch(error => {
        reject(error);
      });
    });
  },

2:拿到用户信息和token这些,就可以去拿路由数据了

 const asyncRoutes = await store.dispatch('GenerateRoutes');

具体代码

  GenerateRoutes({ commit }) {
      return new Promise(resolve => {
         向后端请求路由数据
         getRouters().then(res => {
         const accessedRoutes = filterAsyncRouter(res.data);
         console.log(accessedRoutes);
         accessedRoutes.push({ path: '*', redirect: '/404', hidden: true });
         commit('SET_ROUTES', accessedRoutes);
         resolve(accessedRoutes);
         });
      });
    }

2-1 filterAsyncRouter处理后台返回json路由转为前端能使用的路由 具体代码如下

// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
  return asyncRouterMap.filter(route => {
    if (route.component) {
      // Layout组件特殊处理
      if (route.component === 'Layout') {
        route.component = Layout;
      } else {
        route.component = loadView(route.component);
      }
    }
    if (route.children != null && route.children && route.children.length) {
      route.children = filterAsyncRouter(route.children);
    }
    return true;
  });
}

3:拿到步骤2返回的路由信息添加到到自己的路由中

router.addRoutes(asyncRoutes);

4:刷新重载路由

next({ ...to, replace: true });

完整permmison.js代码如下

import router from './router';
import store from './store';
import { Message } from 'element-ui';
import { getToken } from '@/utils/auth';
import getPageTitle from '@/utils/get-page-title';

const whiteList = ['/login', '/forget'];

router.beforeEach(async(to, from, next) => {
  document.title = getPageTitle(to.meta.title);
  const hasToken = getToken();
  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' });
    } else {
      const hasGetUserInfo = store.getters.name;
      if (hasGetUserInfo) {
        // 路由跳转name不匹配的情况,通过path跳转不需判断
        if (to.matched.length === 0) {
          next('/404');
        } else {
          next();
        }
      } else {
        try {
          await store.dispatch('user/getInfo');
          const asyncRoutes = await store.dispatch('GenerateRoutes');
          router.addRoutes(asyncRoutes);
          next({ ...to, replace: true });
        } catch (error) {
          await store.dispatch('user/resetToken');
          Message.error(error || '获取用户信息失败');
          next('/login');
        }
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
       next(`/login`);
    }
  }
});

router.afterEach(() => {
});

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值