亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

vue addRoutes路由動態加載操作

瀏覽:62日期:2022-12-17 16:50:24

需求:增加權限控制,實現不同角色顯示不同的路由導航

思路:每次登陸后請求接口返回當前角色路由

核心方法:vue-router2.2.0的addRoutes方法 + vuex

以下是我實現的獲取菜單路由的方法,我將該方法的調用放在首頁組件的生命鉤子中,即便用戶刷新瀏覽器清空了路由還是會重新調用接口獲取,不至于會丟失。同時考慮到會有切換用戶的可能,所以不將獲取到的路由信息保存到cookie或者localstorage當中

獲取菜單之前先判斷routerState,避免多次請求, 我這里使用element-ui的導航菜單功能v-for循環this.myRouter參數即可顯示動態路由導航

/*** 獲取菜單*/getMenu () { if (this.$store.getters.routerState === false) { // 清理已經存在的動態路由 this.clearDynamicRoute() // 更改請求路由狀態為true this.$store.commit(’SET_ROUTERSTATE’, true) getMyMenu().then((res) => { if (res.code === ’0’) { // 格式化路由,將數據轉為addRoutes可接受的route格式數組 let myMenu = this.formatMenu(res.data.menu) if (myMenu.length <= 0) { // 沒有動態路由 return } for (let index = 0; index < myMenu.length; index++) { // 將請求的路由先存放到options對象中 this.$router.options.routes.push(myMenu[index]) } // 將完整需要顯示的路由添加進去 this.$router.addRoutes(this.$router.options.routes) // 這里將路由顯示在頁面上 this.MyRouter = this.$router.options.routes } // 在這里就可以打印出新路由 console.log(this.$router) }) }}

補充知識:vue+element 進入不同路由頁面(二級頁面),讓相應的左側菜單

路由配置

{ path: ’/finance’, name: ’Finance’, meta: { title: ’財務’ }, component: () => import(’@/components/Finance’), redirect: ’/finance/code/code’, children: [{ path: ’/finance/code’, name: ’financeindex’, meta: { title: ’稅收配置’ }, redirect: ’/finance/code/code’, component: () => import(’@/components/finance/financeindex’), children: [{ path: ’/finance/code/code’, name: ’FinanceCode’, hidden: false, active:’/finance/code’, //這里是在左側菜單顯示并且需要選中的 meta: { title: ’稅收編碼(金稅)’ }, component: () => import(’@/components/finance/code/Code’), }, { path: ’/finance/code/codeimportrecord’, // 這個路由進入的界面是 稅收編碼(金稅)的二級頁面, 當進入這個頁面的時候,需要菜單中的稅收編碼(金稅)顯示選中 name: ’FinanceCodeImportRecord’, hidden: true, meta: { title: ’稅收編碼導入記錄’ }, component: () => import(’@/components/finance/code/CodeImportRecord’), }, { path: ’/finance/classcode/classcode’, name: ’FinanceClassCode’, hidden: false, active:’/finance/classcode’, //為了省事,只給需要在左側顯示的路由添加active屬性 meta: { title: ’分類稅收編碼確認’ }, component: () => import(’@/components/finance/classCode/ClassCode’), }, ] }, ]

element

<template> <div class='leftnav'> <!--<div class='' v-for='nav in navs'> <div class='LiHeader'> {{nav.name}} </div> <li v-for='item in nav.san'> {{item.name}} </li> </div>--> <el-menu :default-active=this.show // 這是的值是指與 el-menu-item中:index的值對應的那一天顯示選中(正常情況就是一個頁面一個路由,進入那個路由,對應的導航菜單需要選中) @open='handleOpen' @close='handleClose' background-color='#282b33' text-color='#bcbcbc' active-text-color='#ffffff'> <div class='' v-for='(nav,index) in navs' :key='index' style='width: 200px;'> <div class='' style='color: #ffffff;height: 40px;line-height: 40px;padding-left: 20px;font-size: 16px;'> {{nav.meta.title}} </div> <el-menu-item @click='clickroute(item.path)' v-for='(item,index) in nav.children' v-if='!item.hidden' :key='index' :index='item.active'(這里原來是item.path) style='height: 40px;line-height: 40px;'>{{item.meta.title}}</el-menu-item> </div> </el-menu> </div></template><script>

js

data() { return { navs:[], show:null //初始化上面:default-active綁定的值 } }, created() { //// 頁面載入的時候,拿到url,用/拆開,然后拼起來前兩個路徑,并且賦值, 這個時候show對應的就是路由表中的 avtive, let route=this.$route.path.split(’/’) let vueRouter=this.$router.options.routes.filter(router=>{return router.path==’/’})[0].children let filterVuerouter=vueRouter.filter(router=>{return ’/’+route[1] == router.path }) this.navs=filterVuerouter[0].children console.log(this.navs) let router =’/’+route[1]+’/’+route[2] console.log(router) this.show=router// console.log(this.show) }, mounted() { },

以上這篇vue addRoutes路由動態加載操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 免费香蕉依人在线视频久 | 9191精品国产免费不久久 | 国产成人综合久久 | 中文精品久久久久国产不卡 | 一级特黄牲大片免费视频 | 黄色欧美一级片 | 婷婷激情综合 | 精品一区二区久久 | 欧美精品国产一区二区三区 | 午夜视频免费在线观看 | 可以看毛片的网址 | 国产黄三级三·级三级 | 九九亚洲精品自拍 | 扒开双腿猛进入jk校视频 | 欧美精品在线观看视频 | 国产小视频在线观看 | 两个人看的www视频中文字幕 | 国产又黄又爽又色视频影视网免费 | 国产一区影视 | 国产高清专区 | 激情97| 日韩精品视频在线 | 女人洗澡一级特黄毛片 | 日本一级做人免费视频 | 成人国产精品999视频 | 亚洲人成在线影院 | 日本xxxwwxxx免费视频 | 澳门麻豆传媒精东影业 | 日韩欧美一区二区三区在线视频 | 一区二区三区在线视频观看 | 黄色一集片 | 在线日本妇人成熟免费观看 | 欧美日韩一区二区在线视频播放 | 99久久99久久精品免费看蜜桃 | 久久国产精品亚洲综合 | 台湾亚洲精品一区二区tv | 韩日在线 | 成人欧美一区二区三区在线观看 | 99视频都是精品热在线播放 | 91精品国产91久久久久久青草 | 女人16一毛片 |