溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue實現(xiàn)動態(tài)路由的操作過程

發(fā)布時間:2021-10-09 09:12:36 來源:億速云 閱讀:135 作者:柒染 欄目:開發(fā)技術(shù)

本篇文章為大家展示了vue實現(xiàn)動態(tài)路由的操作過程,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

簡單聊一下兩種方式的優(yōu)勢,畢竟如果你從來沒做過,說再多也看不明白,還是得看代碼

前端控制

  • 不用后端幫助,路由表維護在前端

  • 邏輯相對比較簡單,比較容易上手

后端控制

  • 相對更安全一點

  • 路由表維護在數(shù)據(jù)庫

一、前端控制

思路:在路由配置里,通過meta屬性,擴展權(quán)限相關(guān)的字段,在路由守衛(wèi)里通過判斷這個權(quán)限標識,實現(xiàn)路由的動態(tài)增加,及頁面跳轉(zhuǎn);如:我們增加一個role字段來控制角色

具體方案:

1、根據(jù)登錄用戶的賬號,返回前端用戶的角色

2、前端根據(jù)用戶的角色,跟路由表的meta.role進行匹配

3、講匹配到的路由形成可訪問路由

核心代碼邏輯

1、在router.js文件(把靜態(tài)路由和動態(tài)路由分別寫在router.js)

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

// constantRoutes  靜態(tài)路由,主要是登錄頁、404頁等不需要動態(tài)的路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  }
] 

// asyncRoutes 動態(tài)路由
export const asyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    redirect: '/permission/page',
    alwaysShow: true, 
    name: 'Permission',
    meta: {
      title: 'Permission',
      icon: 'lock',
      // 核心代碼,可以通過配的角色來進行遍歷,從而是否展示
      // 這個意思就是admin、editor這兩個角色,這個菜單是可以顯示
      roles: ['admin', 'editor']
    },
    children: [
      {
        path: 'page',
        component: () => import('@/views/permission/page'),
        name: 'PagePermission',
        meta: {
          title: 'Page Permission',
          // 這個意思就是只有admin能展示
          roles: ['admin']
        }
      }
     ]
    }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 這個是重置路由用的,很有用,別看這么幾行代碼
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher 
}

export default router

2、store/permission.js(在vuex維護一個state,通過配角色來控制菜單顯不顯示)

import { asyncRoutes, constantRoutes } from '@/router'

// 這個方法是用來把角色和route.meta.role來進行匹配
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}


// 這個方法是通過遞歸來遍歷路由,把有權(quán)限的路由給遍歷出來
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) => {
    // 這個地方維護了兩個狀態(tài)一個是addRouters,一個是routes
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes
      if (roles.includes('admin')) {
        accessedRoutes = asyncRoutes || []
      } else {
        // 核心代碼,把路由和獲取到的角色(后臺獲取的)傳進去進行匹配
        accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      }
      // 把匹配完有權(quán)限的路由給set到vuex里面
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

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

3、src/permission.js

(新建一個路由守衛(wèi)函數(shù),可以在main.js,也可以抽離出來一個文件)

這里面的代碼主要是控制路由跳轉(zhuǎn)之前,先查一下有哪些可訪問的路由,登錄以后跳轉(zhuǎn)的邏輯可以在這個地方寫

// permission.js
router.beforeEach((to, from, next) => {
  if (store.getters.token) { // 判斷是否有token
    if (to.path === '/login') {
      next({ path: '/' });
    } else {
        // 判斷當前用戶是否已拉取完user_info信息
      if (store.getters.roles.length === 0) {
        store.dispatch('GetInfo').then(res => { // 拉取info
          const roles = res.data.role;
          // 把獲取到的role傳進去進行匹配,生成可以訪問的路由
          store.dispatch('GenerateRoutes', { roles }).then(() => { 
            // 動態(tài)添加可訪問路由表(核心代碼,沒有它啥也干不了)
            router.addRoutes(store.getters.addRouters)
            
            // hack方法 確保addRoutes已完成
            next({ ...to, replace: true })
          })
        }).catch(err => {
          console.log(err);
        });
      } else {
        next() //當有用戶權(quán)限的時候,說明所有可訪問路由已生成 如訪問沒權(quán)限的全面會自動進入404頁面
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) { // 在免登錄白名單,直接進入
      next();
    } else {
      next('/login'); // 否則全部重定向到登錄頁
    }
  }
})

4、側(cè)邊欄的可以從vuex里面取數(shù)據(jù)來進行渲染

核心代碼是從router取可以用的路由對象,來進行側(cè)邊欄的渲染,不管是前端動態(tài)加載還是后端動態(tài)加載路由,這個代碼都是一樣的

<!-- layout/components/siderbar.vue -->
<el-menu
:default-active="activeMenu"
:collapse="isCollapse"
:background-color="variables.menuBg"
:text-color="variables.menuText"
:unique-opened="false"
:active-text-color="variables.menuActiveText"
:collapse-transition="false"
mode="vertical"
>
    // 把取到的路由進行循環(huán)作為參數(shù)傳給子組件
    <sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
</el-menu>
// 獲取有權(quán)限的路由
routes() {
  return this.$router.options.routes
}


<!-- layout/components/siderbarItem.vue -->
  <template slot="title">
    <item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title" />
  </template>
  <sidebar-item
    v-for="child in item.children"
    :key="child.path"
    :is-nest="true"
    :item="child"
    :base-path="resolvePath(child.path)"
    class="nest-menu"
  />

  props: {
    // route object
    item: {
      type: Object,
      required: true
    },
    isNest: {
      type: Boolean,
      default: false
    },
    basePath: {
      type: String,
      default: ''
    }
  }

前端控制路由,邏輯相對簡單,后端只需要存這個用戶的角色就可以了,前端拿用戶的角色進行匹配。但是如果新增角色,就會非常痛苦,每一個都要加。

二、后端控制路由

后端控制大致思路是:路由配置放在數(shù)據(jù)庫表里,用戶登錄成功后,根據(jù)角色權(quán)限,把有權(quán)限的菜單傳給前端,前端格式化成頁面路由識別的結(jié)構(gòu),再渲染到頁面菜單上;

  • 用戶登錄以后,后端根據(jù)該用戶的角色,直接生成可訪問的路由數(shù)據(jù),注意這個地方是數(shù)據(jù)

  • 前端根據(jù)后端返回的路由數(shù)據(jù),轉(zhuǎn)成自己需要的路由結(jié)構(gòu)

具體邏輯:

  • router.js里面只放一些靜態(tài)的路由,login、404之類

  • 整理一份數(shù)據(jù)結(jié)構(gòu),存到表里

  • 從后端獲取路由數(shù)據(jù),寫一個數(shù)據(jù)轉(zhuǎn)換的方法,講數(shù)據(jù)轉(zhuǎn)成可訪問的路由

  • 也是維護一個vuex狀態(tài),將轉(zhuǎn)換好的路由存到vuex里面

  • 側(cè)邊欄也是從路由取數(shù)據(jù)進行渲染

因為前段控制和后端控制,后面的流程大部分都是一樣的,所以這個地方只看看前面不一樣的流程:

1、store/permission.js,在vuex里面發(fā)送請求獲取數(shù)據(jù)

GenerateRoutes({ commit }, data) {
  return new Promise((resolve, reject) => {
    getRoute(data).then(res => {
     // 將獲取到的數(shù)據(jù)進行一個轉(zhuǎn)換,然后存到vuex里
      const accessedRouters = arrayToMenu(res.data)
      accessedRouters.concat([{ path: '*', redirect: '/404', hidden: true }])
      commit('SET_ROUTERS', accessedRouters)
      resolve()
    }).catch(error => {
      reject(error)
    })
  })
}

2、整理一份數(shù)據(jù)結(jié)構(gòu),存到表里

// 頁面路由格式
{
    path: '/form',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Form',
        component: () => import('@/views/form/index'),
        meta: { title: 'Form', icon: 'form' }
      }
    ]
}

// 整理后的數(shù)據(jù)格式
// 一級菜單
// parentId為0的就可以當做一級菜單,id最好是可以選4位數(shù),至于為什么等你開發(fā)項目的時候就知道了
{
    id: 1300
    parentId: 0
    title: "菜單管理"
    path: "/menu"
    hidden: false
    component: null
    hidden: false
    name: "menu"
},

// 二級菜單
// parentId不為0的,就可以拿parentId跟一級菜單的id去匹配,匹配上的就push到children里面
{
    id: 1307
    parentId: 1300
    title: "子菜單"
    hidden: false
    path: "menuItem"
    component: "menu/menuItem" // 要跟本地的文件地址匹配上
    hidden: false
    name: "menuItem"
}

3、寫一個轉(zhuǎn)化方法,把獲取到的數(shù)據(jù)轉(zhuǎn)換成router結(jié)構(gòu)

export function arrayToMenu(array) {
  const nodes = []
  // 獲取頂級節(jié)點
  for (let i = 0; i < array.length; i++) {
    const row = array[i]
    // 這個exists方法就是判斷下有沒有子級
    if (!exists(array, row.parentId)) {
      nodes.push({
        path: row.path, // 路由地址
        hidden: row.hidden, // 全部true就行,如果后端沒配
        component: Layout, // 一般就是匹配你文件的component
        name: row.name, // 路由名稱
        meta: { title: row.title, icon: row.name }, // title就是顯示的名字
        id: row.id, // 路由的id
        redirect: 'noredirect'
      })
    }
  }
  const toDo = Array.from(nodes)
  while (toDo.length) {
    const node = toDo.shift()
    // 獲取子節(jié)點
    for (let i = 0; i < array.length; i++) {
      const row = array[i]
      // parentId等于哪個父級的id,就push到哪個
      if (row.parentId === node.id) {
        const child = {
          path: row.path,
          name: row.name,
          hidden: row.hidden,
          // 核心代碼,因為二級路由的component是需要匹配頁面的
          component: require('@/views/' + row.component + '/index.vue'),
          meta: { title: row.title, icon: row.name },
          id: row.id
        }
        if (node.children) {
          node.children.push(child)
        } else {
          node.children = [child]
        }
        toDo.push(child)
      }
    }
  }
  return nodes
}
// 看下有沒有子級
function exists(rows, parentId) {
  for (let i = 0; i < rows.length; i++) {
    if (rows[i].id === parentId) return true
  }
  return false
}

上述內(nèi)容就是vue實現(xiàn)動態(tài)路由的操作過程,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI