element ui 关于标签页导航

复制代码
 
标签页导航组件修改样式:
复制代码
.el-tabs__header .is-top div{
    height:30px !important;
    line-height:30px !important;
    color:#9ca7b5;
}
.el-tabs__header .is-top div :hover{
    color:#55aaff;
}
复制代码

父组件:
复制代码
 
<div class="history-router">

  <history-router></history-router>
</div>


<script>

 import historyRouter from '../../view/historyrouter.vue'

  components:{
    historyRouter
  },


</script>


<style>

 .history-router{

    padding:0px 20px;
  }

 </style>

复制代码

 

标签页导航组件:
复制代码
<template>
    <div class="el_breadcrumb" v-if="historyrouter.length">
        <el-tabs v-model="editableTabsValue" type="card" closable @edit="handleTabsEdit" @tab-click="to">
          <el-tab-pane
            :key="item.name"
            v-for="(item, index) in historyrouter"
            :label="item.title"
            :name="item.id"
          >
          </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                historyrouter:[{
                }],//历史记录
                editableTabsValue:''
            }
        },
        mounted(){
            this.gethistoryrouter()
        },
        methods:{
       //点击标签路由跳转 to(a,b){
this.historyrouter.find((v,i)=>{
            //判断路由id和回调的name是否一致,name只能为字符串
if(v.id==a.name){ this.$router.push({ path:v.path }) } }) },
        //删除标签 handleTabsEdit(targetName, action) {
this.historyrouter.find((v,i)=>{
            //targetName回调被改成传路由id,由于删除需要标识,所以name不使用路由名称
if(v.id==targetName){ this.historyrouter.splice(i,1) if(this.historyrouter[0].path){ localStorage.setItem('histryrouter',JSON.stringify(this.historyrouter)) }else{ localStorage.removeItem('histryrouter') } } }) },
       //获取历史记录 gethistoryrouter(){
var historyrouters=JSON.parse(localStorage.getItem('histryrouter')) if(historyrouters){ this.historyrouter=historyrouters } } },
     //组件停用被查询历史记录 deactivated(){
this.gethistoryrouter() } } </script> <style> </style>

复制代码

导航守卫:
复制代码
router.beforeEach((to,from,next)=>{
    if(to){
        // localStorage.clear()
        next()
        if(to){
            var historyrouter=JSON.parse(localStorage.getItem('histryrouter'))
            var timestamp=new Date().getTime()+''//获取时间戳,设置为路由唯一标识,但组件name值只能为字符串,所以加上引号
            if(historyrouter){
                var maxlengrh=historyrouter.length-1
          //防止连续记录重复路由 if(historyrouter[maxlengrh].path!=to.path){ historyrouter.push({path:to.path,title:to.meta.title,id:timestamp}) localStorage.setItem('histryrouter',JSON.stringify(historyrouter)) } } else{ localStorage.setItem('histryrouter',JSON.stringify([{path:to.path,title:to.meta.title,id:timestamp}])) } } } })
复制代码

 


复制代码

 

posted @   谎渊  阅读(641)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示