Home > Web Front-end > JS Tutorial > body text

Use of v-for list rendering instructions in Vue.js (code example)

藏色散人
Release: 2019-03-30 09:58:09
Original
3122 people have browsed it

This article will introduce to you how to loop an array and render a list of items in vuejs. I hope it will be helpful to friends in need!

Use of v-for list rendering instructions in Vue.js (code example)

##v-forDirectives

Vuejs Provides us with a v-for directive for rendering a list of items into the dom.

v-for Syntax of the directive

v-for="user in users"

Copy after login

Example




Copy after login

is above In the code, we use the

v-for instruction to loop through the users array, so that in each loop the user variable points to a different object that appears in the array.

Use of v-for list rendering instructions in Vue.js (code example)

keyAttribute

When using

v-for directive, we need to add a key attribute to the element because vuejs needs to track the list item based on the provided key.

Note: Key should be unique

Let’s add the key attribute to the template.



Copy after login

In the

users array, the id attribute is unique to each object, so we pass it to the key attribute.

We can also access the index of each item in the array.

Copy after login

Iterating over objects

We can also loop over

JavaScript objects by using the v-for directive.



Copy after login

Use of v-for list rendering instructions in Vue.js (code example)

Note: In the object, we need to extract the

value first, and then the key.

This article is about the use of v-for list rendering instructions in Vue.js. I hope it will be helpful to friends in need!

The above is the detailed content of Use of v-for list rendering instructions in Vue.js (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!