当前位置:   article > 正文

vue3-elementplus 后台管理系统的Layout布局_vue3 elementplus布局

vue3 elementplus布局

1.采用elementplus中container布局容器

 2.router.js中进行配置路由

  1. import {
  2. createRouter,
  3. createWebHistory,
  4. } from "vue-router";
  5. import Layout from '@/layout/index.vue'
  6. const routes = [{
  7. path: '/',
  8. redirect: '/login'
  9. },
  10. {
  11. name: 'login',
  12. path: '/login',
  13. component: () => import('@/views/login/index.vue')
  14. },
  15. {
  16. component: Layout,
  17. path: "/",
  18. children: [{
  19. path: "/home",
  20. name: "home",
  21. meta: {
  22. title: '首页',
  23. icon: 'House'
  24. },
  25. /** 在src/views 文件下创建home文件 */
  26. component: () => import("@/views/home/index.vue"),
  27. },
  28. {
  29. path: "/system",
  30. name: "system",
  31. meta: {
  32. title: '系统概况',
  33. icon: 'Grid'
  34. },
  35. /** 在src/views 文件下创建system文件 */
  36. component: () => import("@/views/system/index.vue"),
  37. },
  38. {
  39. path: "/userManagement",
  40. name: "userManagement",
  41. meta: {
  42. title: '用户管理',
  43. icon: 'Avatar'
  44. },
  45. /** 在src/views 文件下创建userManagement文件 */
  46. // component: () => import("@/views/userManagement/index.vue"),
  47. children: [{
  48. path: "/productAdministrator",
  49. name: "productAdministrator",
  50. meta: {
  51. title: '产品1',
  52. icon: 'TakeawayBox'
  53. },
  54. /** 在src/views 文件下创建productAdministrator文件 */
  55. component: () => import("@/views/userManagement/productAdministrator/index.vue"),
  56. },
  57. {
  58. path: "/prod",
  59. name: "productAdministrator",
  60. meta: {
  61. title: '产品2',
  62. icon: 'TakeawayBox'
  63. },
  64. /** 在src/views 文件下创建productAdministrator文件 */
  65. component: () => import("@/views/userManagement/productAdministrator/index.vue"),
  66. }
  67. ]
  68. },
  69. {
  70. path: "/productManagement",
  71. name: "productManagement",
  72. meta: {
  73. title: '产品管理',
  74. icon: 'TakeawayBox'
  75. },
  76. /** 在src/views 文件下创建productManagement文件 */
  77. component: () => import("@/views/productManagement/index.vue"),
  78. },
  79. {
  80. path: "/information",
  81. name: "information",
  82. meta: {
  83. title: '消息管理',
  84. icon: 'ChatLineSquare'
  85. },
  86. /** 在src/views 文件下创建information文件 */
  87. component: () => import("@/views/information/index.vue"),
  88. },
  89. {
  90. path: "/contract",
  91. name: "contract",
  92. meta: {
  93. title: '合同管理',
  94. icon: 'Tickets'
  95. },
  96. /** 在src/views 文件下创建information文件 */
  97. component: () => import("@/views/contract/index.vue"),
  98. },
  99. {
  100. path: "/operation",
  101. name: "operation",
  102. meta: {
  103. title: '操作日志',
  104. icon: 'Pointer'
  105. },
  106. /** 在src/views 文件下创建information文件 */
  107. component: () => import("@/views/operation/index.vue"),
  108. },
  109. {
  110. path: "/conLog",
  111. name: "conLog",
  112. meta: {
  113. title: '登录日志',
  114. icon: 'Document'
  115. },
  116. /** 在src/views 文件下创建information文件 */
  117. component: () => import("@/views/conLog/index.vue"),
  118. },
  119. {
  120. path: "/setting",
  121. name: "setting",
  122. meta: {
  123. title: '系统设置',
  124. icon: 'Setting'
  125. },
  126. /** 在src/views 文件下创建information文件 */
  127. component: () => import("@/views/setting/index.vue"),
  128. }
  129. ]
  130. },
  131. ];
  132. const router = createRouter({
  133. history: createWebHistory(),
  134. routes,
  135. });
  136. export default router;

3.layout/index.vue

  1. <template>
  2. <div class="common-layout">
  3. <el-container>
  4. <el-aside style="width:fit-content;" class="aside">
  5. <Aside></Aside>
  6. </el-aside>
  7. <el-container>
  8. <el-header class="header">
  9. <Header></Header>
  10. </el-header>
  11. <el-main>
  12. <router-view></router-view>
  13. </el-main>
  14. </el-container>
  15. </el-container>
  16. </div>
  17. </template>
  18. <script setup lang="ts">
  19. import Aside from './aside.vue'
  20. import Header from './header.vue'
  21. </script>
  22. <style lang="scss" scoped>
  23. .aside{
  24. height: 100vh;
  25. background-color: #545c64;
  26. }
  27. .header{
  28. background-color: #fff;
  29. border-bottom: 1px solid #c9c6c6;
  30. }
  31. </style>

4.侧边栏导航实现

  1. <template>
  2. <el-menu active-text-color="#ffd04b" background-color="#545c64" text-color="#fff" default-active="2"
  3. class="el-menu-vertical-demo" :collapse="isCollapse" @open="handleOpen" @close="handleClose" router>
  4. <div class="title">后台管理系统</div>
  5. <template v-for="subItem in menus" :key="subItem.path">
  6. <!-- 两层 -->
  7. <el-sub-menu v-if="subItem.children" :index="subItem.path">
  8. <template #title>
  9. <el-icon>
  10. <component :is="subItem.meta.icon"></component>
  11. </el-icon>
  12. <span>{{ subItem.meta.title }}</span>
  13. </template>
  14. <template v-for="item in subItem.children" :key="item.path">
  15. <el-menu-item :index="item.path">
  16. <el-icon>
  17. <component :is="item.meta.icon"></component>
  18. </el-icon>
  19. <template #title>
  20. <span>{{ item.meta.title }}</span>
  21. </template>
  22. </el-menu-item>
  23. </template>
  24. </el-sub-menu>
  25. <!-- 一层 -->
  26. <el-menu-item v-else :index="subItem.path">
  27. <el-icon>
  28. <component :is="subItem.meta.icon"></component>
  29. </el-icon>
  30. <template #title>
  31. <span>{{ subItem.meta.title }}</span>
  32. </template>
  33. </el-menu-item>
  34. </template>
  35. </el-menu>
  36. </template>
  37. <script setup lang="ts">
  38. import { computed, ref } from 'vue'
  39. import { useRouter } from 'vue-router'
  40. const router = useRouter();
  41. const top = ref(true)
  42. const menus = computed(() => {
  43. let menu = router.options.routes.filter((item) => item.children);
  44. return menu[0].children
  45. })
  46. import { layoutStore } from '@/store/layout.js'
  47. const store = layoutStore();
  48. // const isCollapse = ref(false)
  49. const isCollapse = computed(() => {
  50. return store.isCollapse;
  51. })
  52. const handleOpen = (key : string, keyPath : string[]) => {
  53. console.log(key, keyPath)
  54. }
  55. const handleClose = (key : string, keyPath : string[]) => {
  56. console.log(key, keyPath)
  57. }
  58. </script>
  59. <style lang="scss " scoped>
  60. .el-menu-vertical-demo:not(.el-menu--collapse) {
  61. width: 200px;
  62. min-height: 400px;
  63. }
  64. .el-menu {
  65. border-right: 0;
  66. }
  67. .title {
  68. font-size: 18px;
  69. display: flex;
  70. justify-content: center;
  71. top: 5px;
  72. margin-top: 15px;
  73. }
  74. </style>

5.头部header实现

  1. <template>
  2. <div class="icon">
  3. <el-icon v-if="show" @click="change">
  4. <Fold />
  5. </el-icon>
  6. <el-icon v-else @click="change">
  7. <Expand />
  8. </el-icon>
  9. </div>
  10. <el-breadcrumb separator="/">
  11. <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  12. <el-breadcrumb-item>{{current.meta.title}}</el-breadcrumb-item>
  13. </el-breadcrumb>
  14. <div class="right">
  15. <el-icon>
  16. <Message />
  17. </el-icon>
  18. <el-avatar :size="30" :src="circleUrl" />
  19. <el-dropdown>
  20. <span class="el-dropdown-link">
  21. 设置
  22. <el-icon class="el-icon--right">
  23. <arrow-down />
  24. </el-icon>
  25. </span>
  26. <template #dropdown>
  27. <el-dropdown-menu>
  28. <el-dropdown-item>设置账号</el-dropdown-item>
  29. <el-dropdown-item>更改头像</el-dropdown-item>
  30. <el-dropdown-item @click="exit">退出登录</el-dropdown-item>
  31. </el-dropdown-menu>
  32. </template>
  33. </el-dropdown>
  34. </div>
  35. </template>
  36. <script setup lang="ts">
  37. import { ref, reactive, toRefs, computed } from 'vue'
  38. import { layoutStore } from '@/store/layout.js'
  39. import { ArrowDown } from '@element-plus/icons-vue'
  40. import { useRouter } from 'vue-router'
  41. const router = useRouter();
  42. const current = computed(() => {
  43. return router.currentRoute.value
  44. })
  45. console.log(router.currentRoute.value)
  46. const store = layoutStore();
  47. const show = ref(true)
  48. const change = function () {
  49. show.value = !show.value;
  50. store.changeisCollapse();
  51. }
  52. const state = reactive({
  53. circleUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
  54. })
  55. const { circleUrl } = toRefs(state)
  56. const exit = function () {
  57. router.push('/login')
  58. }
  59. </script>
  60. <style lang="scss" scoped>
  61. .title {
  62. width: 100px;
  63. margin: -22px 0px 0 28px;
  64. }
  65. .icon {
  66. margin-top: 15px;
  67. }
  68. .example-showcase .el-dropdown-link {
  69. cursor: pointer;
  70. color: var(--el-color-primary);
  71. display: flex;
  72. align-items: center;
  73. }
  74. .right {
  75. display: flex;
  76. padding-right: 10px;
  77. width: 110px;
  78. justify-content: space-between;
  79. align-items: center;
  80. float: right;
  81. margin-top: -32px;
  82. .el-icon--right {
  83. display: none;
  84. }
  85. }
  86. </style>

6.效果展示

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

闽ICP备14008679号