【tab切换(单选)】【tab切换多选(可取消选中)】

这篇文章展示了如何使用Vue.js创建一个带有动态标签页的组件,利用v-for指令遍历数据,结合事件委托和防抖函数处理点击事件,实现标签页的切换。同时,根据标签页ID更新当前活跃标签并相应地改变路由,支持多选功能,允许用户选择和取消选择标签页。

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

单选

<div class="tabs" @click="goTurn">
  <div
     class="tab"
     v-for="(tab, index) in tabs"
     :key="index"
     :class="{ active: activeTab === tab.id }"
     :data-id="tab.id"
   >
     <div class="tab-bg">
       <p>{{ tab.name }}</p>
     </div>
   </div>
 </div>
import { debounce } from "@/assets/js/debounce.js";
//当前路由名称对应 tabId
const activeTab = ref(router.currentRoute.value.meta?.activeID || 0);
// 路由名称
const tabs = ref([
  {
    id: 1,
    name: "政治",
  },
  {
    id: 2,
    name: "经济",
  },
  {
    id: 3,
    name: "文化",
  },
  {
    id: 4,
    name: "社会",
  },
  {
    id: 5,
    name: "生态",
  },
]);
//事件委托跳转路由

const goTurn1 = debounce(() => {
  router.replace("/");
  activeTab.value = 0;
}, 100);
const goTurn = debounce((event) => {
  event.preventDefault();
  if (event.target instanceof HTMLElement) {
    const element = event.target;
    if (element.closest(".tab") instanceof HTMLElement) {
      const tab = element.closest(".tab");
      const tabId = tab?.dataset.id;
      if (tabId) {
        activeTab.value = Number(tabId);
        switch (tabId) {
          case "0":
            router.replace("/home");
            break;
          case "1":
            router.replace("/home/Politics");
            break;
          case "2":
            router.replace("/home/Economics");
            break;
          case "3":
            router.replace("/home/Culture");
            break;
          case "4":
            router.replace("/home/Social");
            break;
          case "5":
            router.replace("/home/Ecology");
            break;
          case "6":
            router.replace("/home/");
            break;
        }
      }
    }
  }
}, 100);

多选

<div class="tabs" @click="goTurn">
	<!-- activeTab === tab.id -->
	<div
		class="tab"
		v-for="(tab, index) in tabs"
		:key="index"
		:class="{ active: activeTab.filter((el: any) => el === Number(tab.id)).length > 0 }"
		:data-id="tab.id"
	>
		<div class="tab-bg">
			<p>{{ tab.name }}</p>
		</div>
	</div>
</div>

import { debounce } from "lodash-es";
const activeTab: any = ref([]); // 1
// tab名称
const tabs = ref([
	{
		id: 1,
		name: "专业护理"
	},
	{
		id: 2,
		name: "医疗服务"
	},
	{
		id: 3,
		name: "生活照料"
	},
	{
		id: 4,
		name: "康复训练"
	}
]);
//事件委托跳转路由

const goTurn = debounce(event => {
	event.preventDefault();
	if (event.target instanceof HTMLElement) {
		const element = event.target;
		if (element.closest(".tab") instanceof HTMLElement) {
			const tab = element.closest(".tab");
			const tabId = tab?.dataset.id;
			if (tabId) {
				if (activeTab.value.filter((el: any) => el === Number(tabId)).length === 0) {
					activeTab.value.push(Number(tabId));
				} else {
					activeTab.value = activeTab.value.filter((el: any) => el != tabId);
				}

				// activeTab.value = Number(tabId);
				// activeTab.value = Number(tabId);
			}
			console.log(activeTab.value);
		}
	}
}, 100);

效果图 (多选、取消选中)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值