haizlin / fe-interview

前端面试每日 3+1,以面试题来驱动学习,提倡每日学习与思考,每天进步一点!每天早上5点纯手工发布面试题(死磕自己,愉悦大家),6000+道前端面试题全面覆盖,HTML/CSS/JavaScript/Vue/React/Nodejs/TypeScript/ECMAScritpt/Webpack/Jquery/小程序/软技能……
http://www.h-camel.com
MIT License
25.49k stars 3.26k forks source link

[vue] 使用vue写一个tab切换 #453

Open haizhilin2013 opened 5 years ago

haizhilin2013 commented 5 years ago

[vue] 使用vue写一个tab切换

maozhuo123 commented 5 years ago

v-for循环list,根据索引值设置active的样式和显示内容

HeMin0919 commented 5 years ago

v-for循环,利用下标和v-show显示

`<div id="app">
    <ul class="tabs">
        <li class="li-tab" v-for="(item,index) in tabsParam" 
        @click="toggleTabs(index)" 
        :class="index===nowIndex?'active':''">{{item}}</li>
    </ul>
    <div class="divTab" v-show="nowIndex===0">我是tab1</div>
    <div class="divTab" v-show="nowIndex===1">我是tab2</div>
    <div class="divTab" v-show="nowIndex===2">我是tab3</div>
    <div class="divTab" v-show="nowIndex===3">我是tab4</div>
</div>`
zhengwei1949 commented 5 years ago

作用域 插槽+子组件

canwdev commented 4 years ago
<template>
  <div class="tab-wrap">
    <div class="tabs">
      <button
        v-for="i in list"
        :key="i.id"
        @click="choose = i.id"
      >{{i.title}}</button>
    </div>
    <div class="content">{{list[choose].content}}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {id: 0, title: 'A', content: 'aaaaaaaaaaaaaa'},
        {id: 1, title: 'B', content: 'bbbbbbbbbbbbbb'},
        {id: 2, title: 'C', content: 'cccccccccccccc'}
      ],
      choose: 0
    }
  }
}
</script>
Guokaiming commented 4 years ago

你们是怎么做到大部分的人的写法都相同的- -

Youth-spirit commented 2 years ago

v-for循环数据在使用动态绑定类

<div v-for=(itemContent,index) in tcdata :key=index v-show=index==num

{{itemCon}}

const vm=new Vue({ el:'#app', data(){ return{ tdata:['栏目一','栏目二','栏目三'], tcdata:['一','二','三'], num:0 }, methods:{ handoff(index){ this.num=index } } } })

2860754963 commented 1 year ago
<template>
  <div>
    <button v-for="obj in list" :key="obj.id" @click="choose = obj.id">
      {{ obj.title }}
    </button>
    <div>{{ list[choose].content }}</div>
  </div>
</template>
script>
export default {
  name: 'diG',
  data() {
    return {
      list: [
        { id: 0, title: 'A', content: 'aaaaaaaaaaaaaa' },
        { id: 1, title: 'B', content: 'bbbbbbbbbbbbbb' },
        { id: 2, title: 'C', content: 'cccccccccccccc' },
      ],
      choose: 0,
    }
  },
}
</script>