鸿蒙开发系列教程(九)--ArkTS语言:ForEach循环渲染

ForEach:循环渲染

官方:ForEach接口基于数组类型数据来进行循环渲染,需要与容器组件配合使用,且接口返回的组件应当是允许包含在ForEach父容器组件中的子组件。

语法:

ForEach(

arr: Array,

itemGenerator: (item: any, index?: number) => void,

​ keyGenerator?: (item: any, index?: number) => string

)

参数解释:

1、arr: Array 要遍历的数据,数组

2、itemGenerator: (item: any, index?: number) => void,

​ itemGenerator: (item: any, index?: number) => {},

​ 组件生成函数(数组中的数据项,数组中的数据项索引-可省略)

返回函数或void

3、 keyGenerator?: (item: any, index?: number) => string

可省略,键值生成函数(数组中的数据项,数组中的数据项索引-可省略)

如果函数缺省,框架默认的键值生成函数为(item: T, index: number) => { return index + ‘__’ + JSON.stringify(item); }

示例1:

@State simpleList: Array<string> = ['one', 'two', 'three'];

ForEach(this.simpleList, (item2: string) => {
          Text(item2).fontColor(Color.Red)
  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值