当前位置:   article > 正文

element ui NavMenu 实现侧边栏导航菜单_element ui 侧边栏菜单

element ui 侧边栏菜单

一、原理

 

根据v-for遍历菜单参数,渲染导航栏。

使用Element UI 的 Container 布局容器和NavMenu导航菜单组件,使用router-view存放二级路由出口。

二、效果

三、实现案例

html:

  1. <el-container>
  2. <el-aside
  3. class="aside flex-shrink-0"
  4. style="width:260px;height: 100%;"
  5. >
  6. <el-menu
  7. class="border-r-0"
  8. style="height: 100%;"
  9. router
  10. :default-active="menuActive"
  11. @select="menuChange"
  12. >
  13. <template v-for="(item,index) in menuList">
  14. <el-submenu
  15. :key="item.dictValue"
  16. :index="item.dictValue"
  17. >
  18. <template slot="title">
  19. <i class="el-icon-document" />
  20. <span>{{ item.dictLabel }}</span>
  21. </template>
  22. <el-menu-item
  23. v-for="child in item.children"
  24. :key="`${child.dictValue}`"
  25. :index="`${child.dictValue}`"
  26. :route="{ name: child.dictValue }"
  27. >
  28. <i class="el-icon-document" />
  29. <span slot="title">{{ child.dictLabel }}</span>
  30. </el-menu-item>
  31. </el-submenu>
  32. </template>
  33. </el-menu>
  34. </el-aside>
  35. < !-- 二级路由出口 -->
  36. <router-view />
  37. </el-container>

js:

  1. export default {
  2. name: 'test',
  3. components: {
  4. },
  5. data() {
  6. const menuList = [
  7. {
  8. dictLabel: '导航一',
  9. dictValue: '1',
  10. children: [
  11. { dictLabel: '分组一', dictValue: 'group1Item1'},
  12. ],
  13. },
  14. {
  15. dictLabel: '导航二',
  16. dictValue: '2',
  17. children: [
  18. { dictLabel: '分组一', dictValue: 'group2Item1'},
  19. ],
  20. },
  21. ]
  22. return {
  23. menuList,
  24. menuActive: menuList[0].children[0]?.dictValue,
  25. };
  26. },
  27. watch: {
  28. '$route.path': function watch() {
  29. const { name, params } = this.$route;
  30. this.menuActive = `${name}${params.type || ''}`;
  31. },
  32. },
  33. mounted() {
  34. // 刷新页面时默认展示当前路由
  35. const { name, params } = this.$route;
  36. this.menuActive = `${name}${params.type || ''}`;
  37. },
  38. methods: {
  39. menuChange(index) {
  40. this.menuActive = index;
  41. },
  42. },
  43. };

router/index.js: 

配置路由

  1. {
  2. path: 'test',
  3. name: 'test',
  4. meta: {
  5. title: '菜单',
  6. },
  7. redirect: '/test/group1Item1', // 默认页面
  8. component: () => import('@/test/index.vue'),
  9. children: [
  10. {
  11. path: 'group1Item1',
  12. name: 'group1Item1',
  13. meta: {
  14. title: '导航一分组一',
  15. },
  16. component: () => import('@/test/group1Item1.vue'),
  17. },
  18. {
  19. path: 'group2Item1',
  20. name: 'group2Item1',
  21. meta: {
  22. title: '导航二分组一',
  23. },
  24. component: () => import('@/test/group2Item1.vue'),
  25. }
  26. ]
  27. },


 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号