最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

el-menu動態(tài)加載路由的實現(xiàn)

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

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

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

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

以上的信息均需要在數(shù)據(jù)庫的表中體現(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: '選項設(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: '超時提醒',
        component: WarmConfig
      }
    ]
  },
  {
    component: HomeView,
    children: [
      {
        path: '/statistics',
        name: '統(tǒng)計',
        component: DailyStatistics
      }
    ]
  },
  {
    component: HomeView,
    children: [
      {
        path: '/log',
        name: '日志管理',
        component: LogManager
      }
    ]
  },
]

這是路由,當(dāng)要動態(tài)從數(shù)據(jù)庫加載時,就不能寫在這

<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>
              選項設(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è)置</el-menu-item>
            <el-menu-item index="/employee">員工設(shè)置</el-menu-item>
          </el-sub-menu>
          <el-menu-item index="/statistics">
            <span>統(tǒng)計</span>
          </el-menu-item>
          <el-menu-item index="/log">
            <span>日志管理</span>
          </el-menu-item>
        </el-menu>

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

數(shù)據(jù)庫

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

現(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);//一級菜單
menus.forEach(menu->{//給有二級菜單的目錄設(shè)置children屬性
    List<Menu> children = map.get(menu.getId());
    menu.setChildren(children);
});
return menus;

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

[
{
"name": "不合格品列表",
"path": "/home",
"component": "product/BelowStandard.vue",
"orderNum": 1,
"parentId": 0,
"isHidden": false,
"children": null
},
{
"name": "選項設(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": "超時提醒",
				"path": "/warn",
				"component": "config/WarmConfig.vue",
				"orderNum": 4,
				"parentId": 2,
				"isHidden": false,
				"children": null
				}
]
},
{
"name": "統(tǒng)計",
"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)行處理,再添加到路由,過程中遇到一個問題,vue-router4版本去掉addRoutes換成addRoute帶來的問題困擾我很久

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

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

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里的會被清空,長度變?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('請求菜單失敗')
                }
            }
        })
    }
}

const initRoute = (router,state)=> {
    const loadView = view => {//這種引入方式控制臺不會報警告
        // 路由懶加載
        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)
}

完成這些配置之后,路由就能動態(tài)加載了,然后取出vuex中存儲的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"><!--一級菜單-->
      <template v-if="!route.isHidden">
        <el-menu-item :index = "route.path">
          <span>{{route.name}}</span>
        </el-menu-item>
      </template>
    </template>
    <template v-else><!--二級菜單-->
      <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>

實現(xiàn)效果展示

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

相關(guān)文章

  • Vue通過字符串關(guān)鍵字符實現(xiàn)動態(tài)渲染input輸入框

    Vue通過字符串關(guān)鍵字符實現(xiàn)動態(tài)渲染input輸入框

    這篇文章主要為大家詳細(xì)介紹了Vue如何通過字符串關(guān)鍵字符實現(xiàn)動態(tài)渲染input輸入框。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-12-12
  • Vue實現(xiàn)Excel本地下載及上傳的方法詳解

    Vue實現(xiàn)Excel本地下載及上傳的方法詳解

    相信大家在項目中經(jīng)常會遇到一些上傳下載文件的相關(guān)功能。這篇文章將為大家介紹一下Vue實現(xiàn)Excel本地下載及上傳的示例代碼,需要的可以參考一下
    2022-07-07
  • JS框架之vue.js(深入三:組件1)

    JS框架之vue.js(深入三:組件1)

    這篇文章主要介紹了JS框架之vue.js component組件的相關(guān)資料,本文通過實例詳解的方式給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • vue-element如何實現(xiàn)動態(tài)換膚存儲

    vue-element如何實現(xiàn)動態(tài)換膚存儲

    這篇文章主要介紹了vue-element如何實現(xiàn)動態(tài)換膚存儲問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue+webpack實現(xiàn)異步組件加載的方法

    vue+webpack實現(xiàn)異步組件加載的方法

    下面小編就為大家分享一篇vue+webpack實現(xiàn)異步組件加載的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Vue項目中禁用ESLint的幾種常見方法小結(jié)

    Vue項目中禁用ESLint的幾種常見方法小結(jié)

    Vue ESLint是一個基于ESLint的插件,它專門為Vue.js應(yīng)用設(shè)計,用于提供JavaScript代碼風(fēng)格檢查和最佳實踐規(guī)則,Vue項目通常會集成ESLint,目的是為了提升代碼質(zhì)量、保持一致性和可維護(hù)性,本文介紹了Vue項目中禁用ESLint的幾種常見方法,需要的朋友可以參考下
    2024-07-07
  • 解決vue?app.js/vender.js過大優(yōu)化啟動頁

    解決vue?app.js/vender.js過大優(yōu)化啟動頁

    這篇文章主要為大家介紹了解決vue?app.js/vender.js過大優(yōu)化啟動頁過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • vue點擊單張圖片放大實現(xiàn)步驟(純js)

    vue點擊單張圖片放大實現(xiàn)步驟(純js)

    這篇文章主要給大家介紹了關(guān)于vue點擊單張圖片放大實現(xiàn)的相關(guān)資料,在vue項目中實現(xiàn)點擊圖片放大功能相信對大家來說都不陌生,文中給出了詳細(xì)的js示例代碼,需要的朋友可以參考下
    2023-07-07
  • vue-cli3 引入 font-awesome的操作

    vue-cli3 引入 font-awesome的操作

    這篇文章主要介紹了vue-cli3 引入 font-awesome的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 解決vue動態(tài)為數(shù)據(jù)添加新屬性遇到的問題

    解決vue動態(tài)為數(shù)據(jù)添加新屬性遇到的問題

    今天小編就為大家分享一篇解決vue動態(tài)為數(shù)據(jù)添加新屬性遇到的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09

最新評論

将乐县| 依安县| 阿城市| 垫江县| 巴里| 六安市| 泗水县| 江西省| 莱西市| 常德市| 罗山县| 临安市| 松原市| 宝清县| 安西县| 陆川县| 广昌县| 卢龙县| 凤冈县| 阜宁县| 金山区| 环江| 峨眉山市| 伊宁县| 兴国县| 额济纳旗| 宝兴县| 平阳县| 封开县| 乐业县| 崇文区| 巩义市| 山丹县| 东阳市| 封开县| 仁寿县| 黎城县| 平潭县| 和田市| 海安县| 安岳县|