欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

el-menu動(dòng)態(tài)加載路由的實(shí)現(xiàn)

 更新時(shí)間:2023年04月13日 15:44:16   作者:天外飛鮮橙子哥  
本文主要介紹了el-menu動(dòng)態(tài)加載路由的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

先看需要實(shí)現(xiàn)的效果

這里有一級(jí)也有二級(jí)菜單,注意二級(jí)菜單的父目錄(”選項(xiàng)設(shè)置“點(diǎn)擊不會(huì)跳轉(zhuǎn),只是展開目錄),然后點(diǎn)擊去詳情頁(yè),需要跳到一個(gè)隱藏的路由,不在菜單展示的路由

還有一點(diǎn)要注意,就是這里有兩個(gè)router-view,整個(gè)頁(yè)面是一個(gè)router-view,可以由LoginView和HomeView替換(當(dāng)前看到的頁(yè)面),而HomeView下又有一個(gè)router-view,需要用來展示部門,系統(tǒng),超時(shí),員工設(shè)置,不合格品列表和不合格品詳情頁(yè)。

以上的信息均需要在數(shù)據(jù)庫(kù)的表中體現(xiàn)

先看看直接寫在代碼里需要哪些操作

const routes = [
  {
    path: '',
    name: 'login',
    component: LoginView,
  }
  ,
  {
    component: HomeView,
    children: [
      {
        path: '/home',
        name: '不合格品列表',
        component: BelowStandard
      },
      {
        path: '/product/:id',
        name: '不合格品詳情',
        component: BelowStandardDetail
      }
    ]
  },
  {
    component: HomeView,
    name: '選項(xiàng)設(shè)置',
    children: [
      {
        path: '/employee',
        name: '員工設(shè)置',
        component: EmployeeConfig,
      },
      {
        path: '/department',
        name: '部門設(shè)置',
        component: DepartmentConfig
      },
      {
        path: '/system',
        name: '系統(tǒng)設(shè)置',
        component: SystemConfig
      },
      {
        path: '/warn',
        name: '超時(shí)提醒',
        component: WarmConfig
      }
    ]
  },
  {
    component: HomeView,
    children: [
      {
        path: '/statistics',
        name: '統(tǒng)計(jì)',
        component: DailyStatistics
      }
    ]
  },
  {
    component: HomeView,
    children: [
      {
        path: '/log',
        name: '日志管理',
        component: LogManager
      }
    ]
  },
]

這是路由,當(dāng)要?jiǎng)討B(tài)從數(shù)據(jù)庫(kù)加載時(shí),就不能寫在這

<el-menu
            router
            active-text-color="#ffd04b"
            background-color="#000"
            class="el-menu-vertical-demo"
            :default-active="this.$route.path"
            text-color="#fff"
            @open=""
            @close=""
        >
          <el-menu-item index="/home">
            <template #title>
              不合格品列表
            </template>
          </el-menu-item>
          <el-sub-menu index="/subMenuConfig">
            <template #title>
              選項(xiàng)設(shè)置
            </template>
            <el-menu-item index="/department">部門設(shè)置</el-menu-item>
            <el-menu-item index="/system">系統(tǒng)設(shè)置</el-menu-item>
            <el-menu-item index="/warn">超時(shí)設(shè)置</el-menu-item>
            <el-menu-item index="/employee">員工設(shè)置</el-menu-item>
          </el-sub-menu>
          <el-menu-item index="/statistics">
            <span>統(tǒng)計(jì)</span>
          </el-menu-item>
          <el-menu-item index="/log">
            <span>日志管理</span>
          </el-menu-item>
        </el-menu>

這是el-menu開啟了路由功能,所以能跳轉(zhuǎn)路由,當(dāng)動(dòng)態(tài)加載的時(shí)候,這部分需要改造成v-for

數(shù)據(jù)庫(kù)

說明:parent_id為0的即是一級(jí)目錄,但是一級(jí)目錄里一部分可以直接展示界面,一部分是展開二級(jí)目錄,我這是以component字段為home/HomeView.vue來區(qū)分是展示二級(jí)目錄。

現(xiàn)在開始寫后端程序,返回菜單的json格式數(shù)據(jù)。

List<Menu> menuList = menuMapper.getMenuByUserId(UserUtils.getLoginUser().getId());
//根據(jù)ParentId分組
Map<Integer, List<Menu>> map = menuList.stream().collect(Collectors.groupingBy(Menu::getParentId, TreeMap::new,Collectors.toList()));
List<Menu> menus = map.get(0);//一級(jí)菜單
menus.forEach(menu->{//給有二級(jí)菜單的目錄設(shè)置children屬性
    List<Menu> children = map.get(menu.getId());
    menu.setChildren(children);
});
return menus;

從數(shù)據(jù)庫(kù)查詢到的數(shù)據(jù)格式如圖,然后分一級(jí)二級(jí)菜單處理后,再返回前端

[
{
"name": "不合格品列表",
"path": "/home",
"component": "product/BelowStandard.vue",
"orderNum": 1,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "選項(xiàng)設(shè)置",
"path": "/subMenuConfig",
"component": "home/HomeView.vue",
"orderNum": 2,
"parentId": 0,
"isHidden": false,
"children": [
				{
				"name": "員工設(shè)置",
				"path": "/employee",
				"component": "config/EmployeeConfig.vue",
				"orderNum": 1,
				"parentId": 2,
				"isHidden": false,
				"children": null
				},
				{
				"name": "部門設(shè)置",
				"path": "/department",
				"component": "config/DepartmentConfig.vue",
				"orderNum": 2,
				"parentId": 2,
				"isHidden": false,
				"children": null
				},
				{
				"name": "系統(tǒng)設(shè)置",
				"path": "/system",
				"component": "config/SystemConfig.vue",
				"orderNum": 3,
				"parentId": 2,
				"isHidden": false,
				"children": null
				},
				{
				"name": "超時(shí)提醒",
				"path": "/warn",
				"component": "config/WarmConfig.vue",
				"orderNum": 4,
				"parentId": 2,
				"isHidden": false,
				"children": null
				}
]
},
{
"name": "統(tǒng)計(jì)",
"path": "/statistics",
"component": "statistics/DailyStatistics.vue",
"orderNum": 3,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "日志管理",
"path": "/log",
"component": "log/LogManager.vue",
"orderNum": 4,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "不合格品詳情",
"path": "/product/:id",
"component": "product/BelowStandardDetail.vue",
"orderNum": 5,
"parentId": 0,
"isHidden": true,
"children": null
}
]

前端得到數(shù)據(jù)之后進(jìn)行處理,再添加到路由,過程中遇到一個(gè)問題,vue-router4版本去掉addRoutes換成addRoute帶來的問題困擾我很久

用Vue3就必須用Router4.x版本,由于4.0去掉了addRoutes 所以只能用addRoute

現(xiàn)在是只能添加一個(gè)

function routerPackag(routers:any) {
  if (routers) {
    routers.filter((itemRouter:any) => {
      if (itemRouter.component != "Layout") {
        router.addRoute('home',{ //home是父組件 add-route添加進(jìn)父組件chilren里
          path: `${itemRouter.path}`,
          name: itemRouter.name,
          meta: {
            title: itemRouter.name,
          },
          component: () => import(`../views/${itemRouter.component}`),
        })
      }
      if (itemRouter.children && itemRouter.children.length) {
        routerPackag(itemRouter.children)
      }
      return true
    })
  }
}

初始化路由:

router.beforeEach((to, from, next) => {//配置路由守衛(wèi)
    if(to.path==='/'){
        next()
    }else if(store.state.user.id){
        initMenus(router,store,next,to)
    }else{
        next({ path: '/',query: {redirect: to.path}});
    }
});

export const initMenus = (router, store,next,to) => {//按F5刷新的話vuex里的會(huì)被清空,長(zhǎng)度變?yōu)?
    if (store.state.menu !== null) {
        next()
    }else {
        axios.get("/menu").then(response => {
            if (response) {
                let responseData = response.data
                if (responseData.flag) {
                    store.state.menu = responseData.data
                    initRoute(router,store.state)
                    next({...to,replace:true})//解決router4版本的第一次路由不匹配問題
                } else {
                    this.$ElMessage.error('請(qǐng)求菜單失敗')
                }
            }
        })
    }
}

const initRoute = (router,state)=> {
    const loadView = view => {//這種引入方式控制臺(tái)不會(huì)報(bào)警告
        // 路由懶加載
        return () => import(`@/views/${view}`)
    };
    const menus = state.menu
    const firstLevelMenu = {
        children: [],
        component: loadView('home/HomeView.vue')
    }
    menus.forEach(menu=>{
        menu.component = loadView(menu.component)
        if(menu.children === null || menu.children.length === 0){
            firstLevelMenu.children.push(menu)
        }else{
            menu.children.forEach(children=>{
                children.component = loadView(children.component)
            })
            router.addRoute(menu)
        }
    })
    router.addRoute(firstLevelMenu)
}

完成這些配置之后,路由就能動(dòng)態(tài)加載了,然后取出vuex中存儲(chǔ)的menu生成el-menu

vuex中菜單大致如圖

<el-menu
    router
    active-text-color="#ffd04b"
    background-color="#000"
    class="el-menu-vertical-demo"
    :default-active="this.$route.path"
    text-color="#fff"
    @open=""
    @close=""
>
  <template v-for="route of this.$store.state.menu">
    <template v-if="route.children === null || route.children.length === 0"><!--一級(jí)菜單-->
      <template v-if="!route.isHidden">
        <el-menu-item :index = "route.path">
          <span>{{route.name}}</span>
        </el-menu-item>
      </template>
    </template>
    <template v-else><!--二級(jí)菜單-->
      <template v-if="!route.isHidden">
        <el-sub-menu :index = "route.path">
          <template #title>
            <span>{{route.name}}</span>
          </template>
          <template v-for="children of route.children">
            <template v-if="!children.isHidden">
              <el-menu-item :index = "children.path">
                <span>{{children.name}}</span>
              </el-menu-item>
            </template>
          </template>
        </el-sub-menu>
      </template>
    </template>
  </template>
</el-menu>

實(shí)現(xiàn)效果展示

到此這篇關(guān)于el-menu動(dòng)態(tài)加載路由的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)el-menu動(dòng)態(tài)加載路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于vue-tree-chart簡(jiǎn)單的使用

    關(guān)于vue-tree-chart簡(jiǎn)單的使用

    這篇文章主要介紹了關(guān)于vue-tree-chart簡(jiǎn)單的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue新搭檔TypeScript快速入門實(shí)踐記錄

    Vue新搭檔TypeScript快速入門實(shí)踐記錄

    TypeScript 是一種由微軟開發(fā)的自由和開源的編程語言,它是 JavaScript 的一個(gè)超集,擴(kuò)展了 JavaScript 的語法。這篇文章主要介紹了Vue新搭檔TypeScript快速入門實(shí)踐,需要的朋友可以參考下
    2021-06-06
  • vue自定義組件如何通過v-model指令控制組件的隱藏、顯示

    vue自定義組件如何通過v-model指令控制組件的隱藏、顯示

    這篇文章主要介紹了vue自定義組件如何通過v-model指令控制組件的隱藏、顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 解決el-tree數(shù)據(jù)回顯時(shí)子節(jié)點(diǎn)部分選中父節(jié)點(diǎn)都全選中的坑

    解決el-tree數(shù)據(jù)回顯時(shí)子節(jié)點(diǎn)部分選中父節(jié)點(diǎn)都全選中的坑

    本文主要介紹了解決el-tree數(shù)據(jù)回顯時(shí)子節(jié)點(diǎn)部分選中父節(jié)點(diǎn)都全選中的坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 解決VUEX的mapState/...mapState等取值問題

    解決VUEX的mapState/...mapState等取值問題

    這篇文章主要介紹了解決VUEX的mapState/...mapState等取值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue.js?的過濾器你了解多少

    Vue.js?的過濾器你了解多少

    這篇文章主要為大家詳細(xì)介紹了Vue.js?的過濾器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • vue實(shí)現(xiàn)四級(jí)導(dǎo)航及驗(yàn)證碼的方法實(shí)例

    vue實(shí)現(xiàn)四級(jí)導(dǎo)航及驗(yàn)證碼的方法實(shí)例

    我們?cè)谧鲰?xiàng)目經(jīng)常會(huì)遇到多級(jí)導(dǎo)航這個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)四級(jí)導(dǎo)航及驗(yàn)證碼的相關(guān)資料,文章通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07
  • 使用VUE實(shí)現(xiàn)一鍵復(fù)制內(nèi)容功能

    使用VUE實(shí)現(xiàn)一鍵復(fù)制內(nèi)容功能

    這篇文章主要介紹了使用VUE實(shí)現(xiàn)一鍵復(fù)制內(nèi)容功能,功能就是當(dāng)我們點(diǎn)擊復(fù)制按鈕時(shí),會(huì)提示“復(fù)制成功”,這樣復(fù)制的內(nèi)容就可以在其他地方使用了,感興趣的朋友可以學(xué)習(xí)一下
    2023-04-04
  • Vue組件的使用及個(gè)人理解與介紹

    Vue組件的使用及個(gè)人理解與介紹

    本文介紹了VUE中組件的基本使用以及個(gè)人對(duì)VUE組件的理解,希望能幫助到大家
    2019-02-02
  • Vue中render函數(shù)的使用方法

    Vue中render函數(shù)的使用方法

    本篇文章主要介紹了Vue中render函數(shù)的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01

最新評(píng)論