当前位置:   article > 正文

vue-element-admin实现顶部菜单栏,并可互相切换_vue 顶部菜单

vue 顶部菜单

目录

零、先上传效果图

一、配置顶部菜单

二、配置右侧切换菜单位置按钮

三、配置index.vue和Navbar.vue


零、先上传效果图

 一、配置顶部菜单

1.复制一份src/layout/componets/Sidebar所有文件至同级目录,改名为Headbar,并在src/layout/components/index.js中声明Headbar

声明Headbar

2.修改Headbar/index.vue文件内容,主要删除了logo和更改了el-menu的mode属性为horizontal,并将name更改为Headbar;

  1. <template>
  2. <el-scrollbar wrap-class="scrollbar-wrapper">
  3. <el-menu
  4. :default-active="activeMenu"
  5. :background-color="variables.menuBg"
  6. :text-color="variables.menuText"
  7. :active-text-color="variables.menuActiveText"
  8. mode="horizontal">
  9. <sidebar-item
  10. v-for="route in permission_routes"
  11. :key="route.path"
  12. :item="route"
  13. :base-path="route.path"/>
  14. </el-menu>
  15. </el-scrollbar>
  16. </template>
  17. <script>
  18. import {mapGetters} from 'vuex'
  19. import SidebarItem from './SidebarItem'
  20. import variables from '@/styles/variables.scss'
  21. export default {
  22. name: 'Headbar',
  23. components: {SidebarItem},
  24. computed: {
  25. ...mapGetters([
  26. 'permission_routes',
  27. 'sidebar'
  28. ]),
  29. activeMenu() {
  30. const route = this.$route
  31. const {meta, path} = route
  32. // if set path, the sidebar will highlight the path you set
  33. if (meta.activeMenu) {
  34. return meta.activeMenu
  35. }
  36. return path
  37. },
  38. showLogo() {
  39. return this.$store.state.settings.sidebarLogo
  40. },
  41. variables() {
  42. return variables
  43. },
  44. isCollapse() {
  45. return !this.sidebar.opened
  46. }
  47. }
  48. }
  49. </script>

 3.修改Headbar/SidebarItem.vue文件内容,主要配置stype、删除icon、增加固定宽度;

  1. <template>
  2. <!-- style设置为inline-block,避免垂直布局的标题-->
  3. <div v-if="!item.hidden" style="display:inline-block;">
  4. <template
  5. v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
  6. <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
  7. <el-menu-item :index="resolvePath(onlyOneChild.path)">
  8. <item :title="generateTitle(onlyOneChild.meta.title)"/>
  9. </el-menu-item>
  10. </app-link>
  11. </template>
  12. <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" >
  13. <template slot="title">
  14. <item v-if="item.meta" :title="generateTitle(item.meta.title)"/>
  15. <!-- 增加固定宽度防止箭头被遮挡-->
  16. <div style="display: inline-block; width:20px;"></div>
  17. </template>
  18. <sidebar-item
  19. v-for="child in item.children"
  20. :key="child.path"
  21. :is-nest="true"
  22. :item="child"
  23. :base-path="resolvePath(child.path)"
  24. class="nest-menu"
  25. />
  26. </el-submenu>
  27. </div>
  28. </template>
  29. <script>
  30. import path from 'path'
  31. import {generateTitle} from '@/utils/i18n'
  32. import {isExternal} from '@/utils/validate'
  33. import Item from './Item'
  34. import AppLink from './Link'
  35. import FixiOSBug from './FixiOSBug'
  36. import SidebarItem from "./SidebarItem";
  37. export default {
  38. name: 'SidebarItem',
  39. components: {Item, AppLink, SidebarItem},
  40. mixins: [FixiOSBug],
  41. props: {
  42. // route object
  43. item: {
  44. type: Object,
  45. required: true
  46. },
  47. isNest: {
  48. type: Boolean,
  49. default: false
  50. },
  51. basePath: {
  52. type: String,
  53. default: ''
  54. }
  55. },
  56. data() {
  57. // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
  58. // TODO: refactor with render function
  59. this.onlyOneChild = null
  60. return {}
  61. },
  62. methods: {
  63. hasOneShowingChild(children = [], parent) {
  64. const showingChildren = children.filter(item => {
  65. if (item.hidden) {
  66. return false
  67. } else {
  68. // Temp set(will be used if only has one showing child)
  69. this.onlyOneChild = item
  70. return true
  71. }
  72. })
  73. // When there is only one child router, the child router is displayed by default
  74. if (showingChildren.length === 1) {
  75. return true
  76. }
  77. // Show parent if there are no child router to display
  78. if (showingChildren.length === 0) {
  79. this.onlyOneChild = {...parent, path: '', noShowingChildren: true}
  80. return true
  81. }
  82. return false
  83. },
  84. resolvePath(routePath) {
  85. if (isExternal(routePath)) {
  86. return routePath
  87. }
  88. if (isExternal(this.basePath)) {
  89. return this.basePath
  90. }
  91. return path.resolve(this.basePath, routePath)
  92. },
  93. generateTitle
  94. }
  95. }
  96. </script>

二、配置右侧切换菜单位置按钮

1.在src/settings.js中增加menuInLeft 

 2.在右侧边栏中增加切换按钮,src/layout/components/Settings/index.vue

  1. <template>
  2. <div class="drawer-container">
  3. <div>
  4. <!--原有标签保持不变,此处省略-->
  5. <!--增加切换按钮-->
  6. <div class="drawer-item">
  7. <span>切换菜单位置</span>
  8. <el-switch v-model="menuInLeft" class="drawer-switch" />
  9. </div>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. import ThemePicker from '@/components/ThemePicker'
  15. export default {
  16. components: { ThemePicker },
  17. data() {
  18. return {}
  19. },
  20. computed: {
  21. // ===原有参数保持不变,此处省略====
  22. // 增加绑定的menuInLeft值
  23. menuInLeft: {
  24. get() {
  25. return this.$store.state.settings.menuInLeft
  26. },
  27. set(val) {
  28. this.$store.dispatch('settings/changeSetting', {
  29. key: 'menuInLeft',
  30. value: val
  31. })
  32. }
  33. },
  34. },
  35. methods: {
  36. themeChange(val) {
  37. this.$store.dispatch('settings/changeSetting', {
  38. key: 'theme',
  39. value: val
  40. })
  41. }
  42. }
  43. }
  44. </script>
  45. <style lang="scss" scoped>
  46. /*原有样式保持不变,此处省略*/
  47. </style>

3.引入menuInLeft,src/store/modules/settings.js

三、配置index.vue和Navbar.vue

1.配置index.vue(src/layout/inex.vue)

  1. <template>
  2. <div :class="classObj" class="app-wrapper">
  3. <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside"/>
  4. <!--当 menuInLeft===true,左菜单栏显示,顶部隐藏-->
  5. <sidebar class="sidebar-container" v-if="menuInLeft"/>
  6. <!--当左菜单栏显示时,需配置main-container 样式,改样式位置在src/styles/sidebar.scss文件中-->
  7. <div :class="{hasTagsView:needTagsView,'main-container':menuInLeft}">
  8. <div :class="{'fixed-header':fixedHeader}">
  9. <navbar/>
  10. <!--当 menuInLeft===false,左菜单栏隐藏,顶部显示-->
  11. <headbar v-if="!menuInLeft"/>
  12. <tags-view v-if="needTagsView"/>
  13. </div>
  14. <app-main/>
  15. <right-panel v-if="showSettings">
  16. <settings/>
  17. </right-panel>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. import RightPanel from '@/components/RightPanel'
  23. //引入Sidebar
  24. import {AppMain, Navbar, Settings, Headbar, Sidebar, TagsView} from './components'
  25. import ResizeMixin from './mixin/ResizeHandler'
  26. import {mapState} from 'vuex'
  27. export default {
  28. name: 'Layout',
  29. components: {
  30. AppMain,
  31. Navbar,
  32. RightPanel,
  33. Settings,
  34. Sidebar,//引入Sidebar
  35. Headbar,
  36. TagsView
  37. },
  38. mixins: [ResizeMixin],
  39. computed: {
  40. ...mapState({
  41. sidebar: state => state.app.sidebar,
  42. device: state => state.app.device,
  43. showSettings: state => state.settings.showSettings,
  44. needTagsView: state => state.settings.tagsView,
  45. fixedHeader: state => state.settings.fixedHeader,
  46. //获取menuInLeft值
  47. menuInLeft: state => state.settings.menuInLeft
  48. }),
  49. classObj() {
  50. return {
  51. hideSidebar: !this.sidebar.opened,
  52. openSidebar: this.sidebar.opened,
  53. withoutAnimation: this.sidebar.withoutAnimation,
  54. mobile: this.device === 'mobile'
  55. }
  56. }
  57. },
  58. methods: {
  59. handleClickOutside() {
  60. this.$store.dispatch('app/closeSideBar', {withoutAnimation: false})
  61. }
  62. }
  63. }
  64. </script>

 2、配置Navbar.vue(src/layout/components/Navbar.vue)

  1. <template>
  2. <div class="navbar">
  3. <!--注释 侧边栏展开关闭按钮-->
  4. <hamburger v-if="menuInLeft"
  5. id="hamburger-container"
  6. :is-active="sidebar.opened"
  7. class="hamburger-container"
  8. @toggleClick="toggleSideBar"/>
  9. <breadcrumb id="breadcrumb-container" class="breadcrumb-container"/>
  10. <div class="right-menu">
  11. <template v-if="device!=='mobile'">
  12. <search id="header-search" class="right-menu-item"/>
  13. <error-log class="errLog-container right-menu-item hover-effect"/>
  14. <screenfull id="screenfull" class="right-menu-item hover-effect"/>
  15. <el-tooltip :content="$t('navbar.size')" effect="dark" placement="bottom">
  16. <size-select id="size-select" class="right-menu-item hover-effect"/>
  17. </el-tooltip>
  18. <lang-select class="right-menu-item hover-effect"/>
  19. </template>
  20. <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
  21. <div class="avatar-wrapper">
  22. <img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar">
  23. <i class="el-icon-caret-bottom"/>
  24. </div>
  25. <el-dropdown-menu slot="dropdown">
  26. <router-link to="/profile/index">
  27. <el-dropdown-item>
  28. {{ $t('navbar.profile') }}
  29. </el-dropdown-item>
  30. </router-link>
  31. <router-link to="/">
  32. <el-dropdown-item>
  33. {{ $t('navbar.dashboard') }}
  34. </el-dropdown-item>
  35. </router-link>
  36. <a target="_blank" href="https://github.com/PanJiaChen/vue-element-admin/">
  37. <el-dropdown-item>
  38. {{ $t('navbar.github') }}
  39. </el-dropdown-item>
  40. </a>
  41. <a target="_blank" href="https://panjiachen.github.io/vue-element-admin-site/#/">
  42. <el-dropdown-item>Docs</el-dropdown-item>
  43. </a>
  44. <el-dropdown-item divided @click.native="logout">
  45. <span style="display:block;">{{ $t('navbar.logOut') }}</span>
  46. </el-dropdown-item>
  47. </el-dropdown-menu>
  48. </el-dropdown>
  49. </div>
  50. </div>
  51. </template>
  52. <script>
  53. import {mapGetters, mapState} from 'vuex'
  54. import Breadcrumb from '@/components/Breadcrumb'
  55. import Hamburger from '@/components/Hamburger'
  56. import ErrorLog from '@/components/ErrorLog'
  57. import Screenfull from '@/components/Screenfull'
  58. import SizeSelect from '@/components/SizeSelect'
  59. import LangSelect from '@/components/LangSelect'
  60. import Search from '@/components/HeaderSearch'
  61. export default {
  62. components: {
  63. Breadcrumb,
  64. Hamburger,
  65. ErrorLog,
  66. Screenfull,
  67. SizeSelect,
  68. LangSelect,
  69. Search,
  70. },
  71. computed: {
  72. ...mapGetters([
  73. 'sidebar',
  74. 'avatar',
  75. 'device'
  76. ]),
  77. ...mapState({
  78. menuInLeft: state => state.settings.menuInLeft
  79. }),
  80. },
  81. methods: {
  82. toggleSideBar() {
  83. this.$store.dispatch('app/toggleSideBar')
  84. },
  85. async logout() {
  86. await this.$store.dispatch('user/logout')
  87. this.$router.push(`/login?redirect=${this.$route.fullPath}`)
  88. }
  89. }
  90. }
  91. </script>
  92. <style lang="scss" scoped>
  93. .navbar {
  94. height: 50px;
  95. overflow: hidden;
  96. position: relative;
  97. background: #fff;
  98. box-shadow: 0 1px 4px rgba(0, 21, 41, .08);
  99. .hamburger-container {
  100. line-height: 46px;
  101. height: 100%;
  102. float: left;
  103. cursor: pointer;
  104. transition: background .3s;
  105. -webkit-tap-highlight-color: transparent;
  106. &:hover {
  107. background: rgba(0, 0, 0, .025)
  108. }
  109. }
  110. .breadcrumb-container {
  111. float: left;
  112. }
  113. .errLog-container {
  114. display: inline-block;
  115. vertical-align: top;
  116. }
  117. .right-menu {
  118. float: right;
  119. height: 100%;
  120. line-height: 50px;
  121. &:focus {
  122. outline: none;
  123. }
  124. .right-menu-item {
  125. display: inline-block;
  126. padding: 0 8px;
  127. height: 100%;
  128. font-size: 18px;
  129. color: #5a5e66;
  130. vertical-align: text-bottom;
  131. &.hover-effect {
  132. cursor: pointer;
  133. transition: background .3s;
  134. &:hover {
  135. background: rgba(0, 0, 0, .025)
  136. }
  137. }
  138. }
  139. .avatar-container {
  140. margin-right: 30px;
  141. .avatar-wrapper {
  142. margin-top: 5px;
  143. position: relative;
  144. .user-avatar {
  145. cursor: pointer;
  146. width: 40px;
  147. height: 40px;
  148. border-radius: 10px;
  149. }
  150. .el-icon-caret-bottom {
  151. cursor: pointer;
  152. position: absolute;
  153. right: -20px;
  154. top: 25px;
  155. font-size: 12px;
  156. }
  157. }
  158. }
  159. }
  160. }
  161. </style>

-----------------------------------------------------------分割线-------------------------------------------------------------

说明:

上面文章中用的是vue-element-amdin的,i18n版本(增加了多语言功能),generateTitle方法是原项目默认的,在代码的import位置进行了引用。

评论中的百度云源代码使用的是项目的master版本,里面没有generateTitle。

-----------------------------------------------------------分割线-------------------------------------------------------------

四、将顶部菜单放入到navbar中

        

 直接在layout/components/Navbar.vue中引入 headbar,并加入headbar标签即可。效果如下:

 由于顶部位置有限,在菜单过多时,会存在放不下的情况。代码中为了将位置空出来,我把breadrumb标签进行了隐藏,不过还是没有放下…………

最好的解决方式是让顶部菜单能够左右滑动,目前还没有去调试加上,先进行记录一下, 后面调试出来了再加。

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

闽ICP备14008679号