首页 > 其他分享 >vue实现页面顶部路由标签跳转

vue实现页面顶部路由标签跳转

时间:2024-09-02 17:20:57浏览次数:7  
标签:vue const name route state 跳转 activeRoutes 路由

在页面使用

<!-- 活跃菜单 -->
									<div class="panel-group" ref="scrollPane" >
										<router-link ref="tag" :to="menu.path" v-for="(menu, idx) in activeRoutes"
											style="display: inline-block;">
											<div class="pannelItem" :class="menu.isAct == 1 ? 'actPannel' : ''">
												<div v-if="menu.isAct == 1" class="circule"></div>
												<div class="card-panel-description" style="text-wrap: nowrap;">
													{{ menu.name }}
												</div>
												<i @click.prevent.stop="delRoute(menu.name, idx)" class="el-icon-close"
													style="margin-left: 4px;"></i>
											</div>

										</router-link>
									</div>
computed: {
		...mapState({
			activeRoutes: state => state.permission.activeRoutes
		}),
	},

// 监听路由变化

watch: {
		$route(route) {
			const newRoute = {
				...route, // 复制原有的路由属性  
				name: route.name === 'dashboard' ? '首页' : (route.name == 'changePassword' ? '修改密码' : route.name), // 修改名称为 '首页' 如果是 'dashboard'  
				isAct: 1, // 设置活跃状态  
			};

			this.$store.dispatch('permission/addRoute', newRoute);
			this.moveToCurrentTag()

		},
	},

mounted() {

		// 查看当前页面路由 刷新后展示当前路由
		const currentRoute = this.$route || false

		if (currentRoute) {
			const newRoute = {
				...currentRoute, // 复制原有的路由属性  
				name: currentRoute.name === 'dashboard' ? '首页' : (currentRoute.name == 'changePassword' ? '修改密码' : currentRoute.name), // 修改名称为 '首页' 如果是 'dashboard'  
				isAct: 1, // 设置活跃状态  
			};
			this.$store.dispatch('permission/addRoute', newRoute);
		}

	},
methods: {

		// 关闭路由
		delRoute(name, idx) {
			if (idx == 0) {
				return
			}

            // 调用vuex模块中actions要用 ('模块名/方法')
			this.$store.dispatch('permission/removeRoute', name);
			const latestView = this.activeRoutes.slice(-1)[0];
			
            // 删除最后一个标签后前一个设为活跃标签
			if (latestView) {
				this.$router.push(latestView.path
				)
			} else {
				this.$router.push('/dashboard')
			}
		},

		// 超出后展示滚动条最后一项
		moveToCurrentTag() {
			this.$nextTick(() => {
				const scrollPane = this.$refs.scrollPane;
				if (scrollPane) {
					scrollPane.scrollLeft = scrollPane.scrollWidth; // 滚动到最后一项  
				}
			});

		},
}

样式

.panel-group {
	background-color: #fff;
	padding: 10px;
	margin-bottom: 10px;
	display: flex;
	max-width: 100%;
	/* 设置最大宽度 */
	overflow-x: auto;
	/* 允许水平滚动 */

	.pannelItem {
		background-color: #e6d8cc;
		color: #333;
		font-size: 14px;
		padding: 8px 10px;
		border-radius: 5px;
		margin-right: 10px;
		cursor: pointer;
		display: flex;
		align-items: center;
		flex-wrap: nowrap;
	}

	.actPannel {
		background-color: #aa8878;
		color: #fff;

	}

		.card-panel-description {
			float: right;
			font-size: 15px;
			font-weight: bold;
			margin: 26px;
			margin-left: 0px;

		
		}
	}
}

.circule {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background-color: #fff;
	margin-right: 5px;
}

store中新建modules模块 新建存储活跃路由文件permission.js



const state = {
	activeRoutes: [{
		name: '首页',
		path: '/dashboard',
		isAct: 1
	}]
}

const mutations = {
	SET_ROUTES: (state, routes) => {
		state.addRoutes = routes
		state.routes = constantRoutes.concat(routes)

	},

	ADD_ROUTE(state, route) {
		const existingRoute = state.activeRoutes.find(r => r.name === route.name);
		if (!existingRoute) {
			// 如果路由不存在,检查是否为 'dashboard'  
			if (route.name === 'dashboard') {
				route.name = '首页'; // 修改路由名称为 '首页'  
			}
			route.isAct = 1; // 设置活跃状态  
			state.activeRoutes.push(route); // 添加路由  
		} else {
			// 如果路由已存在,更新活跃状态  
			existingRoute.isAct = 1; // 更新活跃状态  
		}
		// 将其他不活跃的路由状态设置为 2  
		state.activeRoutes.forEach(r => {
			if (r.name !== route.name) {
				r.isAct = 2; // 设置不活跃状态为 2  
			}
		});

	},
	REMOVE_ROUTE(state, routeName) {
		// 移除指定的路由  
		state.activeRoutes = state.activeRoutes.filter(route => route.name !== routeName);
	},
}

const actions = {
	addRoute({ commit }, route) {
		commit('ADD_ROUTE', route);
	},
	removeRoute({ commit }, routeName) {
		commit('REMOVE_ROUTE', routeName);
	},

	
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}

在store下getter.js中引入activeRoute

const getters = {
	activeRouter: state => state.permission.activeRouter,
}
export default getters

在store index.js中导出

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'

Vue.use(Vuex)

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)



// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

const store = new Vuex.Store({
  modules,
  getters
})

export default store

标签:vue,const,name,route,state,跳转,activeRoutes,路由
From: https://blog.csdn.net/m0_64883027/article/details/141824045

相关文章

  • 基于SpringBoot+MySQL+SSM+Vue.js的学生选课系统
    获取见最下方名片获取见最下方名片获取见最下方名片演示视频基于SpringBoot+MySQL+SSM+Vue.js的学生选课系统(附论文)技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+SpringBoot文字描......
  • 【vue、react】前端如何为package.json添加注释
    文章目录前言安装使用方法问题前言写了个项目,想给package.json加注释结果发现加不上去,就在网上查找了相关文章,特意总结记录一下,防止下次使用。参考文章:如何为package.json添加注释众所周知,JSON文件是不支持注释的,除了JSON5/JSONC之外,我们在开发项目特别是前端项目......
  • PrimeVue DataTable 属性值解析
    primeVueDataTable组件的属性值使用DataTable属性NameTypeDefaultdescriptionvaluenull|any[]null要显示的对象数组。dataKeystring|Functionnull唯一标识数据中的记录的字段名称。rowsnumber0每页显示的行数。firstnumber0要显示的第一行的索引。totalR......
  • 基于nodejs+vue宠物网站[程序+论文+开题]-计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着社会经济的快速发展与人们生活水平的提高,宠物已成为许多家庭不可或缺的一员,它们不仅带来了陪伴与欢乐,还促进了宠物相关产业的蓬勃发展。然而,在享受宠物......
  • 基于nodejs+vue宠物网络社区论坛系统[程序+论文+开题]-计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码毕设程序文件开题报告内容研究背景在当今社会,随着人们生活水平的提高和情感需求的日益增长,宠物已成为许多家庭不可或缺的重要成员。宠物市场的繁荣不仅体现在宠物数量的激增,还体现在宠物主人对宠......
  • 腾讯云服务器上线一个springboot+vue项目最全教程 非Docker 自带上传到gitee中的项目
    前言博主看网络上的各种介绍项目上线的视频/文章,要么不是真实项目上线,要么没有良好的引导,要么不全。因此就写了此博客。此博客应该是最全的了,全面地以一个项目来介绍如何上线。即使你是小白看完博客也能让你成为糕手糕手糕糕手......
  • Nginx 部署前端 Vue 项目实战指南
    一、环境准备1.安装Nginx首先,需要在服务器上安装Nginx。Nginx是一款轻量级、高性能的HTTP和反向代理服务器。安装方式因操作系统而异。Linux系统(以Ubuntu为例):sudoapt-getupdatesudoapt-getinstallnginxWindows系统:从Nginx官方网站下载安装包,按......
  • 基于SpringBoot+Vue+uniapp的网络游戏交易系统(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 基于SpringBoot+Vue+uniapp的购物商场(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 2024最新——基于SpringBoot+Vue+uniapp的QQ村旅游网站(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......