自定义菜单组件修复完毕,注意:如果超过三层结构,需要递归组件

prod
许宏杰 1 year ago
parent f0ae5c391e
commit 1608e82020

@ -1,15 +1,16 @@
<template> <template>
<el-dropdown trigger="hover"> <el-menu
<div class="menu-item"> class="el-menu-vertical-demo"
{{ item.meta.title }} @select="selectRouter"
</div> :default-active="$route.path"
<el-dropdown-menu slot="dropdown"> >
<el-dropdown-item <template v-for="(secondRoute, secondIndex) in item.children">
@click.native="handlePath(secondRoute, secondIndex)" <el-menu-item
v-for="(secondRoute, secondIndex) in item.children" :index="secondRoute.fullPath"
:key="secondIndex" :key="secondIndex"
v-if="!secondRoute.children"
> >
<section v-if="!secondRoute.children"> <template slot="title">
<svg-icon <svg-icon
v-if=" v-if="
secondRoute.meta && secondRoute.meta &&
@ -19,16 +20,36 @@
:icon-class="secondRoute.meta.icon" :icon-class="secondRoute.meta.icon"
/> />
{{ secondRoute.meta.title }} {{ secondRoute.meta.title }}
</section> </template>
<section v-else> </el-menu-item>
<menu-item <el-submenu v-else :index="item.fullPath" :key="secondRoute.fullPath">
:item="secondRoute" <template slot="title">
:base-path="secondRoute.path" <svg-icon
></menu-item> v-if="
</section> secondRoute.meta &&
</el-dropdown-item> secondRoute.meta.icon &&
</el-dropdown-menu> secondRoute.meta.icon !== '#'
</el-dropdown> "
:icon-class="secondRoute.meta.icon"
/>
{{ secondRoute.meta.title }}
</template>
<el-menu-item
v-for="(nextItem, nextIndex) in secondRoute.children"
:key="nextIndex"
:index="nextItem.fullPath"
>
<svg-icon
v-if="
nextItem.meta && nextItem.meta.icon && nextItem.meta.icon !== '#'
"
:icon-class="nextItem.meta.icon"
/>
{{ nextItem.meta.title }}
</el-menu-item>
</el-submenu>
</template>
</el-menu>
</template> </template>
<script> <script>
@ -39,25 +60,36 @@ export default {
type: Object, type: Object,
required: true, required: true,
}, },
basePath: {
type: String,
default: "",
},
}, },
data() { data() {
return {}; return {};
}, },
mounted() {}, mounted() {
console.log(this.item);
},
methods: { methods: {
selectRouter(index, indexPath) {
console.log(index, "aaa");
this.$router.push(index);
},
filterActiveCss(path) {
return this.$route.path.indexOf(path) > -1;
},
handlePath(item, index) { handlePath(item, index) {
console.log(item, "aaa"); if (item.children) return;
// this.$router.push(item.path); this.$router.push(item.fullPath);
}, },
}, },
}; };
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.el-menu-vertical-demo {
overflow: hidden;
li {
width: 160px;
}
}
.menu-item { .menu-item {
font-size: 18px; font-size: 18px;
color: #ffffff; color: #ffffff;
@ -68,4 +100,14 @@ export default {
padding: 3px 12px; padding: 3px 12px;
cursor: pointer; cursor: pointer;
} }
.activeCss {
color: #192a92;
}
::v-deep .el-menu-item,
::v-deep .el-submenu__title {
height: 35px;
line-height: 35px;
padding: 0 15px;
}
</style> </style>

@ -3,77 +3,95 @@
<div <div
v-for="(item, index) in topMenus" v-for="(item, index) in topMenus"
:key="index" :key="index"
:class="isCurrentRoute(item.path) ? 'activeMenu' : ''" :class="isCurrentRoute(item.fullPath) ? 'activeMenu' : ''"
@click="handlerRouter(item.path)"
> >
<div class="menu-item" v-if="!item.children"> <div
class="menu-item"
v-if="!item.children"
@click="handlerRouter(item.fullPath)"
>
{{ item.meta.title }} {{ item.meta.title }}
</div> </div>
<menu-item v-else :item="item" :base-path="item.path"></menu-item> <el-dropdown trigger="click" v-else>
<div class="menu-item">
{{ item.meta.title }}
</div>
<el-dropdown-menu slot="dropdown">
<menu-item :item="item"></menu-item>
</el-dropdown-menu>
</el-dropdown>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import { mapGetters } from "vuex";
import MenuItem from "../MenuItem"; import MenuItem from "../MenuItem";
import { constantRoutes } from "@/router"; import { mapGetters } from "vuex";
// //
export default { export default {
data() { data() {
return {}; return {
topMenus: [],
};
}, },
components: { components: {
MenuItem, MenuItem,
}, },
mounted() { mounted() {
this.childrenMenus(); this.filterRouter();
}, },
methods: { methods: {
// filterRouter() {
childrenMenus() { this.sidebarRouters.forEach((item) => {
var childrenMenus = []; if (item.hidden !== true && item.redirect != "index") {
this.routers.map((router) => { if (item.path === "/") {
for (var item in router.children) { this.topMenus.push(item.children[0]);
if (router.children[item].parentPath === undefined) { } else {
if (router.path === "/") { this.topMenus.push(item);
router.children[item].path = "/" + router.children[item].path;
} else {
if (!this.ishttp(router.children[item].path)) {
router.children[item].path =
router.path + "/" + router.children[item].path;
}
}
router.children[item].parentPath = router.path;
} }
childrenMenus.push(router.children[item]);
} }
}); });
return constantRoutes.concat(childrenMenus); this.addParentId(this.topMenus, null);
},
addParentId(tree, parentPath) {
tree.forEach((node) => {
// node.parentPath = node.path + parentPath; // id
let merge = parentPath
? this.filterPath(parentPath) + this.filterPath(node.path)
: this.filterPath(node.path);
node.fullPath = merge;
if (node.children && node.children.length > 0) {
this.addParentId(node.children, node.fullPath); // id id
}
});
return tree;
},
filterPath(path) {
path = path.charAt(0) === "/" ? path : "/" + path;
return path;
}, },
// //
isCurrentRoute(key) { isCurrentRoute(key) {
return this.$route.path === key; return this.$route.path.indexOf(key) > -1;
}, },
handlerRouter(key) { handlerRouter(key) {
console.log(key); console.log(key, "Path");
const route = this.routers.find((item) => item.path === key);
if (this.ishttp(key)) { if (this.ishttp(key)) {
// //
window.open(key, "_blank"); window.open(key, "_blank");
} else if (!route || !route.children) { } else {
// this.$router.push({ path: key });
const routeMenu = this.childrenMenus().find(
(item) => item.path === key // if (routeMenu && routeMenu.query) {
); // let query = JSON.parse(routeMenu.query);
if (routeMenu && routeMenu.query) { // this.$router.push({ path: key, query: query });
let query = JSON.parse(routeMenu.query); // } else {
this.$router.push({ path: key, query: query });
} else { // }
this.$router.push({ path: key });
}
} }
}, },
@ -87,25 +105,20 @@ export default {
return this.$store.state.settings.theme; return this.$store.state.settings.theme;
}, },
// //
topMenus() { // topMenus() {
let topMenus = []; // let topMenus = [];
this.sidebarRouters.map((menu) => { // this.sidebarRouters.map((menu) => {
if (menu.hidden !== true && menu.redirect !== "index") { // if (menu.hidden !== true && menu.redirect !== "index") {
// // //
if (menu.path === "/") { // if (menu.path === "/") {
topMenus.push(menu.children[0]); // topMenus.push(menu.children[0]);
} else { // } else {
topMenus.push(menu); // topMenus.push(menu);
} // }
} // }
}); // });
// return topMenus;
return topMenus; // },
},
//
routers() {
return this.$store.state.permission.topbarRouters;
},
}, },
}; };
</script> </script>

Loading…
Cancel
Save