vue3后台系统项目-左侧导航菜单栏

1.创建目录结构

进入cmd,先cd到项目目录(项目vue3-project)

cd vue3-project
mkdir -p src\\views\\home\\components\\menubar

2.创建组件文件

3.编辑menu-item.content.vue

<template>
	<template v-if="item.icon">
	<i :class="`iconfont el-input__icon  ${item.icon}`" 
       :title="item.title" ></i>
	</template>
	<span :title="item.title">{{ item.title }}</span>
</template>

<script setup lang="ts">
import { computed } from 'vue';

const props= defineProps({
	// 菜单内容
	content: null,
});

// 获取父级菜单数据
const item = computed(() => {
	return props.content;
});

</script>
<style scoped lang="scss">
.el-menu i.iconfont {
	padding-bottom: 2px;
}
</style>

4.编辑menu-item.vue

<template>
	<el-menu-item v-if="!item.children || (item.children && item.children.length==0)" 
        :index="item.path" 
        :key="item.path" >
		<MenuItemContent :content="item" />
	</el-menu-item>

	<el-sub-menu v-else :index="item.path" :key="item.path">
		<template #title>
			<MenuItemContent :content="item" />
		</template>
		
		<o-menu v-for="data in item.children" 
                :index="data.path" 
                :key="data.path" 
                :content="data" />
	</el-sub-menu>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import MenuItemContent from './menu-item-content.vue';

defineOptions({
		name:"OMenu"
	}
);
const props= defineProps({
	// 菜单内容
	content: null,
});

// 获取父级菜单数据
const item = computed(() => {
	return props.content;
});


</script>
<style scoped lang="scss">

.el-menu-item.is-active, .el-menu-item:hover {
    color:white!important;
    background-color: #1890ff !important;
}

:deep(.el-sub-menu__title) {
  &:hover {
	color: #fff !important;
  }
}
</style>

5. 编辑menubar.vue

<template>
	<el-menu  router 
			  :default-active="$route.path"
			  :unique-opened="true" 
			  text-color='#ffffffb3' 
			  background-color="transparent">
		<template v-for="menu in menus">
			<MenuItem :content="menu" />
		</template>
	</el-menu>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { storeToRefs } from 'pinia';
import { useMenuStore } from '@/store/menu'
import MenuItem from './menu-item.vue';

const menuStore = useMenuStore()
const { menus } = storeToRefs(menuStore)

</script>
<style scoped lang="scss">

</style>

6.添加菜单数据类型定义

types/menu.d.ts

declare interface Meta{
	id: string;
	name: string;
	path: string;
	title: string;
}

declare interface MenuMeta extends Meta{
	icon?: string;
}

declare interface MenuJsonInfo  extends MenuMeta{
	parentId: number;
}

declare interface MenuInfo extends MenuMeta{
	children?: MenuInfo[];
}

// 菜单信息
declare interface MenuInfoState {
  activeName: string;
  menu: MenuInfo;
}

7.添加store

stroes/menu.ts

import {defineStore} from 'pinia';
import { MENU_ROOT,MENU_DEFAULT_ACTIVE } from '@/utils/constant';
/**
 * 
 * @methods 设置菜单信息
 */
export const useMenuStore = defineStore('menu',{
	persist: true,//本地保存设置
  state: (): MenuInfoState => ({
    activeName: MENU_DEFAULT_ACTIVE,
	menu: {
		id: '',
		name: '',
		title: '',
		path: '/',
		children: [],
		type: 0
	},
  }),
  
  getters:{
	menus(state){
		return state.menu.children;
	},
	root(state){
		return MENU_ROOT;
	},
	  
  },
  actions: {

    async gen(datas:MenuJsonInfo[]) {
	  const that = this;
      const map = {};
	  datas.forEach((item: MenuJsonInfo) =>{
		map[item.id] = {...item, children: [] };
	  });

	  const findParent = (parentId: number, item: MenuJsonInfo)=>{
		  if(parentId == 0 ){
				that.menu.children?.push(map[item.id]);
		  }else {
			  const parent = map[parentId.toString()];
			  if(parent){
				  parent.children.push(item);
			  }
		  }
	  };
	  
	  datas.forEach((item: MenuJsonInfo) =>{
		  findParent(item.parentId, map[item.id]);
	  });

    },

	clear(){
		this.menu.children =[];
		this.activeName = MENU_DEFAULT_ACTIVE;
	},

  },

  

});

展示菜单效果:

  • 15
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue2后台系统中,左菜单栏的折叠展开功能是指左侧导航菜单的可折叠和展开状态。这个功能通常用来优化界面显示,当导航菜单过长时,可以将其折叠起来,以节省页面空间,增强用户体验。 实现左菜单栏的折叠展开功能可以考虑以下步骤: 1. 在Vue组件中定义一个变量menuCollapsed来标记左菜单栏的折叠与展开状态,默认为false。 2. 在菜单栏的最外层div元素上绑定一个class,通过类名来控制菜单栏的宽度和显示方式。通过:class绑定判断menuCollapsed的值,来添加折叠和展开时的样式类。 3. 在菜单的折叠展开按钮上绑定一个点击事件,点击时改变menuCollapsed的值,实现菜单栏的折叠与展开。 4. 在菜单项的列表上根据menuCollapsed的值来决定是否显示菜单项的文字。 具体代码如下: ```html <template> <div class="menu-container" :class="{'collapsed': menuCollapsed}"> <button class="collapse-button" @click="toggleMenu"> {{ menuCollapsed ? '展开' : '折叠' }} </button> <ul class="menu-items"> <li v-for="item in menuItems" :key="item.id" :class="{'hidden-text': menuCollapsed}"> {{ item.title }} </li> </ul> </div> </template> <script> export default { data() { return { menuCollapsed: false, menuItems: [ { id: 1, title: '菜单项1' }, { id: 2, title: '菜单项2' }, { id: 3, title: '菜单项3' } ] } }, methods: { toggleMenu() { this.menuCollapsed = !this.menuCollapsed; } } } </script> <style> .menu-container { width: 200px; transition: width 0.3s; } .collapsed { width: 50px; } .collapse-button { margin-bottom: 10px; width: 100%; text-align: center; } .hidden-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } </style> ``` 以上代码实现了一个简单的左菜单栏的折叠展开功能。通过点击```.collapse-button```按钮,可以切换菜单栏的折叠与展开状态。菜单项目在折叠状态下会隐藏文字,并且用省略号表示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Oscar_0208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值