Vue+Ant Design 实现 tags标签选中高亮,跳转路由,删除最后一个路由跳转至前一个,删除非最后一个路由跳转至后一个

注意点:
        1.要完成高亮效果在<a-tag>组件上 直接判断当前的$route上的路由与遍历时item的路由是否相等即可
        2.跳转路由时要判断是否为最后一个,所以要根据数组的长度以及点击时所在的下标来判断
        3.将整个<a-tag>分装成一个公共组件方便调用
<template>
  <div>
    <a-tag :color="$route.path === t.path? '#2db7f5':'#87d068'" v-for="(t,index) in tagsList" :key="t.path"
           :closable="t.label !== '首页'"
           @close="closeHandle(t,index)" @click="handleChange(t)">{{ t.label }}
    </a-tag>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: '',
  data() {
    return {};
  },
  created() {

  },
  mounted() {
  },
  components: {},
  computed: {
    ...mapState('t', ['tagsList']),
  },
  methods: {
    closeHandle(val, index) {
      const t_index = this.tagsList.findIndex((item) =>
          item.path === val.path
      )
      this.tagsList.splice(t_index, 1)

      const length = this.tagsList.length
      if (this.$route.path !== val.path) {
        return
      }
      if (index === length) {
        const path_one = this.tagsList[index - 1].path
        this.$router.push(path_one)
      } else {
        const path_two = this.tagsList[index].path
        this.$router.push(path_two)
      }
    },
    handleChange(val) {
      if (this.$route.path !== val.path && !(this.$route.path === '/home' && val.path === '/')) {
        this.$router.push(val.path)
      }

    },
  }
}
</script>

<style scoped lang="less">
.ant-tag {
  font-size: 16px;
  padding: 4px;
}
</style>

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,我们可以使用location.href来实现跳转一个外链。location.href是JavaScript的内置对象,代表当前窗口的URL地址。我们可以通过修改location.href的值来改变当前窗口的URL地址,从而实现跳转。 要实现通过location.href跳转一个路由外链,我们可以按照以下步骤进行操作: 1. 首先,在Vue组件中需要进行跳转的地方,例如一个按钮的点击事件中,我们可以通过使用location.href来进行跳转。例如,我们可以在按钮的点击事件处理函数中,使用如下代码来实现跳转一个外链的功能: ```javascript window.location.href = 'http://www.example.com'; ``` 在上面的代码中,我们将location.href的值设置为一个外链的URL地址,这样就可以跳转到这个外链。 2. 如果需要在跳转到外链的同时,还需要保存一些参数或状态,我们可以在跳转前将这些参数或状态存储到url中,例如可以使用URLSearchParams来实现。例如,我们可以根据需要构建一个完整的URL,并将其赋值给location.href,例如: ```javascript const params = new URLSearchParams(); params.set('param1', 'value1'); params.set('param2', 'value2'); const url = 'http://www.example.com?' + params.toString(); window.location.href = url; ``` 在上面的代码中,我们先创建了一个URLSearchParams对象params,然后使用set方法将参数和对应的值添加到params中,之后使用toString方法将params转换为字符串,并将其与外链的URL地址拼接起来,最后赋值给location.href,完成跳转。 通过以上的步骤,我们可以使用location.href来实现Vue跳转一个外链的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值