Vue3实现简约型侧边栏的示例代码

Vue3实现简约型侧边栏的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1、首先配置好路由地址

【如:/src/router/index.ts】

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

2、然后实现页面入口

【如:/src/views/XXXXXX/index.vue】

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

3、然后实现AAAAAA和BBBBBB页面

【如:/src/views/XXXXXX/AAAAAA/index.vue    /src/views/XXXXXX/BBBBBB/index.vue】

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

4、效果如下:~

图片[1]-Vue3实现简约型侧边栏的示例代码-爱站

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容