欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 017、Vue动态tag标签

017、Vue动态tag标签

2024/10/24 5:20:15 来源:https://blog.csdn.net/bestcxx/article/details/140622007  浏览:    关键词:017、Vue动态tag标签

文章目录

    • 1、先看效果
    • 2、代码

1、先看效果

在这里插入图片描述

2、代码

<template><div class = "tags"><el-tag size="medium"closable v-for="item,index in tags":key="item.path":effect="item.title==$route.name?'dark':'plain'"@click="goTo(item.path)"@close = "close(index)":disable-transitions="true"><i class="cir" v-show="item.title==$route.name"></i>{{item.title}}</el-tag></div>
</template><script>
import { Tag } from 'element-ui';export default{data(){return {tags:[{title:"layout",path:"/layout",isActive: true,}]}},methods:{goTo(path){this.$router.replace({path: (path == '/' || path == undefined ? '/Index' : path)});},close(index){const name = this.tags[index].title;this.tags.splice(index,1);if(this.tags.length==0) return;//如果关闭当前页,则激活最后一个标签页const path = this.tags[this.tags.length-1].path;if(name===this.$route.name&&this.tags.length!=0){this.$router.replace({path: (path == '/' || path == undefined ? '/Index' : path)});}}},watch:{$route:{immediate:true,handler(val,oldVal){console.log(val);const bool = this.tags.find(item=>{return item.path == val.path;});if(!bool){this.tags.push({title: val.name,path: val.path});}}}}}
</script><style scoped>.tags .el-tag{padding-left: 10px;padding-top: 0px;margin-right: 5px;.cir{width: 8px;height: 8px;margin-right: 4px;background-color: #fff;border-radius: 50%;display: inline-block;}}</style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com