vue-music 关于基础组件 (Tab组件)

本文介绍了一个基于 Vue 的 Tab 组件实现方式,通过定义 tab 切换数量的数组和当前高亮索引的 props 来控制 tab 的显示状态。文章详细展示了如何使用 v-for 循环渲染 tab 项,并通过点击事件实现 tab 的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义在项目的基础组类别的 tab组件中,定义一个tab切换数量的数组 和一个currentIndex 当前高亮索引 的props,当前高亮(active)的类等于currentIndex === index 当前循环的索引值,增加点击派发事件传入index 索引参数,

调用组件的页面接受派发事件点击的index 索引,底下的内容根据this.currentIndex 的值v-if 显示隐藏

tab组件

<template>
  <ul class="switches">
    <li class="switch-item" v-for="(item,index) in switches" :class="{'active':currentIndex === index}"
        @click="switchItem(index)">
      <span>{{item.name}} </span>
    </li>
  </ul>
</template>

<script type="text/ecmascript-6">
  export default {
    props: {
      switches: {
        type: Array,
        default: []
      },
      currentIndex: {
        type: Number,
        default: 0
      }
    },
    methods: {
      switchItem(index) {
        this.$emit('switch', index)
      }
    }
  }
</script>

调用

<switches :switches="switches" :currentIndex="currentIndex" @switch="switchItem"></switches>
data() {
  return {
    currentIndex: 0,
    switches: [
      {
        name: '最近播放'
      },
      {
        name: '搜索历史'
      }
    ]
  }
},

switchItem(index) {
  this.currentIndex = index
},

 

转载于:https://www.cnblogs.com/catbrother/p/9180503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值