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

news2024/9/28 7:27:22

在这里插入图片描述

文章目录

  • 效果展示
  • 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.children
			routesListItem.name = parsedValue.name
			routesListItem.path = parsedValue.path
			routesListItem.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.children
				routesListItem.name = item.name
				routesListItem.path = item.path
				routesListItem.meta = item.meta
			}
		})
		console.log(routesListItem, 'routesList');
	}
	if (value3 !== null) {
		// 如果value不是null,则尝试解析它  
		try {
			const parsedValue = JSON.parse(value3);
			currentRoute.children = parsedValue.children
			currentRoute.name = parsedValue.name
			currentRoute.path = parsedValue.path
			currentRoute.meta = parsedValue.meta
			asideActive.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 = item
				currentRoute.name = routesListItem.name
				currentRoute.path = routesListItem.path
				currentRoute.meta = routesListItem.meta
				asideActive.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 = false
		console.log(123123);

	}
	routesListItem.children = item.children
	routesListItem.name = item.name
	routesListItem.path = item.path
	routesListItem.meta = item.meta
	asideActive.value = item.path

	// console.log(routesListItem.valueOf);
};
const handleRoutesItem = (item: any) => {
	router.push(item.path);
	currentRoute.name = routesListItem.name
	currentRoute.path = routesListItem.path
	currentRoute.meta = routesListItem.meta
	currentRoute.children = item
	localStorage.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>

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1551011.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

白酒:浓香型白酒的典型代表与特点

云仓酒庄的豪迈白酒作为白酒的品牌&#xff0c;具有一系列与众不同的特点和优势。下面云仓酒庄的豪迈白酒将从典型性、品质、口感和包装等方面深入分析白酒的特点&#xff0c;以及它如何体现浓香型白酒的魅力。 浓香型白酒是中国白酒的重要分支&#xff0c;以浓郁的香味和与众不…

Redis - 5k star! 一款简洁美观的 Redis 客户端工具~

项目简介 Tiny RDM 是一款现代化、轻量级的跨平台 Redis 桌面客户端&#xff0c;可在 Mac、Windows 和 Linux 系统上运行。初次打开 Tiny RDM&#xff0c;你会被它舒适的风格和配色所吸引&#xff0c;界面简约而不简单&#xff0c;功能齐全。 Tiny RDM 有着如下的功能特性 项…

进程与文件

目录 Linux的 > 和 >> 文件的本质 &#xff1a; 操作系统的系统调用函数 open&#xff1a; close&#xff1a;关闭文件 write&#xff1a; open的返回值&#xff1a; 操作系统视角中的“文件与进程之间的关系”&#xff1a; 从上图可以得知以下论点&#xff1a…

Linux:环境变量的特性及获取

目录 一、环境变量基本概念 1.1命令行参数 1.2常见环境变量 二、环境变量相关指令 创建本地变量 三、环境变量通常是具有全局属性的 一、环境变量基本概念 环境变量(environment variables)不是一个而是一堆&#xff0c;彼此之间其实没有关系。本质上是为了解决不同场景下…

Transformer的前世今生 day10(Transformer编码器

前情提要 ResNet&#xff08;残差网络&#xff09; 由于我们加更多层&#xff0c;更复杂的模型并不总会改进精度&#xff0c;可能会让模型与真实值越来越远&#xff0c;如下&#xff1a; 我们想要实现&#xff0c;加上一个层把并不会让模型变复杂&#xff0c;即没有它也没关系…

【JavaWeb】Day24.Web入门——SPringBootWeb入门

什么是SPring&#xff1f; 我们可以打开Spring的官网(Spring | Home)&#xff0c;去看一下Spring的简介&#xff1a;Spring makes Java simple。Spring的官方提供很多开源的项目&#xff0c;我们可以点击上面的projects&#xff0c;看到spring家族旗下的项目&#xff0c;按照流…

数据库是怎么做到事务回滚的呢?

数据库实现事务回滚的原理涉及到数据库管理系统&#xff08;DBMS&#xff09;如何维护事务的一致性和持久性。 基本原理&#xff1a; ACID属性&#xff1a;事务的原子性&#xff08;Atomicity&#xff09;、一致性&#xff08;Consistency&#xff09;、隔离性&#xff08;Iso…

Elasticsearch从入门到精通-07ES底层原理学习

Elasticsearch从入门到精通-07ES底层原理和高级功能 &#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是程序员行走的鱼 &#x1f4d6; 本篇主要介绍和大家一块学习一下ES底层原理包括集群原理、路由原理、分配控制、分配原理、文档分析原理、文档并发安全原理以及一些高…

【热门话题】ECMAScript vs JavaScript:理解两者间的联系与区别

&#x1f308;个人主页: 鑫宝Code &#x1f525;热门专栏: 闲话杂谈&#xff5c; 炫酷HTML | JavaScript基础 ​&#x1f4ab;个人格言: "如无必要&#xff0c;勿增实体" 文章目录 ECMAScript vs JavaScript&#xff1a;理解两者间的联系与区别1. ECMAScript&am…

创建一个vue3 + ts + vite 项目

vite 官网&#xff1a; https://cn.vitejs.dev/guide/ 兼容性注意 Vite 需要 Node.js 版本 18&#xff0c;20。然而&#xff0c;有些模板需要依赖更高的 Node 版本才能正常运行&#xff0c;当你的包管理器发出警告时&#xff0c;请注意升级你的 Node 版本。 安装项目 1. 使用n…

【Redis主从架构。主从工作原理psync、bgsave、部分数据复制、主从复制风暴解决方案】【Redis哨兵高可用架构。sentinel】

Redis主从架构 Redis主从工作原理数据部分复制 Redis哨兵高可用架构client连接哨兵规则主节点挂了&#xff0c;集群从新选择主节点&#xff0c;并且同步给sentinel 转自图灵课堂 redis主从架构搭建&#xff0c;配置从节点步骤&#xff1a; 1、复制一份redis.conf文件2、将相关…

基于java+springboot+vue实现的成都旅游网系统(文末源码+Lw+ppt)23-358

摘 要 人类现已迈入二十一世纪&#xff0c;科学技术日新月异&#xff0c;经济、资讯等各方面都有了非常大的进步&#xff0c;尤其是资讯与网络技术的飞速发展&#xff0c;对政治、经济、军事、文化等各方面都有了极大的影响。 利用电脑网络的这些便利&#xff0c;发展一套成…

Linux系统使用Docker部署MongoDB数据库并实现无公网IP远程访问

文章目录 前言1. 安装Docker2. 使用Docker拉取MongoDB镜像3. 创建并启动MongoDB容器4. 本地连接测试5. 公网远程访问本地MongoDB容器5.1 内网穿透工具安装5.2 创建远程连接公网地址5.3 使用固定TCP地址远程访问 前言 本文主要介绍如何在Linux Ubuntu系统使用Docker快速部署Mon…

Linux Php 连接 SAP Hana数据库客户端

下载地址 : SAP Development Tools https://tools.hana.ondemand.com/#hanatools 进入hanaclient-2.19.21-linux-x64 无需编译&#xff0c;运行 ./hdbinst 提示没有权限&#xff0c;执行chmod x * 有个子目录里面的也是没有权限&#xff0c;进入那个子目录 执行chmod …

通过测量扭矩和转角法评估紧固件的连接质量——SunTorque智能扭矩系统

智能扭矩系统-智能拧紧系统-扭矩自动控制系统-SunTorque 扭矩转角法是一种用于测量材料力学性能和评估紧固件连接质量的重要方法。其原理基于材料在受到扭矩作用时产生的弹性变形和塑性变形&#xff0c;通过测量施加在紧固件上的扭矩和对应的转角关系&#xff0c;来推断材料的…

浅模仿小米商城布局(有微调)

CSS文件 *{margin: 0;padding: 0;box-sizing: border-box; }div[class^"h"]{height: 40px; } div[class^"s"]{height: 100px; } .h1{width: 1528px;background-color: green; } .h11{background-color:rgb(8, 220, 8); } .h111{width: 683px;background-c…

PLC/FA 电机信号隔离控制模拟信号数据隔离采集变换分配器0-5V/0-10V/1-5V,0-10mA/0-20mA/4-20mA

主要特性: >>精度等级&#xff1a;0.1级、0.2级。产品出厂前已检验校正&#xff0c;用户可以直接使用 >>辅助电源&#xff1a;5V/12V/15V/24VDC&#xff08;范围10%&#xff09; >>国际标准一路信号输入:0-5V/0-10V/1-5V,0-10mA/0-20mA/4-20mA等 >>…

【详细讲解React 快速入门教程】

&#x1f525;博主&#xff1a;程序员不想YY啊&#x1f525; &#x1f4ab;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家&#x1f4ab; &#x1f917;点赞&#x1f388;收藏⭐再看&#x1f4ab;养成习惯 &#x1f308;希望本文对您有所裨益&#xff0c;如有…

稀碎从零算法笔记Day26-LeetCode:跳跃游戏

断更多天&#xff0c;懒狗ex 题型&#xff1a;数组、模拟、类似双指针&#xff1f; 链接&#xff1a;55. 跳跃游戏 - 力扣&#xff08;LeetCode&#xff09; 来源&#xff1a;LeetCode 题目描述 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组…

JAVA批量下载

环境&#xff1a; JDK:1.7.0_80 tomcat:7.0.47 工程截图&#xff1a; 1.布署到tomcat 2.在浏览器中访问 启用数据库支持 启用数据库后下载信息将会保存在数据库中。在关闭重启浏览器后仍然可以继续下载。 将“DataBase”配置为true则为启用数据库支持 使用mysql文件夹下的脚…