基于vue3.0和element-plus的组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.2 KiB

2 years ago
<template>
<el-menu :default-active="active" router :mode="mode" :class="mode" :text-color="state.style.menuColor"
:background-color="mode == 'horizontal' ? state.style.headBg : state.style.menuBg"
:active-text-color="state.style.menuActiveColor" :size="state.size.size">
<template v-for="menu in data || state.menus">
2 years ago
<template v-if="menu.children && menu.children.length > 0">
<el-sub-menu :index="getPath(menu)" :key="menu.path">
2 years ago
<template #title>
<component class="icon" :is="menu.icon || state.style.menuDefaultIcon">
</component>
2 years ago
<span>{{ menu.title || t(menu.i18n) }}</span>
2 years ago
</template>
<el-menu-item v-for="child in menu.children" :index="getPath(child)" :router="router">
<component class="icon" :width="getters.menuIconSize" :height="getters.menuIconSize"
:is="child.icon || state.style.menuDefaultIcon">
</component>
2 years ago
{{ child.title || t(child.i18n) }}
</el-menu-item>
2 years ago
</el-sub-menu>
</template>
<template v-else>
<el-menu-item :index="getPath(menu)" :key="menu.path" :router="router">
2 years ago
<component class="icon" :is="menu.icon || state.style.menuDefaultIcon"></component>
<span>{{ menu.title || t(menu.i18n) }}</span>
2 years ago
</el-menu-item>
</template>
</template>
</el-menu>
</template>
<script setup lang="ts">
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { ref, onMounted, reactive } from "vue"
2 years ago
import { useI18n } from "vue3-i18n";
const { t } = useI18n();
2 years ago
const { state, getters } = useStore()
2 years ago
const props = defineProps({
mode: {
type: String,
default: 'vertical'
},
2 years ago
data: {
type: Array<any>(),
default: null
},
});
const style = reactive({ 'margin-right': '10px', width: getters.menuIconSize, height: getters.menuIconSize })
2 years ago
const router = useRouter();
const getPath = (menu) => {
const prefix = menu.path.startsWith("/") ? "" : "/";
return prefix + menu.path;
2 years ago
};
const active = ref("");
onMounted(() => {
2 years ago
setTimeout(() => {
active.value = router.currentRoute.value.path.substring(1);
}, 200);
2 years ago
router.push(active.value);
2 years ago
})
</script>
<style scoped lang="scss">
.horizontal {
padding-top: 1px;
height: v-bind('state.size.headHeight');
border: none;
// display: inline-flex;
}
.vertical {
2 years ago
padding-top: 1px;
2 years ago
height: v-bind('state.size.mainHeight');
2 years ago
border: none;
2 years ago
}
2 years ago
.icon {
margin-right: 10px;
width: v-bind('state.size.menuIconSize');
height: v-bind('state.size.menuIconSize');
}
2 years ago
.el-menu-item.is-active {
background-color: v-bind('state.style.menuActiveBg');
2 years ago
}
.vertical .el-sub-menu,
.vertical .el-menu-item {
2 years ago
min-width: v-bind('state.size.asideWidth') !important;
font-size: v-bind('state.size.fontSize') !important;
2 years ago
}
.horizontal .el-sub-menu,
.horizontal .el-menu-item {
font-size: v-bind('state.size.fontSize') !important;
}
2 years ago
</style>