vue+elemen如何t实现页面顶部tag
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章给大家分享的是有关vue+elemen如何t实现页面顶部tag的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。这种tag如何写?思路总结下:1. 页面渲染1页面显示由
千家信息网最后更新 2025年11月07日vue+elemen如何t实现页面顶部tag
这篇文章给大家分享的是有关vue+elemen如何t实现页面顶部tag的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

这种tag如何写?思路总结下:
1. 页面渲染
1页面显示由数组循环得出,数组可存储在store里
(1)存储前判断是否有重复的数据,重复的话,先删除再添加。
(2)没有重复直接push
addTag: (state, tag) => { const { fullPath, path, meta, query } = tag if (tag.path === '/login') { return false } const findIndex = state.tags.findIndex(item => item.path === tag.path) console.log(findIndex) if (findIndex >= 0) { state.tags.splice(findIndex, 1, { fullPath, path, meta, query }) } else { state.tags.push({ fullPath, path, meta, query }) } },2何时触发这个添加路由方法,监听路由进入的时候,调此方法将当前this实例上的route对象携带过去。
computed: {currentRoute() { return this.$route },}, watch: { $route: { handler(val) { if (val.name) { this.addTags() } }, // 深度观察监听 deep: true } }, methods:{ addTags() { //this.$store.dispatch 先提交给action,由他异步处理处罚mutation里面的方法,改变state里面的tags值 this.$store.dispatch('user/addTag', this.currentRoute) },}此时,tags数组里面已经有值,由于默认是白色,所以页面上看不出,接下来就是给选中的标签高亮。
1element 有个参数可以设定,可以查文档。
2选中的tag值是否等于当前路由进入的页面一致,一致则为true。
{{ tag.meta.title }} methods:{ setTagColor(tag) { return this.currentRoute.path === tag.path ? 'dark' : 'plain' }, }此时,tag的渲染和选中就完成了。
2. 来回切换tag
methods:{ toTagRoute(tag) { this.$router.push({ path: tag.fullPath || tag.path }) },}3. 删除一个tag标签
1由于是数组,你无法确定用户删除哪一个,所以需要遍历找出用户当前选中的tag。然后删除,同时更新store里的值。
2删除当前tag,高亮的标签是哪一个?这里是删除标签的前一个标签,也就是数组最后一个元素。
methods:{ closeTags(tag) { console.log(tag, 4444) this.$store.dispatch('user/delTag', tag) this.toLastTagRouter(this.$store.state.user.tags)//高亮删除标签的前一个tag }, toLastTagRouter(tags) { //注意此处传入tags是已删除后的,所以不能使用splice==》改变原数组;slice==》不改变原数组拿去数组最后一个元素 const latestView = tags.slice(-1)[0]//tags数组最后一个元素 console.log(latestView) if (latestView !== undefined && latestView.path !== undefined) { const { fullPath, meta, path, query } = latestView this.$router.push({ fullPath, meta, path, query }) } },}//action delTag({ commit }, tag) { commit('delTag', tag) },//mutationdelTag: (state, tag) => { //entries()对象变成一个可遍历的数组【0,{name:a,age:'20'}】 //这里使用forEach和map也可以 for (const [i, v] of state.tags.entries()) { if (v.path === tag.path) { state.tags.splice(i, 1) break } } },删除全部标签
methods:{ closeAllTags() { // 关闭所有 tag,仅剩余一个 this.$store.dispatch('user/delAllTags') const { fullPath, meta, path, query } = this.$store.state.user.tags[0] // 跳转剩余 tag 路由 this.$router.push({ fullPath, meta, path, query }) },}//actiondelAllTags({ commit }) { commit('delAllTags') },//mutation delAllTags: (state) => { state.tags.splice(1, state.tags.length) },感谢各位的阅读!关于"vue+elemen如何t实现页面顶部tag"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
数组
标签
页面
路由
元素
高亮
顶部
一致
内容
对象
方法
更多
用户
篇文章
剩余
存储
监听
不错
实用
接下来
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
工业互联网汉威科技
公安信息化网络安全会议讲话
数据库架构更新失败
华为网络安全部
串口单应用层 网络安全
wow服务器贫瘠之地
php 服务数据库 地址
西安市网络安全保卫
重新数据库指令
后端语言和数据库的关系
吉林质量网络技术咨询诚信合作
建立cad数据库
网络安全宣传周图片作品简介
数据库报ORA01033
软件开发过程的质量控制
e3v5无显卡服务器
深圳优拍档网络技术有限公司
无锡智能软件开发大全
三线城市软件开发发展方向
浙江台州五金模具erp软件开发
网络安全科普活动方案
为什么要维护服务器安全
哈密软件开发招聘
武汉电脑软件开发教程
高性能人工智能服务器
常州手机软件开发
亚圣科技互联网规划师
虹口区会计软件开发销售价格
山东调度服务器批发云服务器
重庆管理软件开发服务