码迷,mamicode.com
首页 > 其他好文 > 详细

umi 动态路由配置

时间:2019-12-22 12:43:32      阅读:1446      评论:0      收藏:0      [点我收藏+]

标签:reac   user   event   syn   model   data   amp   auth   erro   

import { getUserPerm } from ‘@/services/menu‘;
import router from ‘umi/router‘;
let authRoutes = [];
export const dva = {
  config: {
    onError(err) {
      err.preventDefault();
      console.error(err.message);
    },
  },
};

function filterRoute(routes,authRoute) {
  const arr = [];
  authRoute.forEach(item => {
    routes.forEach(route => {
      if (route.path === item.path) {
        let obj = {};
        if (route.routes && item.routes) {
          obj = route;
          obj.routes = (filterRoute(route.routes,item.routes));
          arr.push(obj);
        } else {
          obj = route;
          arr.push(obj);
        }
      }
    })
  })
  return arr;
}
export function patchRoutes(routes) {
 routes[1].routes = filterRoute(routes[1].routes,authRoutes)
  window.g_routes = routes;
}
export async function  render(oldRender) {
  const authRoutesResult = await getUserPerm();
  window.oldRender = () => {
    if (authRoutesResult && authRoutesResult.code === 0) {
      authRoutes = authRoutesResult.data;
    } else {
      oldRender();
    }
    oldRender();
  }
  if (window.oldRender) {
    window.oldRender();
  }
}

在model里登陆和登出需要window.location.reload 刷新页面。

umi 动态路由配置

标签:reac   user   event   syn   model   data   amp   auth   erro   

原文地址:https://www.cnblogs.com/l8l8/p/12079254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!