Home > Web Front-end > Vue.js > body text

What is the usage of v-for in vue

WBOY
Release: 2022-03-17 10:52:19
Original
15959 people have browsed it

Usage: 1. Use "v-for="(item,i) in list"" to loop through ordinary arrays; 2. Use "v-for="(user,i) in list"" to loop through arrays Object; 3. Use "v-for="(val,key,i) in user"" to loop the object.

What is the usage of v-for in vue

The operating environment of this article: Windows 10 system, Vue version 2.9.6, DELL G3 computer.

What is the usage of v-for in vue

Several application methods of v-for:

Method 1: v-for loops ordinary arrays

//土蛋方法:
<p>{{list[0]}}</p>
<p>{{list[1]}}</p>
<p>{{list[2]}}</p>
<p>{{list[3]}}</p>
<p>{{list[4]}}</p>

//v-for方法:
<p v-for="(item,i) in list">{{i}},{{item}}</p>

//数组数据部分:
data:{
    list:[1,2,3,4,5,6]
},
Copy after login

Method 2: v-for loop object array

//v-for用法:
<p v-for="(user,i) in list">{{user.id}},{{user.name}},{{i}}</p>

//数组数据部分:
list:[
    {id:1,name:"zs1"},
    {id:2,name:"zs2"},
    {id:3,name:"zs3"},
    {id:4,name:"zs4"},
]
Copy after login

Method 3: v-for loop object

Note: When traversing the key-value pairs on the object, in addition to the val key, there is an index value in the third position

//v-for方法
<p v-for="(val,key,i) in user">{{val}},{{key}}</p>


//对象部分:
user:{
    id:1,
    name:"巧克力"
    gender:"萌妹"
}
Copy after login

Method 4: v-for iterates the array

//in后面我们放过 普通数组 对象数组 对象 还可以放数字
//注意:如果使用 v-for 迭代数字的话,前面的count值从1开始
<p v-for="count in 10">这是第{{count}}次循环</p>
Copy after login

[Related recommendations: "vue.js tutorial"]

The above is the detailed content of What is the usage of v-for in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template