Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

vue-element-admin左侧菜单怎么动态渲染?/api中的接口不可以用,要用另一个接口不需要token

import { constantRoutes } from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, data) {
    return new Promise(resolve => {
      const accessedRoutes = initMenus(data)
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

const initMenus = (routes) => {
  const menus = loadMenus(routes)
  menus.push({ path: '*', redirect: '/404', hidden: true })
  return menus
}

const loadMenus = (routes) => {
  const menus = []
  routes.forEach(route => {
    const menu = loadMenu(route)
    menus.push(menu)
  })
  return menus
}

const loadMenu = (route) => {
  let _route = {}
  if (route.parentId !== '0') { // 非一级菜单
    _route = {
      path: route.path,
      // component: () => import(`@/views/${route.component}.vue`),
      component: () =>Promise.resolve(require(`@/views/${route.component}.vue`).default), // --no-ignore
      name: route.menuName,
      meta: { title: route.title, icon: route.icon, noCache: route.noCache === '1' }
    }
    if (route.children.length !== 0) {
      _route.redirect = route.children[0].path
      _route.children = loadMenus(route.children)
    }
  } else {
    _route = {
      path: route.path,
      component: () => import('@/layout'),
      // alwaysShow: true,
      redirect: route.children[0].path,
      meta: { title: route.title, icon: route.icon, noCache: route.noCache === '1' },
      children: loadMenus(route.children)
    }
  }
  return _route
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

估计这种问题没人回答吧,都不知道想问什么,大概猜下楼主的问题
左侧菜单渲染:根据当前用户权限,过滤当前用户router
token:如果接口需要token,可以把登陆后的token存到request header里面,封装一个公用的request出来,所有的接口都可以使用token

request.interceptors.request.use(function (config) {
    // Do something before request is sent
    config.headers['Authorization'] = getToken()
    return config
  }, function (error) {
    // Do something with request error
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000   // 显示时间, 毫秒。设为 0 则不会自动关闭
    })
    return Promise.reject(error)
  })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...