<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                目錄 vue-element-admin后臺生成動態路由及菜單 定位:src/router/index.js 定位:mock/user.js 定位:src/permission.js 定位:src/store/modules/permission.js vue-element-admin后臺生成動態路由及菜單 我使用的是官方國際化分支vue-element-admin-i18n 根據自己需求將路由及菜單修改成動態生成 如果直接使用的基礎模板 vue-admin-template 自己再下載一份 vue-element-admin 將沒有的文件復制到自己的項目里面 定位:src/router/index.js constantRoutes:通用頁面路由 asyncRoutes:動態路由 將 asyncRoutes =[…] 的值復制到文本中, 并且把里面 component: () => import(‘@/views/dashboard/index’), 內容改為 component: (‘dashboard/index’), 凡是有import的都改一下, 以及 component: Layout 我直接改為component: ‘Layout’ 對我為什么不全部保留 而是把 @/views/ 刪掉 后面會給出答案 當然也可以自己嘗試 記憶猶新 并將其修改為 asyncRoutes=[] 定位:mock/user.js 根據自己需求新增接口 在這里只是為了快速配置 所以直接使用了 url: ‘/vue-element-admin/user/info.*’ 這個接口 原接口內容 ``` // get user info { url: '/vue-element-admin/user/info\.*', type: 'get', response: config => { const { token } = config.query const info = users[token] return { code: 20000, data: info } } }, ``` 修改后接口內容 ``` // get user info { url: '/vue-element-admin/user/info\.*', type: 'get', response: config => { const { token } = config.query const info = users[token] const rdata = [.....]//這里是前面復制到文本中的asyncRoutes值 自行加入 // mock error if (!info) { return { code: 50008, message: 'Login failed, unable to get user details.' } } info.routs = rdata //給info多加一個返回值 我隨便命名為routs return { code: 20000, data: info } } }, ``` 定位:src/permission.js 這里就貼一下原文判斷 token 后實現的內容,修改的是 try 里面的內容 原代碼 ``` if (hasToken) { if (to.path === '/login') { // if is logged in, redirect to the home page next({ path: '/' }) NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939 } else { // determine whether the user has obtained his permission roles through getInfo const hasRoles = store.getters.roles && store.getters.roles.length > 0 if (hasRoles) { next() } else { try { // get user info // note: roles must be a object array! such as: ['admin'] or ,['developer','editor'] const { roles } = await store.dispatch('user/getInfo') // generate accessible routes map based on roles const accessRoutes = await store.dispatch('permission/generateRoutes', roles) // dynamically add accessible routes router.addRoutes(accessRoutes) // hack method to ensure that addRoutes is complete // set the replace: true, so the navigation will not leave a history record next({ ...to, replace: true }) } catch (error) { // remove token and go to login page to re-login await store.dispatch('user/resetToken') Message.error(error || 'Has Error') next(`/login?redirect=${to.path}`) NProgress.done() } } } } ``` 修改后 只貼 try 的內容,主要是將請求 info 后得到的數據 都放入 data 中,然后訪問 store.dispatch(‘permission/generateRoutes’, data) 這就是定位到 src/store/modules/permission.js 使用里面的 generateRoutes 方法 ``` try { // get user info // note: roles must be a object array! such as: ['admin'] or ,['developer','editor'] const data = await store.dispatch('user/getInfo') // console.log(data) // generate accessible routes map based on roles const accessRoutes = await store.dispatch('permission/generateRoutes', data) // dynamically add accessible routes router.addRoutes(accessRoutes) // hack method to ensure that addRoutes is complete // set the replace: true, so the navigation will not leave a history record next({ ...to, replace: true }) } ``` 定位:src/store/modules/permission.js 原代碼 generateRoutes 方法 ``` generateRoutes({ commit }, roles) { return new Promise(resolve => { let accessedRoutes if (roles.includes('admin')) { accessedRoutes = asyncRoutes || [] } else { accessedRoutes = filterAsyncRoutes(asyncRoutes, roles) } commit('SET_ROUTES', accessedRoutes) resolve(accessedRoutes) }) } ``` 修改后 ``` generateRoutes({ commit }, data) { return new Promise(resolve => { const accessedRoutes = filterAsyncRoutes(data.routs, data.roles) commit('SET_ROUTES', accessedRoutes) resolve(accessedRoutes) }) } ``` 然后定位到 filterAsyncRoutes 方法 原代碼 ``` 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 } ``` 修改后代碼 以及我自己新加的代碼 ``` export const loadView = (view) => { console.log(view) return (resolve) => require([`@/views/${view}`], resolve) } export function filterAsyncRoutes(routes, roles) { const list = [] routes.forEach(p => { if (hasPermission(roles, p)) { p.component = () => import('@/layout') if (p.children != null) { p.children = getChildren(p.children) } list.push(p) } }) return list } function getChildren(data) { const array = [] data.forEach(x => { const children = JSON.parse(JSON.stringify(x)) children.component = loadView(x.component) if (x.children != null) { children.children = getChildren(x.children) } array.push(children) }) return array } ``` 這段代碼可能會有些疑惑,在 filterAsyncRoutes 中 我直接定義 component: () => import(‘@/layout’), 是因為我試過動態生成但是因為 會報找不到模塊 根據網上查找的資料顯示,因為路徑的問題。即根據 component 的路徑,匹配不到已編譯的組件,因為匹配期間不會再計算代碼所在文件路徑相對 component 的路徑。比如 component 的值分別為@/views/index.vue、…/views/index.vue、./views/index.vue,運行期間這些直接拿去跟已注冊組件匹配,并不會再次計算真實路徑 來自千年輪回的博客 也就是說 盡管是動態生成 也得定義到頁面存在的路徑前面 沒辦法直接生成 例如:json里面component : ‘/layout’ 定義路由 component:()=>import(‘@’+component) 是無法生成的 必須寫為 import(‘@/layout’) 才能找到模塊 建議動手嘗試一下 ———————————————— 版權聲明:本文為CSDN博主「前端 賈公子」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/qq_63358859/article/details/129587239
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看