列表渲染之基本列表v-for
1.使用v-for让vue自动进行循环遍历data中写的数据。代码中,data中的persons1写了三个id的数据。将v-for使用在一个小li标签中,在网页中则自动生成了三个小li标签数据。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基本列表</title>
<script type="text/javascript" src="../vue.js">
Vue.config.productionTip = false
</script>
</head>
<body>
<div id="root">
<ul>
<li v-for="XianShi in persons1">{{XianShi}}</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
const x = new Vue({
el: '#root',
data() {
return {
persons1: [
{
id: '001', name: '张三', age: 18
},
{
id: '002', name: '李四', age: 19
},
{
id: '003', name: '王五', age: 20
}
]
}
},
})
</script>
</body>
</html>
2.实际上我们不需要显示出这么多的信息,只需要显示有用的姓名和 年龄就够了。那么就在插值语法(两个中括号内做具体修改要显示的数据)。 注意:其中v-for语句里边的in也能换成of。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基本列表</title>
<script type="text/javascript" src="../vue.js">
Vue.config.productionTip = false
</script>
</head>
<body>
<div id="root">
<ul>
<li v-for="XianShi in persons1">{{XianShi.name}}-{{XianShi.age}}</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
const x = new Vue({
el: '#root',
data() {
return {
persons1: [
{
id: '001', name: '张三', age: 18
},
{
id: '002', name: '李四', age: 19
},
{
id: '003', name: '王五', age: 20
}
]
}
},
})
</script>
</body>
</html>
3.v-for不仅能遍历数据中的数组形式数据。还能遍历对象类型。注意这里v-for的括号写法。其中的a和b所代替的意义。从结果图片上看,值和关键词。即value和key的值反了。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基本列表</title>
<script type="text/javascript" src="../vue.js">
Vue.config.productionTip = false
</script>
</head>
<body>
<div id="root">
<ul>
<li v-for="XianShi in persons1">{{XianShi.name}}-{{XianShi.age}}</li>
</ul>
<ul>
<li v-for="(a,b) in car">{{a}}-{{b}}</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
const x = new Vue({
el: '#root',
data() {
return {
persons1: [
{
id: '001', name: '张三', age: 18
},
{
id: '002', name: '李四', age: 19
},
{
id: '003', name: '王五', age: 20
}
],
car: {
name: "奥迪A8",
price: "70w",
color: "black"
}
}
},
})
</script>
</body>
</html>
4.解决第3点中显示结果出现反的问题。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基本列表</title>
<script type="text/javascript" src="../vue.js">
Vue.config.productionTip = false
</script>
</head>
<body>
<div id="root">
<ul>
<li v-for="XianShi in persons1">{{XianShi.name}}-{{XianShi.age}}</li>
</ul>
<ul>
<li v-for="(value,k) in car" :key="k">{{k}}-{{value}}</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
const x = new Vue({
el: '#root',
data() {
return {
persons1: [
{
id: '001', name: '张三', age: 18
},
{
id: '002', name: '李四', age: 19
},
{
id: '003', name: '王五', age: 20
}
],
car: {
name: "奥迪A8",
price: "70w",
color: "black"
}
}
},
})
</script>
</body>
</html>
5.v-for也可以遍历字符串,不过使用不多。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>基本列表</title>
<script type="text/javascript" src="../vue.js">
Vue.config.productionTip = false
</script>
</head>
<body>
<div id="root">
<ul>
<li v-for="XianShi in persons1">{{XianShi.name}}-{{XianShi.age}}</li>
</ul>
<ul>
<li v-for="(value,k) in car" :key="k">{{k}}-{{value}}</li>
</ul>
<ul>
<li v-for="(char,index) of str" :key="index">{{char}}-{{index}}</li>
</ul>
</div>
<script type="text/javascript">
Vue.config.productionTip = false
const x = new Vue({
el: '#root',
data() {
return {
persons1: [
{
id: '001', name: '张三', age: 18
},
{
id: '002', name: '李四', age: 19
},
{
id: '003', name: '王五', age: 20
}
],
car: {
name: "奥迪A8",
price: "70w",
color: "black"
},
str: 'hello'
}
},
})
</script>
</body>
</html>
总结
V- for指令: 1.用于展示列表数据 2.语法: v-for="(item, index) in XXX" :key= “yyy” 3.可遍历:数组、对象、字符串(用的很少)、指定次数(用的很少)
|