当前位置:   article > 正文

基于Vue3实现简约型侧边栏_vue3侧边栏导航

vue3侧边栏导航

前言

有时遇到一些需求,需要实现左边侧边栏为父级菜单,右侧内容区的顶部为子级菜单,以及其底部为子级菜单对应的模块内容。

一、示例代码

(1)首先配置好路由地址【如:/src/router/index.ts】

  1. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
  2. const routes: Array<RouteRecordRaw> = [
  3. {
  4. path: '/',
  5. redirect: '/xxxxxx'
  6. },
  7. {
  8. path: '/xxxxxx',
  9. name: '帅龍之龍',
  10. component: () => import('@/views/XXXXXX/index.vue'),
  11. children: [
  12. {
  13. path: '/xxxxxx/aaaaaa',
  14. name: '赤龍神帝',
  15. components: { AAAAAA: () => import('@/views/XXXXXX/AAAAAA/index.vue') },
  16. children: []
  17. },
  18. {
  19. path: '/xxxxxx/bbbbbb',
  20. name: '待定栏目',
  21. components: { BBBBBB: () => import('@/views/XXXXXX/BBBBBB/index.vue') },
  22. children: [],
  23. },
  24. ]
  25. }
  26. ]
  27. const router = createRouter({
  28. history: createWebHashHistory(),
  29. routes
  30. })
  31. export default router

(2)然后实现页面入口【如:/src/views/XXXXXX/index.vue】

  1. <template>
  2. <div class="index-page">
  3. <div class="index-page-navbar">
  4. <div class="index-page-navbar-item" :class="activePage == 'AAAAAA' ? 'index-page-navbar-active' : ''" @click="handleNavbarItemClick('AAAAAA')">
  5. <span>赤龍神帝</span>
  6. </div>
  7. <div class="index-page-navbar-item" :class="activePage == 'BBBBBB' ? 'index-page-navbar-active' : ''" @click="handleNavbarItemClick('BBBBBB')">
  8. <span>待定栏目</span>
  9. </div>
  10. </div>
  11. <div class="index-page-content">
  12. <router-view name="AAAAAA" v-if="activePage == 'AAAAAA'" />
  13. <router-view name="BBBBBB" v-if="activePage == 'BBBBBB'" />
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. export default {
  19. data () {
  20. return {
  21. // 当前激活的页面
  22. activePage: '',
  23. }
  24. },
  25. watch: {
  26. },
  27. created() {
  28. this.init()
  29. },
  30. mounted() {
  31. // 设置页面标题
  32. document.title = '帅龍之龍'
  33. },
  34. methods: {
  35. /**
  36. * 获取初始化参数
  37. */
  38. async init() {
  39. this.activePage = 'AAAAAA'
  40. const query = this.$route.query
  41. this.handleMatchRouter(this.activePage)
  42. },
  43. /**
  44. * 激活页面句柄
  45. */
  46. handleActivePageChange(activePage) {
  47. // 点击 el-tab 页面时,将 this.$route.query 置为 {}
  48. this.$route.query = {}
  49. this.handleMatchRouter(activePage)
  50. },
  51. /**
  52. * 激活页面句柄
  53. */
  54. handleMatchRouter(activePage) {
  55. const path = this.$route.path
  56. const b = path.toLowerCase().includes(activePage.toLowerCase())
  57. if (activePage == 'AAAAAA') {
  58. if (!b) {
  59. this.$router.push({
  60. path: '/xxxxxx/aaaaaa',
  61. query: this.$route.query,
  62. })
  63. }
  64. } else if (activePage == 'BBBBBB') {
  65. if (!b) {
  66. this.$router.push({
  67. path: '/xxxxxx/bbbbbb',
  68. query: this.$route.query,
  69. })
  70. }
  71. }
  72. },
  73. /**
  74. * 点击侧边导航栏
  75. */
  76. handleNavbarItemClick(item) {
  77. this.activePage = item
  78. this.$route.query = {}
  79. this.handleMatchRouter(item)
  80. },
  81. }
  82. }
  83. </script>
  84. <style lang="less" scoped>
  85. .index-page {
  86. display: flex;
  87. flex-direction: row;
  88. width: 100%;
  89. height: 100%;
  90. position: relative;
  91. background-color: #fff;
  92. .index-page-navbar {
  93. flex: none;
  94. width: 40px;
  95. height: 100%;
  96. border-right: 1px solid #dfe1e6;
  97. .index-page-navbar-item {
  98. display: grid;
  99. width: 100%;
  100. height: 150px;
  101. background-color: #fff;
  102. border-bottom: 1px solid #dfe1e6;
  103. writing-mode: tb-rl;
  104. text-align: center;
  105. align-items: center;
  106. user-select: none;
  107. cursor: pointer;
  108. transition: all ease 0.3s;
  109. span {
  110. color: #303133;
  111. font-size: 14px;
  112. letter-spacing: 1.5px;
  113. }
  114. }
  115. .index-page-navbar-active {
  116. background-color: #5e7ce0;
  117. span {
  118. color: #fff;
  119. }
  120. }
  121. }
  122. .index-page-content {
  123. flex: 1;
  124. position: relative;
  125. height: 100%;
  126. margin: 0;
  127. padding: 0;
  128. overflow: hidden;
  129. }
  130. }
  131. </style>

(3)然后实现AAAAAA和BBBBBB页面
【如:/src/views/XXXXXX/AAAAAA/index.vue    /src/views/XXXXXX/BBBBBB/index.vue】

  1. <template>
  2. <div style="width: 100%; height: 100%; display: grid; align-items: center; text-align: center">
  3. <span style="color: #303133; font-size: 14px;">HelloWorld!...</span>
  4. </div>
  5. </template>

二、运行效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/411203
推荐阅读
相关标签
  

闽ICP备14008679号