HarmonyOS 开发基础(九)forEach

一、基础使用

@Entry
@Component
struct Index {imageUrl: string = 'https://gw.alicdn.com/imgextra/i2/2201227850912/O1CN01B7gVvP1Ibk6HMiDRz_!!2201227850912.jpg_Q75.jpg_.webp'private items: Array<Object> = [{name: '红米k70', image: this.imageUrl, price: 2499},{name: '红米k70', image: this.imageUrl, price: 2499},{name: '红米k70', image: this.imageUrl, price: 2499},{name: '红米k70', image: this.imageUrl, price: 2499},{name: '红米k70', image: this.imageUrl, price: 2499},{name: '红米k70', image: this.imageUrl, price: 2499},]build() {Column({space: 15}) {Row() {Text('商品列表').fontSize(30).fontWeight(600)}.width('100%').justifyContent(FlexAlign.Start)ForEach(this.items,(item) => {Row() {Column() {Image(item.image).width(60).height(90).interpolation(ImageInterpolation.High)}Column() {Text(item.name).fontSize(20).fontWeight(600).margin({bottom: 5})Text(`¥${item.price}`).fontColor('#F36')}.margin({left: 25}).alignItems(HorizontalAlign.Start)}.width('100%').padding(10).alignItems(VerticalAlign.Top).borderRadius('8').backgroundColor('#ffffff')},(item, index): string => {return item + index})}.width('100%').height('100%').padding(20).backgroundColor('#f0eef0')}
}