W
W
wiyod2017-10-14 02:35:27
Vue.js
wiyod, 2017-10-14 02:35:27

How to iterate over each element and assign a class in this case?

I am splitting a word by letter and it is necessary to add a class to each element (letter) one by one As far as I understand, if I use then I cannot access the index, say in mounted? How then to be? Well, do not manually write an object with each letter and property in data.
v-for="(word, index) in element.value.split('')"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2017-10-14
@wiyod

So ?

L
lavezzi1, 2017-10-14
@lavezzi1

Inside v-for, you can use the method that is an example as an argument to the index you need. And already inside the method you return the required class for the v-for loop element.

methods: {
  setRandomClass(word, index) {
     return some class here;
  }
}

<div v-for="(word, index) in element.value.split('')" :class="[setRandomClass(word, index)]"></div

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question