vue-element-admin实现顶部菜单栏,并可互相切换
⽬录
零、先上传效果图
⼀、配置顶部菜单
1.复制⼀份src/layout/componets/Sidebar所有⽂件⾄同级⽬录,改名为Headbar,并在src/layout/components/index.js中声明Headbar
声明Headbar
2.修改Headbar/index.vue⽂件内容,主要删除了logo和更改了el-menu的mode属性为horizontal,并将name更改为Headbar;
<template>
<el-scrollbar wrap-class="scrollbar-wrapper">
<el-menu
:default-active="activeMenu"
:background-color="uBg"
:text-color="uText"
:active-text-color="uActiveText"
mode="horizontal">
<sidebar-item
v-for="route in permission_routes"
:key="route.path"
:
item="route"
:base-path="route.path"/>
</el-menu>
</el-scrollbar>
</template>
<script>
import {mapGetters} from 'vuex'
import SidebarItem from './SidebarItem'
import variables from '@/styles/variables.scss'
export default {
name: 'Headbar',
components: {SidebarItem},
computed: {
...mapGetters([
'permission_routes',
'sidebar'
]),
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>
3.修改Headbar/SidebarItem.vue⽂件内容,主要配置stype、删除icon、增加固定宽度;
<template>
<!-- style设置为inline-block,避免垂直布局的标题-->
<div v-if="!item.hidden" >
<template
v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||Showing
Children)&&!item.alwaysShow"> <app-link v-if="a" :to="resolvePath(onlyOneChild.path)">
<el-menu-item :index="resolvePath(onlyOneChild.path)">
<el-menu-item :index="resolvePath(onlyOneChild.path)">
<item :title="a.title)"/>
</el-menu-item>
</app-link>
</template>
<el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" > <template slot="title">
<item v-if="a" :title="a.title)"/>
<!-- 增加固定宽度防⽌箭头被遮挡-->
<div ></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 {generateTitle} from '@/utils/i18n'
import {isExternal} from '@/utils/validate'
import Item from './Item'
import AppLink from './Link'
import FixiOSBug from './FixiOSBug'
import SidebarItem from "./SidebarItem";
export default {
name: 'SidebarItem',
components: {Item, AppLink, SidebarItem},
mixins: [FixiOSBug],
props: {
// route object
item: {
type: Object,
required: true
},
isNest: {
type: Boolean,
default: false
},
basePath: {
type: String,
default: ''
}
},
data() {
// To fix github/PanJiaChen/vue-admin-template/issues/237 // TODO: refactor with render function
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)
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) {
return true
}
return false
},
resolvePath(routePath) {
if (isExternal(routePath)) {
return routePath
}
if (isExternal(this.basePath)) {
return this.basePath
}
solve(this.basePath, routePath)
},
generateTitle
}
}
</script>
⼆、配置右侧切换菜单位置按钮
1.在src/settings.js中增加menuInLeft
2.在右侧边栏中增加切换按钮,src/layout/components/Settings/index.vue
<template>
<div class="drawer-container">vue element admin
<div>
<!--原有标签保持不变,此处省略-->
<!--增加切换按钮-->
<div class="drawer-item">
<span>切换菜单位置</span>
<el-switch v-model="menuInLeft" class="drawer-switch" /> </div>
</div>
</div>
</template>
<script>
import ThemePicker from '@/components/ThemePicker'
export default {
components: { ThemePicker },
data() {
return {}
},
computed: {
// ===原有参数保持不变,此处省略====
// 增加绑定的menuInLeft值
menuInLeft: {
get() {
return this.$store.uInLeft
},
set(val) {
this.$store.dispatch('settings/changeSetting', {
key: 'menuInLeft',
value: val
})
}
},
},
methods: {
themeChange(val) {
this.$store.dispatch('settings/changeSetting', {
key: 'theme',
value: val
})
}
}
}
</script>
<style lang="scss" scoped>
/*原有样式保持不变,此处省略*/
</style>
3.引⼊menuInLeft,src/store/modules/settings.js
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论