VUE指令(3)——v-for
遍历数据渲染页面是非常常用的需求,Vue 中通过 v-for 指令来实现1.遍历数组语法:v-for=“item in items”items:要遍历的数组,需要在 vue 的 data 中定义好item:迭代得到的当前正在遍历的元素<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
·
遍历数据渲染页面是非常常用的需求,Vue 中通过 v-for 指令来实现
1.遍历数组
语法:v-for=“item in items”
- items:要遍历的数组,需要在 vue 的 data 中定义好
- item:迭代得到的当前正在遍历的元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for</title>
</head>
<body>
<div id="app">
<ul>
<!-- v-for遍历建议使用:key唯一属性,加快vue渲染 -->
<li v-for="p in users" :key="p.name">
{{p.name}}---{{p.gender}}---{{p.age}}
</li>
</ul>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
users: [
{ name: '柳岩', gender: '女', age: 21 },
{ name: '赵奕欢', gender: '女', age: 18 },
{ name: '范冰冰', gender: '女', age: 24 },
{ name: '刘亦菲', gender: '女', age: 18 },
{ name: '古力娜扎', gender: '女', age: 25 }]
}
})
</script>
</body>
</html>
2.显示数组索引
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for</title>
</head>
<body>
<div id="app">
<ul>
<!-- v-for遍历建议使用:key唯一属性,加快vue渲染 -->
<li v-for="p in users" :key="p.name">
{{p.name}}---{{p.gender}}---{{p.age}}
</li>
<hr>
<p style="font-size: larger;">遍历并显示当前索引值:</p>
<li v-for="(p,index) in users">
当前索引为{{index}}---{{p.name}}---{{p.sex}}---{{p.age}}
</li><br><br>
</ul>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
users: [
{ name: '柳岩', gender: '女', age: 21 },
{ name: '赵奕欢', gender: '女', age: 18 },
{ name: '范冰冰', gender: '女', age: 24 },
{ name: '刘亦菲', gender: '女', age: 18 },
{ name: '古力娜扎', gender: '女', age: 25 }]
}
})
</script>
</body>
</html>
3.遍历对象
v-for 除了可以迭代数组,也可以迭代对象。语法基本类似
v-for=“value in object”
v-for="(value,key) in object"
v-for="(value,key,index) in object"
- 1 个参数时,得到的是对象的属性值
- 2 个参数时,第一个是属性值,第二个是属性名
- 3 个参数时,第三个是索引,从 0 开始
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for</title>
</head>
<body>
<div id="app">
<ul>
<!-- v-for遍历建议使用:key唯一属性,加快vue渲染 -->
<li v-for="p in users" :key="p.name">
{{p.name}}---{{p.gender}}---{{p.age}}
<p style="font-size: small;">遍历对象信息:</p>
<span v-for="user in p">{{user}},</span>
</li>
<hr>
<p style="font-size: larger;">遍历并显示当前索引值:</p>
<li v-for="(p,index) in users">
当前索引为{{index}}---{{p.name}}---{{p.sex}}---{{p.age}}
<p style="font-size: medium;">遍历对象信息显示键值:</p>
<span v-for="(user,k) in p">{{k}}:{{user}} </span>
<p style="font-size: larger;">遍历对象信息显示键值和索引:</p>
<span v-for="(i,k,user) in p">{{user}} :{{k}} ==>{{i}} </span>
</li><br><br>
</ul>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
users: [
{ name: '柳岩', gender: '女', age: 21 },
{ name: '赵奕欢', gender: '女', age: 18 },
{ name: '范冰冰', gender: '女', age: 24 },
{ name: '刘亦菲', gender: '女', age: 18 },
{ name: '古力娜扎', gender: '女', age: 25 }]
}
})
</script>
</body>
</html>
4.Key
用来标识每一个元素的唯一特征,这样 Vue 可以使用“就地复用”策略有效的提高渲染的效率(加快vue渲染)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for</title>
</head>
<body>
<div id="app">
<ul>
<!-- v-for遍历建议使用:key唯一属性,加快vue渲染 -->
<li v-for="p in users" :key="p.name">
{{p.name}}---{{p.gender}}---{{p.age}}
<p style="font-size: small;">遍历对象信息:</p>
<span v-for="user in p">{{user}},</span>
<hr>
<p style="font-size: larger;">遍历并显示当前索引值:</p>
<li v-for="(p,index) in users">
当前索引为{{index}}---{{p.name}}---{{p.sex}}---{{p.age}}
<p style="font-size: medium;">遍历对象信息显示键值:</p>
<span v-for="(user,k) in p">{{k}}:{{user}} </span>
<p style="font-size: larger;">遍历对象信息显示键值和索引:</p>
<span v-for="(i,k,user) in p">{{user}} :{{k}} ==>{{i}} </span>
</li><br><br>
</ul>
<p>遍历nums:</p>
<ul>
<li v-for="(num,index) in nums" :key="index">{{num}}</li>
</ul>
</div>
<script src="./node_modules/vue/dist/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
users: [
{ name: '柳岩', gender: '女', age: 21 },
{ name: '赵奕欢', gender: '女', age: 18 },
{ name: '范冰冰', gender: '女', age: 24 },
{ name: '刘亦菲', gender: '女', age: 18 },
{ name: '古力娜扎', gender: '女', age: 25 }],
nums: [1, 2, 3, 4, 4]
}
})
</script>
</body>
</html>
更多推荐
已为社区贡献1条内容
所有评论(0)