vue3+ts实现简单的选项卡

 第一种(不推荐使用)

<template>
  <div class="Tab">
    <button v-for="(item, index) in arr" :key="index" :class="index === Index ? 'ac' : ''" @click="FnClick(index)">按钮{{ item }}</button>

    <div v-for="(item, index) in arr" :key="index" :style="{display:Index==index?'block':'none'}">内容{{ item }}</div>
  </div>
</template>

<script setup lang="ts">
// 引入vue
import { ref } from 'vue'
const arr = [1, 2, 3]
// 定义下标
const Index = ref<number>(0)
const FnClick = (index: number) => {
  Index.value = index
}
</script>

<style scoped lang="less">
.Tab {
  button.ac {
    background-color: red;
  }
  div {
    width: 320px;
    height: 280px;
    border: 1px solid #000;
    display: none;
  }
}
</style>

  第二种

<template>
  <div class="increment">
    <div class="tab">
      <button v-for="(item,index) in buuton" :key="item.id" :class="Index==index?'ac':'' " @click="Fnclick(index)">{{ item.value }}</button>
      <div v-for="(item,index) in con" :key="item.id" :style="{display:Index==index?'block':'none'}">{{ item.value }}</div>
    </div>
  </div>
</template>
<style scoped lang="less">
  .increment{
    .tab button.ac{
      background-color: red;
    }
    .tab div{
      width: 280px;
      height: 180px;
      border: 1px solid #000;
      display: none;
    }
  }
</style>

<script setup lang="ts">
import { ref } from 'vue'
interface iBuuton {
  id: string
  value: string
}
interface iCon {
  id: string
  value: string
}

const Index=ref<number>(0)

const Fnclick = (index: number) => {
  Index.value = index
}

// 选项卡
const buuton = ref<iBuuton[]>([
  {
    id: 'b1',
    value: '按钮一'
  },
  {
    id: 'b2',
    value: '按钮二'
  },
  {
    id: 'b3',
    value: '按钮三'
  }
])
const con = ref<iCon[]>([
  {
    id: 'c1',
    value: '内容一'
  },
  {
    id: 'c2',
    value: '内容二'
  },
  {
    id: 'c3',
    value: '内容三'
  }
])
</script>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值