工具:HBuilder X
图片来自网络侵权私信删除
v-for 指令需要以 student in students形式的特殊语法, students是源数据数组并且 student 是数组元素迭代 (交换替代) 的别名
v-for 可以绑定数据到数组来渲染一个列表
今天学习for循环,先上案例:
然后看一下源码:
<template>
<view>
<view class="demo">
<view class="demo2" v-for="student in students">
<!-- 头像 -->
<view class="img-v">
<image class="img" :src="student.img"></image>
</view>
<!-- 信息 -->
<view class="user">
<view>姓名:{{student.name}}</view>
<view>id:{{student.id}}</view>
<view>性别:{{student.sex}}</view>
<view>年龄:{{student.age}}</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
students:[{
img:'https://img2.baidu.com/it/u=895967582,2760717342&fm=26&fmt=auto',
name:"张三",
id:1,
sex:"male",
age:18
},{
img:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftx20%2F09041801136147.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638932347&t=928f444819f4e3c2c174f60990b7d47e',
name:"李四",
id:2,
sex:"male",
age:18
},{
img:'https://img1.baidu.com/it/u=1924720413,2026555247&fm=26&fmt=auto',
name:"王五",
id:3,
sex:"male",
age:18
},{
img:'https://img1.baidu.com/it/u=1236883555,3923456608&fm=26&fmt=auto',
name:"赵六",
id:4,
sex:"male",
age:18
},{
img:'https://img1.baidu.com/it/u=3443724077,4284733591&fm=26&fmt=auto',
name:"孙七",
id:5,
sex:"male",
age:18
}]
}
},
onLoad() {
},
methods: {
}
}
</script>
<style>
.img{
width: 200rpx;
height: 200rpx;
border-radius: 25%;
}
.demo2{
display: flex;
/* justify-content: center; */
align-items: center;
}
.img-v{
margin-right: 50rpx;
margin-top: 20rpx;
margin-left: 20rpx;
/* background-color: #007AFF; */
}
.user{
background-image: linear-gradient(120deg, #a6c0fe 0%, #f68084 100%);
display: flex;
flex-direction: column;
justify-content: center;
padding-left: 100rpx;
padding-right: 100rpx;
padding-top: 25rpx;
padding-bottom: 25rpx;
border-radius: 25rpx;
margin-top: 20rpx;
color: #FFFFFF;
font-weight: 1000;
}
</style>