vue3+ts+element home页面侧边栏+头部组件+路由组件组合页面教程

本文主要是介绍vue3+ts+element home页面侧边栏+头部组件+路由组件组合页面教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

文章目录

  • 效果展示
  • template代码
  • script代码
  • 样式代码


效果展示

在这里插入图片描述

template代码

<template><el-container class="home"><el-aside class="flex" :style="{ width: asideDisplay ? '70px' : '290px' }"><div class="aside-left"><div class="aside-logo"><img src="./logo.png" class="aside-logo-img" /></div><div class="aside-list"><div class="aside-list-item" :class="{ active: item.path === asideActive }"v-for="(item, index) in routesList" :key="index" @click="handleRoutes(item)">{{ item.meta.title }}</div></div></div><div class="aside-right" :style="{ display: asideDisplay ? 'none' : 'block' }"><div class="aside-right-title">Admin.NET</div><div class="aside-right-list"><div class="aside-right-list-item" :class="{ active: item.path === currentRoute.children.path }"v-for="(item, index) in routesListItem.children" :key="index" @click="handleRoutesItem(item)">{{item.meta.title }}</div></div></div></el-aside><el-container class="flex1"><el-header class=""><div class="header-navbars-container"><el-icon v-if="asideDisplay" @click="asideDisplay = !asideDisplay"><Expand /></el-icon><el-icon v-if="!asideDisplay" @click="asideDisplay = !asideDisplay"><Fold /></el-icon><el-breadcrumb separator="/"><el-breadcrumb-item>{{ currentRoute.meta.title }}</el-breadcrumb-item><el-breadcrumb-item :to="{ path: currentRoute.children.path }">{{ currentRoute.children.meta.title }}</el-breadcrumb-item></el-breadcrumb></div><div class="header-navbars-tagsview"><span class="header-navbars-tagsview-span"><span class="header-navbars-tagsview-item" v-for="(item, index) in currentList" :key="index"@click="handleRoutes(item)" :class="{ 'active': item.path === currentRoute.children.path }">{{ item.meta.title }}<!-- {{currentList}} --><el-icon><Close /></el-icon></span></span></div></el-header><el-main><router-view></router-view></el-main></el-container></el-container>
</template>

script代码

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { Expand, Fold, Close } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';// 页面加载时
onMounted(() => {getAllRoutes();firstEnter();
});const router = useRouter();// 当前页面的路由对象
const routesList = reactive(new Array<any>());
const asideActive = ref('');
const asideDisplay = ref(true);
const routesListItem = reactive({meta: { title: '' },children: new Array<any>(),name: '',path: '',});
const currentRoute = reactive({meta: { title: '' },children: {meta: { title: '' },name: '',path: '',},name: '',path: '',
});const currentList = reactive(new Array<any>());const getAllRoutes = () => {const routes = router.getRoutes();console.log(routes); // 这里会输出所有的路由记录routes.forEach((route: any) => {if (route.meta.level == 1) {console.log(route);routesList.push(route)}})return routes;
};const firstEnter = () => {const value = localStorage.getItem('currentList');const value2 = localStorage.getItem('routesListItem');const value3 = localStorage.getItem('currentRoute');// 检查value是否为null  if (value !== null) {// 如果value不是null,则尝试解析它  try {const parsedValue = JSON.parse(value);parsedValue.forEach((item: any) => {const valFind = currentList.find((val: any) => {if (val.name == item.name) {return val}})if (!valFind) {currentList.push(item)}});} catch (error) {// 如果解析过程中发生错误,比如value不是一个有效的JSON字符串,则处理错误  console.error('Error parsing JSON:', error);currentList.push({name: router.currentRoute.value.name,path: router.currentRoute.value.path,meta: {title: router.currentRoute.value.meta.title}})}} else {// 如果value是null,打印null或者做其他处理  console.log(null, 'currentList is null or not set');currentList.push({name: router.currentRoute.value.name,path: router.currentRoute.value.path,meta: {title: router.currentRoute.value.meta.title}})}// 检查value是否为null  if (value2 !== null) {// 如果value不是null,则尝试解析它  try {const parsedValue = JSON.parse(value2);routesListItem.children = parsedValue.childrenroutesListItem.name = parsedValue.nameroutesListItem.path = parsedValue.pathroutesListItem.meta = parsedValue.meta} catch (error) {// 如果解析过程中发生错误,比如value不是一个有效的JSON字符串,则处理错误  console.error('Error parsing JSON:', error);}} else {// 如果value是null,打印null或者做其他处理  const parsedValue = router.currentRoute.value.matched[1]routesList.forEach(item => {if (parsedValue.path.indexOf(item.name) !== -1) {routesListItem.children = item.childrenroutesListItem.name = item.nameroutesListItem.path = item.pathroutesListItem.meta = item.meta}})console.log(routesListItem, 'routesList');}if (value3 !== null) {// 如果value不是null,则尝试解析它  try {const parsedValue = JSON.parse(value3);currentRoute.children = parsedValue.childrencurrentRoute.name = parsedValue.namecurrentRoute.path = parsedValue.pathcurrentRoute.meta = parsedValue.metaasideActive.value = parsedValue.path} catch (error) {// 如果解析过程中发生错误,比如value不是一个有效的JSON字符串,则处理错误  console.error('Error parsing JSON:', error);}} else {// 如果value是null,打印null或者做其他处理  const parsedValue = router.currentRoute.value.matched[2]routesListItem.children.forEach(item => {if (parsedValue.path.indexOf(item.path) != -1) {console.log();console.log(item, 'item');currentRoute.children = itemcurrentRoute.name = routesListItem.namecurrentRoute.path = routesListItem.pathcurrentRoute.meta = routesListItem.metaasideActive.value = routesListItem.path}})console.log(asideActive, 'currentRoute is null or not set');}
};
const handleRoutes = (item: any) => {if (item.name == routesListItem.name) {asideDisplay.value = !asideDisplay.value} else {asideDisplay.value = falseconsole.log(123123);}routesListItem.children = item.childrenroutesListItem.name = item.nameroutesListItem.path = item.pathroutesListItem.meta = item.metaasideActive.value = item.path// console.log(routesListItem.valueOf);
};
const handleRoutesItem = (item: any) => {router.push(item.path);currentRoute.name = routesListItem.namecurrentRoute.path = routesListItem.pathcurrentRoute.meta = routesListItem.metacurrentRoute.children = itemlocalStorage.setItem('currentRoute', JSON.stringify(currentRoute));localStorage.setItem('routesListItem', JSON.stringify(routesListItem));const valFind = currentList.find((val: any) => {if (val.name == item.name) {return val}})if (!valFind) {currentList.push(item)localStorage.setItem('currentList', JSON.stringify(currentList));}};</script>

样式代码

<style lang="scss">
.home {width: 100vw;height: 100vh;
}.el-container {width: 100%;height: 100%;
}.el-aside {min-width: 70px;max-width: 290px;
}.aside-left {width: 70px;height: 100%;background: #2c3a49;.aside-logo {height: 50px;display: flex;align-items: center;justify-content: center;img {width: 80%;height: 80%;}}.aside-list-item {width: calc(100% - 10px);height: 40px;text-align: center;color: #f0f0f0;font-size: 12px;background: #de291080;cursor: pointer;margin: 5px;border-radius: 5px;line-height: 40px;}.active {background: #de2910;}
}.aside-right {width: 220px;transition: width 0.3s ease;border-right: 1px solid #e4e7ed;.aside-right-title {width: 220px;height: 50px;display: flex;align-items: center;justify-content: center;box-shadow: rgba(0, 21, 41, 0.02) 0px 1px 4px;white-space: nowrap;font-weight: 800;font-size: 18px;color: #11559c;}.aside-right-list {.aside-right-list-item {width: 100%;height: 50px;display: flex;align-items: center;justify-content: center;cursor: pointer;}.active {width: calc(100% - 5px);border-right: 5px solid #de2910;color: #de2910;background-color: #de281027;}}
}.el-header {padding: 0px;height: 100px;
}.header-navbars-container {background-color: #fff;border-bottom: 1px solid #f1f2f3;position: relative;z-index: 1999;display: flex;height: 60px;.el-icon {width: 60px;height: 60px;font-size: 12px;svg {height: 2em;width: 2em;}}.el-breadcrumb {// display: flex;font-size: 14px;line-height: 60px;.el-breadcrumb__item {align-items: center;display: math;float: none;}}
}.header-navbars-tagsview {min-height: 40px;padding-right: 20px;background-color: #fff;border-bottom: 1px solid #f1f2f3;overflow: auto;.header-navbars-tagsview-span {white-space: nowrap;}.header-navbars-tagsview-item {// display: inline-block;line-height: 40px;margin: 0px 0px 0px 10px;font-size: 12px;background-color: #de291080;padding: 5px 10px;color: #fff;border-radius: 5px;cursor: pointer;white-space: nowrap;}.header-navbars-tagsview-item:hover {background-color: #de2810d2;}.el-icon {position: relative;top: 2px;right: -2px;}.active {background-color: #de2910;}
}.el-main {min-width: 1000px;
}
</style>

您好,我是肥晨。
欢迎关注我获取前端学习资源,日常分享技术变革,生存法则;行业内幕,洞察先机。

这篇关于vue3+ts+element home页面侧边栏+头部组件+路由组件组合页面教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

SpringBoot连接Redis集群教程

《SpringBoot连接Redis集群教程》:本文主要介绍SpringBoot连接Redis集群教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 依赖2. 修改配置文件3. 创建RedisClusterConfig4. 测试总结1. 依赖 <de

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.