一、框架侧边栏改为顶部导航栏

1、复制src/layout/componets/Sidebar所有文件至同级目录,改名为Headbar

2、src/layout/components/index.js中声明Headbar

export { default as Headbar } from './Headbar'
3、修改Headbar/index.vue文件内容
更改el-menu的mode属性为horizontal
<template>
<div :class="{'has-logo':showLogo}">
<logo v-if="showLogo" :collapse="isCollapse" />
<el-scrollbar wrap-class="scrollbar-wrapper">
<el-menu
:default-active="activeMenu"
:collapse="isCollapse"
:background-color="variables.menuBg"
:text-color="variables.menuText"
:unique-opened="false"
:active-text-color="variables.menuActiveText"
:collapse-transition="false"
mode="horizontal"
>
<!-- <sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" /> -->
<!-- 动态路由 -->
<sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" /> </el-menu>
</el-scrollbar>
</div>
</template> <script>
import { mapGetters } from 'vuex'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from '@/styles/variables.scss' export default {
components: { SidebarItem, Logo },
computed: {
...mapGetters([
'permission_routes', // 动态路由
'sidebar'
]),
routes() {
return this.$router.options.routes
},
activeMenu() {
const route = this.$route
const { meta, path } = route
// if set path, the sidebar will highlight the path you set
if (meta.activeMenu) {
return meta.activeMenu
}
return path
},
showLogo() {
return this.$store.state.settings.sidebarLogo
},
variables() {
return variables
},
isCollapse() {
return !this.sidebar.opened
}
}
}
</script>
4、修改Headbar/SidebarItem.vue文件内容,主要配置stype、删除icon、增加固定宽度
<template>
<!-- style设置为inline-block,避免垂直布局的标题 -->
<div v-if="!item.hidden" style="display:inline-block;">
<template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
<item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" />
</el-menu-item>
</app-link>
</template> <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
<template slot="title">
<item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title" />
<!-- 增加固定宽度防止箭头被遮挡-->
<div style="display: inline-block; width:20px;"></div>
</template>
<sidebar-item
v-for="child in item.children"
:key="child.path"
:is-nest="true"
:item="child"
:base-path="resolvePath(child.path)"
class="nest-menu"
/>
</el-submenu>
</div>
</template> <script>
import path from 'path'
import { isExternal } from '@/utils/validate'
import Item from './Item'
import AppLink from './Link'
import FixiOSBug from './FixiOSBug' export default {
name: 'SidebarItem',
components: { Item, AppLink },
mixins: [FixiOSBug],
props: {
// route object
item: {
type: Object,
required: true
},
isNest: {
type: Boolean,
default: false
},
basePath: {
type: String,
default: ''
}
},
data() {
// To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
// TODO: refactor with render function
this.onlyOneChild = null
return {}
},
methods: {
hasOneShowingChild(children = [], parent) {
const showingChildren = children.filter(item => {
if (item.hidden) {
return false
} else {
// Temp set(will be used if only has one showing child)
this.onlyOneChild = item
return true
}
}) // When there is only one child router, the child router is displayed by default
if (showingChildren.length === 1) {
return true
} // Show parent if there are no child router to display
if (showingChildren.length === 0) {
this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
return true
} return false
},
resolvePath(routePath) {
if (isExternal(routePath)) {
return routePath
}
if (isExternal(this.basePath)) {
return this.basePath
}
return path.resolve(this.basePath, routePath)
}
}
}
</script>
5、修改src/layout/inex.vue
<template>
<div :class="classObj" class="app-wrapper">
<div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
<!--当 menuInLeft===true,左菜单栏显示,顶部隐藏-->
<sidebar class="sidebar-container" v-if="menuInLeft"/>
<!-- <div class="main-container"> 改为下面代码 -->
<div :class="{'main-container':menuInLeft}">
<div :class="{'fixed-header':fixedHeader}">
<navbar /> <!--当 menuInLeft===false,左菜单栏隐藏,顶部显示-->
<!-- <headbar v-if="!menuInLeft"/> -->
</div>
<app-main />
</div>
</div>
</template> <script>
// 引入Headbar
import { Navbar, Sidebar, Headbar, AppMain } from './components'
import ResizeMixin from './mixin/ResizeHandler'
import { mapState } from 'vuex' export default {
name: 'Layout',
components: {
Navbar,
Sidebar,
Headbar,
AppMain
},
mixins: [ResizeMixin],
computed: {
...mapState({
//获取menuInLeft值
menuInLeft: state => state.settings.menuInLeft
}),
sidebar() {
return this.$store.state.app.sidebar
},
device() {
return this.$store.state.app.device
},
fixedHeader() {
return this.$store.state.settings.fixedHeader
},
classObj() {
return {
hideSidebar: !this.sidebar.opened,
openSidebar: this.sidebar.opened,
withoutAnimation: this.sidebar.withoutAnimation,
mobile: this.device === 'mobile'
}
}
},
methods: {
handleClickOutside() {
this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
}
}
}
</script> <style lang="scss" scoped>
@import "~@/styles/mixin.scss";
@import "~@/styles/variables.scss"; .app-wrapper {
@include clearfix;
position: relative;
height: 100%;
width: 100%;
&.mobile.openSidebar{
position: fixed;
top: 0;
}
}
.drawer-bg {
background: #000;
opacity: 0.3;
width: 100%;
top: 0;
height: 100%;
position: absolute;
z-index: 999;
} .fixed-header {
position: fixed;
top: 0;
right: 0;
z-index: 9;
width: calc(100% - #{$sideBarWidth});
transition: width 0.28s;
} .hideSidebar .fixed-header {
width: calc(100% - 54px)
} .mobile .fixed-header {
width: 100%;
}
</style>
6、修改src/layout/components/Navbar.vue
<template>
<!-- 修改顶部导航背景色 -->
<div class="navbar" :style="{'background':menuInLeft?'':'#304156'}">
<!-- 修改左侧菜单才展示 -->
<hamburger v-if="menuInLeft"
:is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
<!-- 修改左侧菜单才展示 -->
<breadcrumb v-if="menuInLeft" class="breadcrumb-container" /> <!-- 增加顶部导航 -->
<headbar v-if="!menuInLeft" class="left-menu"/> <div class="right-menu">
<el-dropdown class="avatar-container" trigger="click">
<div class="avatar-wrapper">
<img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar">
<i class="el-icon-caret-bottom" />
</div>
<el-dropdown-menu slot="dropdown" class="user-dropdown">
<router-link to="/">
<el-dropdown-item>
Home
</el-dropdown-item>
</router-link>
<a target="_blank" href="https://aisuda.github.io/amis-editor-demo/">
<el-dropdown-item>Amis工具</el-dropdown-item>
</a>
<!-- <el-dropdown-item divided @click.native="logout">
<span style="display:block;">Log Out</span>
</el-dropdown-item> -->
</el-dropdown-menu>
</el-dropdown>
</div>
</div>
</template> <script>
import { mapGetters,mapState } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Headbar from './Headbar' export default {
components: {
Breadcrumb,
Hamburger,
Headbar
},
computed: {
...mapGetters([
'sidebar',
'avatar'
]),
...mapState({
menuInLeft: state => state.settings.menuInLeft
}),
},
methods: {
toggleSideBar() {
this.$store.dispatch('app/toggleSideBar')
},
async logout() {
await this.$store.dispatch('user/logout')
this.$router.push(`/login?redirect=${this.$route.fullPath}`)
}
}
}
</script> <style lang="scss" scoped>
.navbar {
height: 50px;
overflow: hidden;
position: relative;
background: #fff;
box-shadow: 0 1px 4px rgba(0,21,41,.08); .hamburger-container {
line-height: 46px;
height: 100%;
float: left;
cursor: pointer;
transition: background .3s;
-webkit-tap-highlight-color:transparent; &:hover {
background: rgba(0, 0, 0, .025)
}
} .breadcrumb-container {
float: left;
} // 左侧菜单样式
.left-menu{
float: left;
height: 100%;
} .right-menu {
float: right;
height: 100%;
line-height: 50px; &:focus {
outline: none;
} .right-menu-item {
display: inline-block;
padding: 0 8px;
height: 100%;
font-size: 18px;
color: #5a5e66;
vertical-align: text-bottom; &.hover-effect {
cursor: pointer;
transition: background .3s; &:hover {
background: rgba(0, 0, 0, .025)
}
}
} .avatar-container {
margin-right: 30px; .avatar-wrapper {
margin-top: 5px;
position: relative; .user-avatar {
cursor: pointer;
width: 40px;
height: 40px;
border-radius: 10px;
} .el-icon-caret-bottom {
cursor: pointer;
position: absolute;
right: -20px;
top: 25px;
font-size: 12px;
}
}
}
}
}
</style>
 

参考文档:https://blog.csdn.net/qq_36365860/article/details/120073481

vue-element-template实现顶部菜单栏的更多相关文章

  1. 在Vue+element 开发中报: The template root requires exactly one elemen 错的解决和原因

    一.我正准备使用Vue + Element进行新的项目开发,然后在进行添加下一个组件时报错  二.解决及原因: 原来template中只允许模板里存在一个根节点,在 template 中添加一个 &l ...

  2. Vue+Element实现网页版个人简历系统

    这篇文章介绍一个使用Vue+Element实现的个人简历系统,主要用到的技术有:vue.element.css3.css定位. 作者在window10进行开发,目前只在chrome上进行过测试,没有大 ...

  3. (vue.js)Vue element tab 每个tab用一个路由来管理?

    (vue.js)Vue element tab 每个tab用一个路由来管理? 来源:网络整理     时间:2017/5/13 0:24:01     关键词:   关于网友提出的“ (vue.js) ...

  4. 循序渐进VUE+Element 前端应用开发(5)--- 表格列表页面的查询,列表展示和字段转义处理

    在我们一般开发的系统界面里面,列表页面是一个非常重要的综合展示界面,包括有条件查询.列表展示和分页处理,以及对每项列表内容可能进行的转义处理,本篇随笔介绍基于Vue +Element基础上实现表格列表 ...

  5. 循序渐进VUE+Element 前端应用开发(6)--- 常规Element 界面组件的使用

    在我们开发BS页面的时候,往往需要了解常规界面组件的使用,小到最普通的单文本输入框.多文本框.下拉列表,以及按钮.图片展示.弹出对话框.表单处理.条码二维码等等,本篇随笔基于普通表格业务的展示录入的场 ...

  6. 循序渐进VUE+Element 前端应用开发(9)--- 界面语言国际化的处理

    我们开发的系统,一般可以不用考虑语言国际化的问题,大多数系统一般是给本国人使用的,而且直接使用中文开发界面会更加迅速 一些,不过框架最好能够支持国际化的处理,以便在需要的时候,可以花点时间来实现多语言 ...

  7. 循序渐进VUE+Element 前端应用开发(26)--- 各种界面组件的使用(2)

    在我们使用Vue+Element开发前端的时候,往往涉及到很多界面组件的使用,其中很多直接采用Element官方的案例即可,有些则是在这个基础上封装更好利用.更少代码的组件:另外有些则是直接采用第三方 ...

  8. Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成)

    Vue+Element的动态表单,动态表格(后端发送配置,前端动态生成) 动态表单生成 ElementUI官网引导 Element表单生成 Element动态增减表单,在线代码 关键配置 templa ...

  9. 前端小菜鸡使用Vue+Element笔记(一)

    关于使用Vue+Element的项目简介~ 最近因为项目组缺前端人员,所以自己现学现做页面,先把前后台功能调通 觉得前端可真的是不容易呀哎呀~ 首先记录一下相关的Vue入门的教程: vue环境搭建示例 ...

  10. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

随机推荐

  1. golang之热加载Fresh&air

    Fresh 是一个命令行工具,每次保存Go或模版文件时,该工具都会生成或重新启动Web应用程序.Fresh将监视文件事件,并且每次创建/修改/删除文件时,Fresh都会生成并重新启动应用程序.如果go ...

  2. linux模拟HID USB设备及wireshark USB抓包配置

    目录 1. 内核配置 2. 设备配置 附 wireshark USB抓包配置 笔者开发USB设备时的一些记录 1. 内核配置 内核启用USB Gadget,使用fs配置usb device信息. De ...

  3. AI让照片跳舞,人人都能是舞王!Swan下载介绍

    最近,兵马俑.马斯克以及各地网友跳科目三和网红舞的视频陆续在社交媒体和朋友圈刷屏,这些大约10秒左右的视频都不是真人出镜,均由大模型生成,这种低门槛的跳舞方式引发了网友的广泛体验,掀起了一波斗舞狂潮「 ...

  4. 基于surging的木舟平台如何分布式接入设备

    一.概述 上篇文章介绍了木舟通过基于木舟平台浅谈surging 的热点KEY的解决方法,那么此篇文章将介绍基于surging的木舟平台如何分布式接入设备. 木舟 (Kayak) 是什么? 木舟(Kay ...

  5. 多线程编程入门Thread_Task_async_await简单秒懂

    ` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u ...

  6. R机器学习:重复抽样在机器学习模型建立过程中的地位理解

    在做机器学习项目的时候,一开始我们会将数据集分为训练集和测试集,要记住测试集只能用一次,只能用来评估最终最好的模型.如果你反复去使用测试集,反复测试后从里面挑最好的,你就是在耍流氓. 建模过程中肯定有 ...

  7. Nuget Reference 丢失问题

    现象 在 Visual Studio 2017 中创建一个控制台项目.创建出来的项目如下所示. 通过 NuGet 管理器,添加 Newtonsoft.Json 的 NuGet 包,安装之后,项目中添加 ...

  8. 【人工智能】深度学习框架值TF入门-模型保存与加载

    资料:https://tensorflow.google.cn/tutorials/keras/save_and_load#选项 Keras的方式 Keras版本模型保存与加载 函数 保存模型权重:m ...

  9. mysql转换类型、类型转换、查询结果类型转换

    一.问题来源 数据库一张表的主键id设为了自增,那就是int型的,但是其他表的关联字段又设置成了字符串! 而且已经开发了很久才发现问题,既然出现了问题肯定需要解决 如图 很明显id是不一样的,花了点时 ...

  10. Qt音视频开发33-vlc和mpv打开后鼠标打圈圈问题的解决

    一.前言 如果采用的vlc句柄模式,如果鼠标停留在句柄控件中会发现在打开后鼠标打圈圈,mpv句柄模式是在关闭后鼠标打圈圈,这两者真是一前一后,这种给人的体验其实很不友好的,播放开始后或者播放完成后鼠标 ...