vue tab切换功能

<template>
  <div id="app">
    <div class="radio-wrap">
      <div class="radio-group" v-model="tabView"> 
        <span v-for="(tab ,index) in tabs" :class="{cur:iscur==index}" @click="iscur=index,tabChange('select' + (index + 1))">{{tab.name}}</span>
      </div>
      <div style="margin:10px 0;"></div>
      <keep-alive> 
        <component v-bind:is="tabView"></component>
      </keep-alive> 
    </div>
  </div>
</template>
 
<script>
  import select1 from './components/select01.vue';
  import select2 from './components/select02.vue';
  import select3 from './components/select03.vue';
export default {
  name: 'app',
  data () {
    return {
      tabView: 'select1',
      tabs: [{name: "选项一"}, {name: "选项二"} ,{name: "选项三"}],
      iscur: 0
    }
  },
  components: {
    select1,
    select2,
    select3
  },
  methods: {
    tabChange:function(tab){
      this.tabView = tab;
    }
  }
}
</script>
 
<style>
  .radio-group{font-size:0;height: 26px;line-height:26px;}
  .radio-group>span{cursor:pointer;display:inline-block;font-size:16px;text-align:center;width:100px;}
  .cur{color:#fff;background-color: #20a0ff;}
</style>
<template>
  <div>
    <div class="tab" @click="toggleTab('prince')"><a>小王子</a></div>
    <div class="tab" @click="toggleTab('rose')"><a>小玫瑰</a></div>
    <div class="tab" @click="toggleTab('fox')"><a>小狐狸</a></div>
    <keep-alive>
      <prince :is="currentTab"></prince>
    </keep-alive>
  </div>
</template>

<script>
import prince from '@/components/prince'
import rose from '@/components/rose'
import fox from '@/components/fox'
export default {
  name: 'index',
  data () {
    return {
      currentTab: 'prince'
    }
  },
  components: { // 声明子组件
    prince,
    rose,
    fox
  },
  methods: {
    toggleTab: function (tab) {
      this.currentTab = tab // tab 为当前触发标签页的组件名
    }
  }
}
</script>

<style scoped>
</style>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值