1. 程式人生 > >vue2.0 實現click點擊當前li,動態切換class

vue2.0 實現click點擊當前li,動態切換class

一行 for ont pla false style function float del

<template>
  <div>
    <ul>
      <li v-for="(item,$index) in items" @click="selectStyle (item, $index) " :class="{‘active‘:item.active,‘unactive‘:!item.active}" >
      {{item.select}}
      <span class="icon" v-show="item.active">當我是圖標</span>
      </li>
    </ul>
  </div>
</template>

<script>

  import Vue from ‘vue‘

  export default{
    data(){
      return {
        active: false,
        items: [
          {select:‘第一行‘},
          {select:‘第二行‘},
          {select:‘第三行‘},
          {select:‘第四行‘}
        ]
      }
    },


  methods: {
    selectStyle (item, index) {
      this.$nextTick(function () {
        this.items.forEach(function (item) {
          Vue.set(item,‘active‘,false);
        });
        Vue.set(item,‘active‘,true);
      });
    }
  }
}
</script>

<!-- 樣式 -->
<style>
  .active{
    color:red;
  }
  .unactive{
    color:#000;
  }
  .icon{
    float: right;
    font-size:12px;
  }


</style>

vue2.0 實現click點擊當前li,動態切換class