自定义Tabs标签页组件(vue)

不知道有没有和我一样项目中用的到Ant Design of Vue的Tabs组件在测试环境上打包时是无效的,于是手写个组件

我会写在src的componets下

我所用到的传值都是用类型1或2来判断或者中英文

父组件

<template>
  <div>
    <!-- 操作tab区域 -->
    <div class="tab-box">
      <div
        class="tab-item"
        v-for="(item, index) in tabName"
        :key="index"
        :class="{tabActive:tabCode == index}"
        @click="getTab(index)"
      >{{item}}</div>
    </div>
  </div>
</template>

<script>
export default {
  props: ['tabName', 'type'],
  data() {
    return {
      tabCode: 0,
      //   tabName: ['tab1', 'tab2'],
      send: 1
    }
  },
  mounted() {},
  methods: {
    getTab(index) {
      this.tabCode = index
      if (index == 0) {
        // this.type = 1
        if (typeof this.type == 'string') {
          this.send = 'CN'
        } else {
          this.send = 1
        }
        this.$emit('typeFn', this.send)
      } else {
        if (typeof this.type == 'string') {
          this.send = 'EN'
        } else {
          this.send = 2
        }
        this.$emit('typeFn', this.send)
      }
      // console.log('this.type: ', this.send)
      //   this.$emit('typeFn', this.type)
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-box {
  display: flex;
  margin-bottom: 18px;
  .tab-item {
    width: 100px;
    height: 35px;
    text-align: center;
    line-height: 35px;
    cursor: pointer;
    border: 1px solid #1890ff;
  }
  .tabActive {
    background-color: #1890ff;
    color: #fff;
  }
}
</style>

子组件

<template>
 <TabBar :tabName="tabName" :type="subject" @typeFn="typeFn" />
</template>

<script>
import TabBar from '@/components/TabBar'
export default {
  components: { TabBar },
   data() {
   subject: 'CN' //定义你需要的
   }
}
 methods: {
 typeFn(value) {
      this.subject = value; //写你的逻辑
    }
 }
 
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的自定义tabs组件的实现示例: ```vue <template> <div class="tabs"> <div class="tab-header"> <div v-for="(tab, index) in tabs" :key="index" :class="{ active: currentIndex === index }" @click="changeTab(index)" > {{ tab.title }} </div> </div> <div class="tab-content"> <slot /> </div> </div> </template> <script> export default { name: "Tabs", props: { defaultIndex: { type: Number, default: 0, }, }, data() { return { currentIndex: this.defaultIndex, tabs: [], }; }, methods: { changeTab(index) { this.currentIndex = index; }, registerTab(tab) { this.tabs.push(tab); }, }, mounted() { this.tabs = this.$children; }, }; </script> ``` 在这个组件中,我们使用了两个插槽,一个是tab-header用于显示标签标题,另一个是tab-content用于显示标签内容。组件内部维护了一个tabs数组,用于存储所有的标签组件标签组件的实现如下: ```vue <template> <div v-show="active"> <slot></slot> </div> </template> <script> export default { name: "Tab", props: { title: { type: String, required: true, }, }, data() { return { active: false, }; }, mounted() { this.$parent.registerTab(this); }, updated() { if (this.$parent.currentIndex === this.$parent.tabs.indexOf(this)) { this.active = true; } else { this.active = false; } }, }; </script> ``` 在标签组件中,我们维护了一个active属性,用于控制标签是否显示。同时,当标签组件的父组件Tabs更新时,我们会根据当前标签tabs数组中的位置和父组件当前激活的标签的位置来更新active属性。 使用方式如下: ```vue <template> <div> <tabs> <tab title="Tab 1"> <p>Content of Tab 1</p> </tab> <tab title="Tab 2"> <p>Content of Tab 2</p> </tab> <tab title="Tab 3"> <p>Content of Tab 3</p> </tab> </tabs> </div> </template> <script> import Tabs from "@/components/Tabs.vue"; import Tab from "@/components/Tab.vue"; export default { name: "App", components: { Tabs, Tab, }, }; </script> ``` 其中,Tabs组件包含了三个Tab组件,每个Tab组件都有一个标题和内容。可以根据需要自行修改样式和内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值