element-plus+vue3项目(侧边栏菜单的使用和历史记录切换问题的解决(高点效果对应不上))

本文主要是介绍element-plus+vue3项目(侧边栏菜单的使用和历史记录切换问题的解决(高点效果对应不上)),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、使用element-plus的菜单,侧边栏类型

 导入element-plus,安装方式有如下几种:

# 选择一个你喜欢的包管理器# NPM
$ npm install element-plus --save# Yarn
$ yarn add element-plus# pnpm
$ pnpm install element-plus

在main.js引入和使用:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import '../src/assets/static/css/normalize.css'
import App from './App.vue'
import router from '../routers/router'
// 引入element-plus和样式
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'const app = createApp(App)
app.use(router).use(ElementPlus).use(createPinia()).mount('#app')

我的实际运用代码:

<template><div class="container"><!-- 菜单侧边栏 --><div class="bar"><!-- 系统名称 --><div class="system-title">智慧物业管理平台</div><el-menuactive-text-color="#ffd04b"background-color="#545c64"class="el-menu-vertical-demo":default-active="mainStore.currentSelected"text-color="#fff"@open="handleOpen"@close="handleClose"style="height: calc(100vh - 52px)"router><el-menu-item index="notice"><el-icon><Bell /></el-icon><span>整改通知</span></el-menu-item><el-menu-item index="projectManagement"><el-icon><Document /></el-icon><span>项目管理</span></el-menu-item><el-menu-item index="supplierManagement"><el-icon><Shop /></el-icon><span>供应商管理</span></el-menu-item><el-sub-menu index="personnel" ><!-- index属性用于指定子菜单的唯一标识符 --><template #title><el-icon><User /></el-icon><span>人事管理</span></template><el-menu-item index="personnelList">人员列表</el-menu-item><!-- <el-menu-item index="departmentManagement">部门管理</el-menu-item><el-menu-item index="postManagement">岗位管理</el-menu-item> --></el-sub-menu><el-menu-item index="dataManagement"><el-icon><DataLine /></el-icon><span>数据管理</span></el-menu-item><el-menu-item index="roleManagement"><el-icon><Avatar /></el-icon><span>角色管理</span></el-menu-item></el-menu></div><!-- 内容区域 --><div class="content"><div class="main"><router-view></router-view></div></div></div>
</template>

注意:这里的侧边栏用到了图标,如果不导入一下就会显示警告,为了让控制不显示乱七八糟的还是导入一下,不然看着也不舒服。

import {Bell,Document,Shop,User,Avatar,DataLine,
} from "@element-plus/icons-vue";

因为我这里涉及到二级菜单,所以对应的路由是这样设计的。

import { createRouter, createWebHistory } from "vue-router";
import Home from "../pages/Home.vue";
import Notice from "../pages/Notice.vue";
import PersonnelList from "../pages/Personnel-list.vue";
import ProjectManagement from "../pages/Project-management.vue";
import RoleManagement from "../pages/Role-management.vue";
import SupplierManagement from "../pages/Supplier-management.vue";
import DataManagement from "../pages/Data-management.vue";
import DepartmentManagement from "../pages/Department-management.vue";
import PostManagement from "../pages/Post-management.vue";
import SendNotifications from "../pages/Send-notifications.vue";
import AddAssociatedItems from "../pages/AddAssociatedItems.vue";
import CreateProject from "../pages/Create-project.vue";
import ProjectSet from "../pages/Project-set.vue";const routes = [{path: "/",name: "Home",component: Home,children: [// 将默认路由重定向为notice页面{path: "/",redirect: "/notice",},{path: "/notice",name: "notice",component: Notice,},{path: "/projectManagement",name: "projectManagement",component: ProjectManagement,},{path: "/supplierManagement",name: "supplierManagement",component: SupplierManagement,},{path: "/personnelList",name: "personnelList",component: PersonnelList,},{path: "/departmentManagement",name: "departmentManagement",component: DepartmentManagement,},{path: "/postManagement",name: "postManagement",component: PostManagement,},{path: "/dataManagement",name: "dataManagement",component: DataManagement,},{path: "/roleManagement",name: "roleManagement",component: RoleManagement,},],},{path: "/notice/sendNotifications",name: "sendNotifications",component: SendNotifications,},{path: "/notice/sendNotifications/addAssociatedItems",name: "addAssociatedItems",component: AddAssociatedItems,},{path: "/projectManagement/createProject",name: "createProject",component: CreateProject,},{path: "/projectManagement/projectSet",name: "projectSet",component: ProjectSet,},
];// 3. 创建路由实例并传递 `routes` 配置
const router = createRouter({history: createWebHistory(),routes, // `routes: routes` 的缩写
});export default router;

 实际的样子(左边为侧边菜单栏,右边为切换菜单对应的页面内容):

二、问题的解决

描述:当我点击左边侧边栏的时候,选中高亮能够选中,但是在浏览器上面点击左右回退,他的高亮就乱来了,比如我点击修改通知---->项目管理,然后点击上面浏览器的回退按钮,右边内容能够对应回退,但是高亮选中效果保留到了修改通知。默认是没有问题的,但是我再从修改通知---->项目管理---->修改通知,按道理来说,回退一步,应该回到项目管理,高亮也是,但是他高亮显示到了修改通知,产生了错乱。从修改通知---->项目管理---->创建项目按钮(进行创建项目页面),点击回退也还是回退到修改通知,应该是回到项目管理。如图:

采取解决方法:使用pinia管理当前选中状态(侧边栏选中高亮),通过窗口状态改变监听时间,配合页面路由的改变,来记录历史的路由,记录方式改为createWebHistory方式。具体如下:

安装pinia:

yarn add pinia
# 或者使用 npm
npm install pinia

在main.js引入和use一下:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import '../src/assets/static/css/normalize.css'
import App from './App.vue'
import router from '../routers/router'
// 引入element-plus和样式
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'const app = createApp(App)
app.use(router).use(ElementPlus).use(createPinia()).mount('#app')

新建一个stores的文件夹用于存放pinia相关的文件,并在下面创建一个main.js当作主仓库:

main.js代码:

默认状态是第一个菜单项,我这里是notice。

import { defineStore } from "pinia";// 你可以对 `defineStore()` 的返回值进行任意命名,但最好使用 store 的名字,同时以 `use` 开头且以 `Store` 结尾。(比如 `useUserStore`,`useCartStore`,`useProductStore`)
// 第一个参数是你的应用中 Store 的唯一 ID。
export const MainStore = defineStore("main", {state: () => {return {// 用于记录当前选中的路由(当前选中的菜单项)currentSelected: "notice",};},getters: {},actions: {},
});

在侧边栏页面使用这个状态:

import { useRouter, useRoute } from "vue-router";
import { MainStore } from "../src/stores/main";
import {Bell,Document,Shop,User,Avatar,DataLine,
} from "@element-plus/icons-vue";
const handleOpen = (key, keyPath) => {};
const handleClose = (key, keyPath) => {};
const router = useRouter();
const route = useRoute();
const mainStore = MainStore();

在app.vue里面写上窗口监听,监听路由的历史变化,来动态改变这个currentSelected当前选中的值,来让高亮效果对应显示:

<script setup>
import { MainStore } from './stores/main.js';
import { useRouter } from "vue-router";const router = useRouter();
const mainStore = MainStore();// 监听浏览器的历史记录变化
window.addEventListener('popstate', () => {if(router.currentRoute.value.name){mainStore.currentSelected = router.currentRoute.value.name}
})
</script><template><router-view></router-view>
</template><style scoped></style>

 我这里的home页面,包括菜单侧边栏和右边切换内容区域,完整代码:

<template><div class="container"><!-- 菜单侧边栏 --><div class="bar"><!-- 系统名称 --><div class="system-title">智慧物业管理平台</div><el-menuactive-text-color="#ffd04b"background-color="#545c64"class="el-menu-vertical-demo":default-active="mainStore.currentSelected"text-color="#fff"@open="handleOpen"@close="handleClose"style="height: calc(100vh - 52px)"router><el-menu-item index="notice"><el-icon><Bell /></el-icon><span>整改通知</span></el-menu-item><el-menu-item index="projectManagement"><el-icon><Document /></el-icon><span>项目管理</span></el-menu-item><el-menu-item index="supplierManagement"><el-icon><Shop /></el-icon><span>供应商管理</span></el-menu-item><el-sub-menu index="personnel" ><!-- index属性用于指定子菜单的唯一标识符 --><template #title><el-icon><User /></el-icon><span>人事管理</span></template><el-menu-item index="personnelList">人员列表</el-menu-item><!-- <el-menu-item index="departmentManagement">部门管理</el-menu-item><el-menu-item index="postManagement">岗位管理</el-menu-item> --></el-sub-menu><el-menu-item index="dataManagement"><el-icon><DataLine /></el-icon><span>数据管理</span></el-menu-item><el-menu-item index="roleManagement"><el-icon><Avatar /></el-icon><span>角色管理</span></el-menu-item></el-menu></div><!-- 内容区域 --><div class="content"><div class="main"><router-view></router-view></div></div></div>
</template><script setup>
import { useRouter, useRoute } from "vue-router";
import { MainStore } from "../src/stores/main";
import {Bell,Document,Shop,User,Avatar,DataLine,
} from "@element-plus/icons-vue";
const handleOpen = (key, keyPath) => {};
const handleClose = (key, keyPath) => {};
const router = useRouter();
const route = useRoute();
const mainStore = MainStore();</script><style scoped>
.container {display: flex;justify-content: space-between;
}
.bar {width: 200px;/* height: 100vh; */background-color: #545c64;
}
.system-title {height: 50px;line-height: 50px;color: #fff;text-align: center;border-bottom: 2px solid #ccc;background-color: #545c64;
}
.avatar {width: 40px;height: 40px;background-color: aqua;border-radius: 50%;
}
.admin-header {display: flex;align-items: center;line-height: 50px;height: 50px;margin-left: 20px;
}
.content {width: calc(100vw - 200px);
}
.main {padding: 20px;
}
</style>

这样就解决了该问题。如有错误,欢迎指正!

这篇关于element-plus+vue3项目(侧边栏菜单的使用和历史记录切换问题的解决(高点效果对应不上))的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/764873

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav