Vue Click Switch Class change and achieve Active current style

Vue just started not long, meet a lot of simple and practical tips in the production process.

There is not much ink directly on the code:

 

First, the first increase in a variable in the data used to store the elements of the current click

data() {
    return {
      activeClass: -1, // 0为默认选择第一个,-1为不选择
    };
  },

 Second, the code inside the Template, which @click To remember the pass index,

<li :class="activeClass == index ? 'active':''" v-for="(item,index) in itemList" :key="index" @click="getItem(index)">
          {{itme.text}}
        </li>

Third, the click event: changing the value of data inside activeClass

    getItme(index) {
      this.activeClass = index;  // 把当前点击元素的index,赋值给activeClass
    },

Fourth, in the style of the writing style on .active

.active {
  /* background: #eee; */
  color: #1e82d2;
  font-weight: bolder;
}

 

Published 18 original articles · won praise 10 · views 50000 +

Guess you like

Origin blog.csdn.net/a350110085/article/details/81605092