repeat 在我看来就是把li(html)或者cell(objective-c)的功能拿出来单独实现
最基础的用法 类似于绑定数据repeat绑定的lis会根据list的元素数渲染相同格式的组件
<template>
<container>
<container repeat = "{{list}}" class="{{gender}}">
<text>{{firstName}}</text>
<text>{{lastName}}</tetx>
</container>
</container>
</template>
<style>
.qw1{
color: red;
}
.qw2{
color: blue;
}
</style>
<script>
module.exports = {
data:{
list:[
{gender:'qw1',firstName:'zhang',lastName:'san'},
{gender:'qw2',firstName:'li',lastName:'si'},
{gender:'qw1',firstName:'wang',lastName:'wu'},
]
}
}
</script>
还可以通过$index获取到数组的index 类似于row(objective-c)
这里有个问题没有解决 当用index的时候只能有一行text 如果我加上两个text他就会出错,如果谁能解决的话也可以告诉我
<template>
<container>
<container repeat = "{{list}}" class="{{gender}}">
<text>{{$index+1}}</text>
</container>
</container>
</template>
// 错误的代码
<template>
<container>
<container repeat = "{{list}}" class="{{gender}}">
<text>{{firstName}}</text>
<text>{{lastName}}</tetx>
<text>{{$index+1}}</text>
</container>
</container>
</template>
repeat还支持获取到list里面的某个属性,不过要先给list放一个对象
<template>
<container>
<container repeat = "{{v in list}}" class="{{gender}}">
<text>No.{{$index + 1}},{{v.firstName}}</text>
<text>No.{{$index + 1}},{{v.lastName}}</text>
</container>
</container>
</template>
<style>
当然 在repeat这个下面还可以继续放一些别的数据