Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js, simple v-for / v-bind to add index to class name

Tags:

vue.js

I can't seem to find the syntax to loop through my items and give each 'li' a class corresponding to the index. How is this done?

<li v-for="(item, idx) in items class="idx"">
</li>
like image 951
rix Avatar asked Oct 13 '16 19:10

rix


People also ask

How do I add a class dynamically on Vue?

Adding a dynamic class name is as simple as adding the prop :class="classname" to your component. Whatever classname evaluates to will be the class name that is added to your component. Join 11,067 other Vue devs and get exclusive tips and insights delivered straight to your inbox, every week.

What does V-bind do in Vue?

The v-bind directive is a Vuejs directive used to bind one or more attributes, or a component prop to an element. If that attribute is binded to our data defined in Vuejs instance then dynamically changes can be observed as data changes.

What is the shorthand for V-bind in Vue?

v-bind shorthand Instead of writing v-bind Vue provides a shorthand with only a colon : .

What is the difference between v-model and V-bind?

v-bind is a one-way binding. v-model is used for binding form elements like inputs, radio buttons, textarea, checkboxes. It is used for binding data, attributes, expressions, class, styles. V-model is input value sensitive.


1 Answers

I think this would be most readable (added a prefix because a class can't start with a number).

<li v-for="(item, index) in items" :class="[`index--${index}`]"></li>

Also using an array because this always looked off to me.

:class="`index--${index}`"
like image 64
Bill Criswell Avatar answered Sep 28 '22 15:09

Bill Criswell