久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      vue動態(tài)路由有哪兩種實(shí)現(xiàn)方法

      vue動態(tài)路由的兩種實(shí)現(xiàn)方法:1、簡單的角色路由設(shè)置,比如只涉及到管理員和普通用戶的權(quán)限;通常直接在前端進(jìn)行簡單的角色權(quán)限設(shè)置。2、復(fù)雜的路由權(quán)限設(shè)置,比如OA系統(tǒng)、多種角色的權(quán)限配置;通常需要后端返回路由列表,前端渲染使用。

      vue動態(tài)路由有哪兩種實(shí)現(xiàn)方法

      本教程操作環(huán)境:windows7系統(tǒng)、vue3版,DELL G3電腦。

      動態(tài)路由不同于常見的靜態(tài)路由,可以根據(jù)不同的「因素」而改變站點(diǎn)路由列表。

      動態(tài)路由設(shè)置一般有兩種:

      (1)、簡單的角色路由設(shè)置:比如只涉及到管理員和普通用戶的權(quán)限。通常直接在前端進(jìn)行簡單的角色權(quán)限設(shè)置

      (2)、復(fù)雜的路由權(quán)限設(shè)置:比如OA系統(tǒng)、多種角色的權(quán)限配置。通常需要后端返回路由列表,前端渲染使用

      1、簡單的角色路由設(shè)置

      (1)配置項(xiàng)目路由權(quán)限

      // router.js import Vue from 'vue' import Router from 'vue-router' import Layout from '@/layout' Vue.use(Router)// 權(quán)限路由列表 let asyncRoutes = [     {         path: '/permission',         component: Layout,         redirect: '/permission/page',         alwaysShow: true,          name: 'Permission',         meta: {             title: 'Permission',             roles: ['admin', 'editor'] // 普通的用戶角色         },         children: [             {                 path: 'page',                 component: () => import('@/views/permission/page'),                 name: 'PagePermission',                 meta: {                     title: 'Page',                     roles: ['editor']  //  editor角色的用戶才能訪問該頁面                 }             },             {                 path: 'role',                 component: () => import('@/views/permission/role'),                 name: 'RolePermission',                 meta: {                     title: 'Role',                     roles: ['admin']    //  admin角色的用戶才能訪問該頁面                 }             }         ]     },  ]  // 靜態(tài)路由  let defaultRouter = [{     path: '/404',     name: '404',     component: () => import('@/views/404'),      meta: {         title: '404'     } }] let router = new Router({     mode: 'history',     scrollBehavior: () => ({ y: 0 }),     routes: defaultRouter }) export default router
      登錄后復(fù)制

      (2)新建一個公共的asyncRouter.js文件

      // asyncRouter.js //判斷當(dāng)前角色是否有訪問權(quán)限 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 }
      登錄后復(fù)制

      (3)創(chuàng)建路由守衛(wèi):創(chuàng)建公共的permission.js文件,設(shè)置路由守衛(wèi)

      import router from './router' import store from './store' import NProgress from 'nprogress' // 進(jìn)度條插件 import 'nprogress/nprogress.css' // 進(jìn)度條樣式 import { getToken } from '@/utils/auth'  import { filterAsyncRoutes } from '@/utils/asyncRouter.js' NProgress.configure({ showSpinner: false }) // 進(jìn)度條配置 const whiteList = ['/login']  router.beforeEach(async (to, from, next) => {     // 進(jìn)度條開始     NProgress.start()      // 獲取路由 meta 中的title,并設(shè)置給頁面標(biāo)題     document.title = to.meta.title    // 獲取用戶登錄的token     const hasToken = getToken()     // 判斷當(dāng)前用戶是否登錄     if (hasToken) {         if (to.path === '/login') {             next({ path: '/' })             NProgress.done()         } else {             // 從store中獲取用戶角色             const hasRoles = store.getters.roles && store.getters.roles.length > 0               if (hasRoles) {                 next()             } else {                 try {                     // 獲取用戶角色                     const roles = await store.state.roles                    // 通過用戶角色,獲取到角色路由表                     const accessRoutes = filterAsyncRoutes(await store.state.routers,roles)                     // 動態(tài)添加路由到router內(nèi)                     router.addRoutes(accessRoutes)                     next({ ...to, replace: true })                 } catch (error) {                     // 清除用戶登錄信息后,回跳到登錄頁去                     next(`/login?redirect=${to.path}`)                     NProgress.done()                 }             }         }     } else {         // 用戶未登錄         if (whiteList.indexOf(to.path) !== -1) {             // 需要跳轉(zhuǎn)的路由是否是whiteList中的路由,若是,則直接條狀             next()         } else {             // 需要跳轉(zhuǎn)的路由不是whiteList中的路由,直接跳轉(zhuǎn)到登錄頁             next(`/login?redirect=${to.path}`)             // 結(jié)束精度條             NProgress.done()         }     }})     router.afterEach(() => {     // 結(jié)束精度條     NProgress.done()    })
      登錄后復(fù)制

      (4)在main.js中引入permission.js文件

      (5)在login登錄的時候?qū)oles存儲到store中

      2、復(fù)雜的路由權(quán)限設(shè)置(后端動態(tài)返回路由數(shù)據(jù))

      (1)配置項(xiàng)目路由文件,該文件中沒有路由,或者存在一部分公共路由,即沒有權(quán)限的路由

      import Vue from 'vue' import Router from 'vue-router' import Layout from '@/layout'; Vue.use(Router)// 配置項(xiàng)目中沒有涉及權(quán)限的公共路由 export const constantRoutes = [     {         path: '/login',         component: () => import('@/views/login'),         hidden: true     },     {         path: '/404',         component: () => import('@/views/404'),         hidden: true     }, ] const createRouter = () => new Router({     mode: 'history',     scrollBehavior: () => ({ y: 0 }),     routes: constantRoutes }) const router = createRouter() export function resetRouter() {     const newRouter = createRouter()     router.matcher = newRouter.matcher } export default router
      登錄后復(fù)制

      (2)新建一個公共的asyncRouter.js文件

      // 引入路由文件這種的公共路由 import { constantRoutes } from '../router';// Layout 組件是項(xiàng)目中的主頁面,切換路由時,僅切換Layout中的組件 import Layout from '@/layout'; export function getAsyncRoutes(routes) {     const res = []     // 定義路由中需要的自定名     const keys = ['path', 'name', 'children', 'redirect', 'meta', 'hidden']     // 遍歷路由數(shù)組去重組可用的路由     routes.forEach(item => {         const newItem = {};         if (item.component) {             // 判斷 item.component 是否等于 'Layout',若是則直接替換成引入的 Layout 組件             if (item.component === 'Layout') {                 newItem.component = Layout             } else {             //  item.component 不等于 'Layout',則說明它是組件路徑地址,因此直接替換成路由引入的方法                 newItem.component = resolve => require([`@/views/${item.component}`],resolve)                                  // 此處用reqiure比較好,import引入變量會有各種莫名的錯誤                 // newItem.component = (() => import(`@/views/${item.component}`));             }         }         for (const key in item) {             if (keys.includes(key)) {                 newItem[key] = item[key]             }         }         // 若遍歷的當(dāng)前路由存在子路由,需要對子路由進(jìn)行遞歸遍歷         if (newItem.children && newItem.children.length) {             newItem.children = getAsyncRoutes(item.children)         }         res.push(newItem)     })     // 返回處理好且可用的路由數(shù)組     return res  }
      登錄后復(fù)制

      (3)創(chuàng)建路由守衛(wèi):創(chuàng)建公共的permission.js文件,設(shè)置路由守衛(wèi)

      //  進(jìn)度條引入設(shè)置如上面第一種描述一樣 import router from './router' import store from './store' import NProgress from 'nprogress' // progress bar import 'nprogress/nprogress.css' // progress bar style import { getToken } from '@/utils/auth' // get token from cookie import { getAsyncRoutes } from '@/utils/asyncRouter' const whiteList = ['/login']; router.beforeEach( async (to, from, next) => {     NProgress.start()     document.title = to.meta.title;     // 獲取用戶token,用來判斷當(dāng)前用戶是否登錄     const hasToken = getToken()     if (hasToken) {         if (to.path === '/login') {             next({ path: '/' })             NProgress.done()         } else {             //異步獲取store中的路由             let route = await store.state.addRoutes;             const hasRouters = route && route.length>0;             //判斷store中是否有路由,若有,進(jìn)行下一步             if ( hasRouters ) {                 next()             } else {                 //store中沒有路由,則需要獲取獲取異步路由,并進(jìn)行格式化處理                 try {                     const accessRoutes = getAsyncRoutes(await store.state.addRoutes );                     // 動態(tài)添加格式化過的路由                     router.addRoutes(accessRoutes);                     next({ ...to, replace: true })                 } catch (error) {                     // Message.error('出錯了')                     next(`/login?redirect=${to.path}`)                     NProgress.done()                 }             }         }     } else {         if (whiteList.indexOf(to.path) !== -1) {             next()         } else {             next(`/login?redirect=${to.path}`)             NProgress.done()         }     }})router.afterEach(() => {     NProgress.done()  })
      登錄后復(fù)制

      (4)在main.js中引入permission.js文件

      (5)在login登錄的時候?qū)⒙酚尚畔⒋鎯Φ絪tore中

      //  登錄接口調(diào)用后,調(diào)用路由接口,后端返回相應(yīng)用戶的路由res.router,我們需要存儲到store中,方便其他地方拿取 this.$store.dispatch("addRoutes", res.router);
      登錄后復(fù)制

      到這里,整個動態(tài)路由就可以走通了,但是頁面跳轉(zhuǎn)、路由守衛(wèi)處理是異步的,會存在動態(tài)路由添加后跳轉(zhuǎn)的是空白頁面,這是因?yàn)槁酚稍趫?zhí)行next()時,router里面的數(shù)據(jù)還不存在,此時,你可以通過window.location.reload()來刷新路由
      后端返回的路由格式:

      routerList = [   {         "path": "/other",         "component": "Layout",         "redirect": "noRedirect",         "name": "otherPage",         "meta": {             "title": "測試",         },         "children": [             {                 "path": "a",                 "component": "file/a",                 "name": "a",                 "meta": { "title": "a頁面", "noCache": "true" }             },             {                 "path": "b",                 "component": "file/b",                 "name": "b",                 "meta": { "title": "b頁面", "noCache": "true" }             },         ]     } ]
      登錄后復(fù)制

      注意:vue是單頁面應(yīng)用程序,所以頁面一刷新數(shù)據(jù)部分?jǐn)?shù)據(jù)也會跟著丟失,所以我們需要將store中的數(shù)據(jù)存儲到本地,才能保證路由不丟失。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號