自定义tabs切换组件

//子组件
// 注册全局组件
import Tabs from '@/components/Tabs'
//全局组件挂载
Vue.component('Tabs', Tabs)
// 
<template>
  <div class="tabs">
    <div class="tabsName">
      <span>选择场地:</span>
      <button
        v-for="(tab, key) in tabs"
        :key="key"
        :class="{ active: key == index }"
        type="button"
        @click="index = key"
      >
        {{ tab }}
      </button>
    </div>
    <ul>
      <div>选择时间段:</div>
      <template v-for="(item, key) in contents">
        <li
          v-if="item.orderFieldList[0].isOrder == 1"
          :key="key"
          :class="box.includes(item) ? 'liActive' : 'trueOrder'"
          @click="change(item)"
        >
          <div class="timebox">
            <span :class="box.includes(item) ? 'isPrice' : 'price'">
              {{ item.hourStart }}~{{ item.hourEnd }}
            </span>
            <span :class="box.includes(item) ? 'isPrice fw' : 'price fw'">
              ¥{{ item.orderFieldList[0].fieldPrice }}
            </span>
          </div>
        </li>
      </template>
    </ul>
  </div>
</template>

<script>
export default {
  props: ['tabs', 'contents', 'fatherlist'],
  data() {
    return {
      index: 0,
      box: []
    }
  },
  methods: {
    change: function (e) {
      console.log('1111111111')
      console.log(e)
      if (this.box.includes(e)) {
        this.box.splice(this.box.indexOf(e), 1)
      } else {
        // 把点击的元素item放入box数组中
        this.box.push(e)
        this.$emit('childByValue', this.box)
        console.log(this.box)
        console.log('-------------')
        const list = this.fatherlist
        console.log(list)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tabs {
  margin-top: 25px;
  background: #f8f8f8;
}
.tabsName {
  border-bottom: 1px solid #ebecef;
  margin-bottom: 20px;
  padding: 20px 10px 0px 10px;
}
.tabs button {
  width: 80px;
  border: none;
  padding: 6px 10px;
  cursor: pointer;
  background: #ffffff;
  box-sizing: border-box;
  border-radius: 4px;
  margin: 5px 7px 20px 7px;
  color: #37425b;
  font-size: 14px;
  height: 30px;
  line-height: 15px;
}
.tabs button:hover {
  border: 1px solid #5e6bdf;
  color: #5e6bdf;
  background: #ffffff;
}
.tabs button.active {
  border: 1px solid #5e6bdf;
  color: #5e6bdf;
  background: #ffffff;
}
.tabs ul {
  padding: 10px 10px 20px 10px;
}
.tabs li {
  display: inline-block;
  width: 137px;
  height: 36px;
  list-style-type: none;
  margin: 6px;
  // border: 1px solid #ccc;
  padding: 7px 5px 7px 5px;
  line-height: 23px;
  text-align: center;
}
.price {
  font-weight: 400;
  font-size: 14px;
  color: #333;
  margin-right: 4px;
  cursor: pointer;
}
.fw {
  font-weight: 600 !important;
}
.isPrice {
  font-weight: 400;
  font-size: 14px;
  color: #fff;
  margin-right: 4px;
  cursor: pointer;
}
.trueOrder {
  background: #fff;
}
.isOrder {
  background: #ebecef;
}
.liActive {
  background: #5e6bdf;
  color: #fff;
}
</style>

父引入子组件

<el-form-item
          v-if="form.reserveType == 1 && form.detailAddDtoS.reserveDate"
          label="预订场次"
          prop="payType"
        >
          <Tabs
            ref="child"
            :contents="contents"
            :fatherlist="childrenList"
            :tabs="tabs"
            @childByValue="childByValue"
          />
        </el-form-item>

        <el-form-item
          v-if="form.reserveType == 1 && form.detailAddDtoS.reserveDate"
          label="已选场次"
          prop="payType"
        >
          <div class="reservebox">
            <ul>
              <li v-for="(item, idx) in childrenList" :key="idx">
                <div class="listbox">
                  <span>{{ item.orderFieldList[0].fieldName }}</span>
                  <span>{{ item.hourStart }}~{{ item.hourEnd }}</span>
                  <span @click="closeClick(item)">
                    <img src="../../../../assets/images/icon_close.png" />
                  </span>
                </div>
              </li>
            </ul>
          </div>
        </el-form-item>



// x关闭
    closeClick(item) {
      this.$refs.child.change(item)
    },
    // 组件接受值
    childByValue(childValue) {
      console.log(childValue)
      this.childrenList = childValue
      // this.detailField = _.map(childValue, 'fieldId')
    },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3和ElementPlus是一组流行的JavaScript库,使用它们可以方便地开发Web应用,尤其是基于组件的应用。而Tabs是一种常用的UI组件,可以让用户在不同的选项卡之间切换并显示不同的内容。在Vue3和ElementPlus中,实现Tabs切换也是非常容易的。 首先,需要在Vue3和ElementPlus中引入相应的组件,并且在页面中定义Tabs的参数和选项卡的内容。例如: ``` <template> <div> <el-tabs v-model="activeTab" @tab-click="handleTabClick"> <el-tab-pane label="选项卡一">选项卡一的内容</el-tab-pane> <el-tab-pane label="选项卡二">选项卡二的内容</el-tab-pane> <el-tab-pane label="选项卡三">选项卡三的内容</el-tab-pane> </el-tabs> </div> </template> <script> import { ElTabs, ElTabPane } from 'element-plus'; import { ref } from 'vue'; export default { components: { ElTabs, ElTabPane, }, setup() { const activeTab = ref('选项卡一'); const handleTabClick = (tab) => { console.log('点击了', tab.name); }; return { activeTab, handleTabClick, }; }, }; </script> ``` 在上面的代码中,定义了一个包含三个选项卡的Tabs组件,每个选项卡都有一个label和对应的内容。activeTab是用来保存当前选中的选项卡的名称,handleTabClick是用来处理切换选项卡时触发的事件。在代码中打印了点击的选项卡的名称,以便验证切换是否成功。 为了在页面中显示Tabs组件,需要在App.vue中引入这个组件并显示出来: ``` <template> <div id="app"> <Navigation /> <router-view /> <Tabs /> </div> </template> <script> import Tabs from './components/Tabs.vue'; import Navigation from './components/Navigation.vue'; export default { components: { Tabs, Navigation, }, }; </script> ``` 在上面的代码中,引入了Tabs组件并显示出来。同时还引入了另一个自定义组件Navigation来作为页面的导航栏,router-view用来显示页面内容。这里可以根据实际需要调整组件的引入和显示方式。 最后,在页面中点击Tabs的选项卡,就可以看到控制台输出了选项卡的名称,证明切换选项卡已经成功了。这样,就可以轻松实现Vue3和ElementPlus中的Tabs切换功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值