Vue----ele-admin7

导般菜单标签tagbar
<template>
<div class='tagbar'>
  <ul class="tag-list">
    <li v-for="(item, index) in tagsList" :key="index" :class="{'active': isActive(item.path)}" >
      <router-link :to='item.path' class="tag-title">{{item.title}}</router-link>
      <span class="tag-close" @click="onCloseCurrentTag(index)" v-show="tagsList.length != 1 || $route.fullPath != '/dashboard'"><i class="icon--close"></i></span>
    </li>
  </ul>

  <div class="tag-option">
    <el-dropdown @command='handleTag'>
      <div class="el-dropdown-link dropdown-icon"><i class="icon--close"></i></div>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item command='closeOther'>关闭其他</el-dropdown-item>
        <el-dropdown-item command='closeAll'>关闭所有</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</div>
</template>

<script>
import evue from '../../utils/evue'

export default {
  components: {},
  data () {
    return {
      tagsList: []
    }
  },
  computed: {
    showTags () {
      return this.tagsList.length > 0
    }
  },
  watch: {
    $route (newValue, oldValue) {
      this.setTags(newValue)
    }
  },
  methods: {
    isActive (path) {
      return path === this.$route.fullPath
    },

    setTags (route) {
      const isExist = this.tagsList.some(item => {
        return item.path === route.fullPath
      })

      if (!isExist) {
        if (this.tagsList.length >= 8) {
          this.tagsList.shift()
        }
        this.tagsList.push({
          title: route.meta.title,
          path: route.fullPath,
          name: route.matched[1].components.default.name
        })
      }

      evue.$emit('tags', this.tagsList)
    },

    onCloseCurrentTag (index) {
      // 默认首页
      if (this.tagsList.length === 1 && this.$route.fullPath === '/dashboard') {
        this.$router.push('/')
        this.setTags(this.$route)
        return false
      }

      const curItem = this.tagsList.splice(index, 1)[0]
      const item = this.tagsList[index] ? this.tagsList[index] : this.tagsList[index - 1]
      if (item) {
        curItem.path === this.$route.fullPath && this.$router.push(item.path)
      } else {
        this.$router.push('/')
      }
    },

    closeOtherTag () {
      const curItem = this.tagsList.filter((item) => {
        return item.path === this.$route.fullPath
      })

      this.tagsList = curItem
    },

    // 关闭全部标签页面
    closeAllTag () {
      this.tagsList = []
      this.$router.push('/')
      this.setTags(this.$route)
    },

    // 处理下拉框点击处理标签
    handleTag (command) {
      command === 'closeOther' ? this.closeOtherTag() : this.closeAllTag()
    }
  },
  created () {
    this.setTags(this.$route)

    // 监听关闭当前页面的标签页
    // evue.$on('close_current_tags', () => {

    // })
  }
}
</script>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值