标签:一个 实现 相等 item list change bind 指定 active
    <div id="app">
        <div class="tab">
            <!--  tab栏  -->
            <ul>
                <li class="active">apple</li>
                <li class="">orange</li>
                <li class="">lemon</li>
            </ul>
              <!--  对应显示的图片 -->
            <div class="current"><img src="img/apple.png"></div>
            <div class=""><img src="img/orange.png"></div>
            <div class=""><img src="img/lemon.png"></div>
        </div>
    </div>
         list: [{
                    id: 1,
                    title: ‘apple‘,
                    path: ‘img/apple.png‘
                }, {
                    id: 2,
                    title: ‘orange‘,
                    path: ‘img/orange.png‘
                }, {
                    id: 3,
                    title: ‘lemon‘,
                    path: ‘img/lemon.png‘
                }]
把tab栏 中的数替换到页面上
    <div id="app">
        <div class="tab">  
            <ul>
                  <!--  
                    1、绑定key的作用 提高Vue的性能 
                    2、 key 需要是唯一的标识 所以需要使用id, 也可以使用index ,
						index 也是唯一的 
                    3、 item 是 数组中对应的每一项  
                    4、 index 是 每一项的 索引
                -->
                   <li :key=‘item.id‘ v-for=‘(item,index) in list‘>{{item.title}}</li>
              </ul>
              <div  :key=‘item.id‘ v-for=‘(item, index) in list‘>
                    <!-- :  是 v-bind 的简写   绑定属性使用 v-bind -->
                    <img :src="item.path">
              </div>
        </div>
    </div>
<script>
    new  Vue({
        //  指定 操作元素 是 id 为app 的 
        el: ‘#app‘,
            data: {
                list: [{
                    id: 1,
                    title: ‘apple‘,
                    path: ‘img/apple.png‘
                }, {
                    id: 2,
                    title: ‘orange‘,
                    path: ‘img/orange.png‘
                }, {
                    id: 3,
                    title: ‘lemon‘,
                    path: ‘img/lemon.png‘
                }]
            }
    })
</script>
4.1 、让默认的第一项tab栏高亮
4.2 、让默认的第一项tab栏对应的div 显示
  <ul>
	   <!-- 动态绑定class   有 active   类名高亮  无 active   不高亮-->
       <li  :class=‘currentIndex==index?"active":""‘
           :key=‘item.id‘ v-for=‘(item,index) in list‘
           >{{item.title}}</li>
  </ul>
	<!-- 动态绑定class   有 current  类名显示  无 current  隐藏-->
  <div :class=‘currentIndex==index?"current":""‘ 
       
       :key=‘item.id‘ v-for=‘(item, index) in list‘>
        <!-- :  是 v-bind 的简写   绑定属性使用 v-bind -->
        <img :src="item.path">
  </div>
<script>
    new  Vue({
        el: ‘#app‘,
            data: {
                currentIndex: 0, // 选项卡当前的索引  默认为 0  
                list: [{
                    id: 1,
                    title: ‘apple‘,
                    path: ‘img/apple.png‘
                }, {
                    id: 2,
                    title: ‘orange‘,
                    path: ‘img/orange.png‘
                }, {
                    id: 3,
                    title: ‘lemon‘,
                    path: ‘img/lemon.png‘
                }]
            }
    })
</script>
4.3 、点击每一个tab栏 当前的高亮 其他的取消高亮
给每一个li添加点击事件
让当前的索引 index 和 当前 currentIndex 的 值 进项比较
如果相等 则当前li 添加active 类名 当前的 li 高亮 当前对应索引的 div 添加 current 当前div 显示 其他隐藏
    <div id="app">
        <div class="tab">
            <ul>
                <!--  通过v-on 添加点击事件   需要把当前li 的索引传过去 
				-->
                <li v-on:click=‘change(index)‘		           			
                    :class=‘currentIndex==index?"active":""‘                   
                    :key=‘item.id‘ 
                    v-for=‘(item,index) in list‘>{{item.title}}</li>
            </ul>
            <div :class=‘currentIndex==index?"current":""‘ 
                 :key=‘item.id‘ v-for=‘(item, index) in list‘>
                <img :src="item.path">
            </div>
        </div>
    </div>
<script>
    new  Vue({
        el: ‘#app‘,
            data: {
                currentIndex: 0, // 选项卡当前的索引  默认为 0  
                list: [{
                    id: 1,
                    title: ‘apple‘,
                    path: ‘img/apple.png‘
                }, {
                    id: 2,
                    title: ‘orange‘,
                    path: ‘img/orange.png‘
                }, {
                    id: 3,
                    title: ‘lemon‘,
                    path: ‘img/lemon.png‘
                }]
            },
            methods: {
                change: function(index) {
                    // 通过传入过来的索引来让当前的  currentIndex  和点击的index 值 相等 
                    //  从而实现 控制类名    
                    this.currentIndex = index;
                }
            }
    
    })
</script>
标签:一个 实现 相等 item list change bind 指定 active
原文地址:https://www.cnblogs.com/royal6/p/12606381.html