当前位置:   article > 正文

HarmonyOS--ArkTS(3)--渲染控制(if/else,ForEach)_harmonyos if组件

harmonyos if组件

 文档地址:icon-default.png?t=N7T8https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/arkts-rendering-control-ifelse-0000001524177637-V3#section550519414249

if/else:条件渲染

  1. @Entry
  2. @Component
  3. struct ViewA {
  4. @State count: number = 0;
  5. build() {
  6. Column() {
  7. Text(`count=${this.count}`)
  8. if (this.count > 1) {
  9. Text(`count 大于 1`)
  10. }else{
  11. Text(`啥也不是`)
  12. }
  13. }
  14. }

ForEach:循环渲染

  1. //三大参数
  2. ForEach(
  3. //要遍历的数组
  4. arr: Array,
  5. //页面组件生成函数
  6. itemGenerator: (item: any, index?: number) => {
  7. },
  8. //键 生成函数--为数组每一项生成唯一标识,组件是否重新渲染的标准
  9. keyGenerator?: (item: any, index?: number): string => {
  10. }
  11. )

用法 

  1. @Entry
  2. @Component
  3. struct Parent {
  4. @State simpleList: Array<string> = ['one', 'two', 'three'];
  5. build() {
  6. Row() {
  7. Column() {
  8. ForEach(this.simpleList, (item: string) => {
  9. ChildItem({ item: item })
  10. }, (item: string) => item)
  11. }
  12. .width('100%')
  13. .height('100%')
  14. }
  15. .height('100%')
  16. .backgroundColor(0xF1F3F5)
  17. }
  18. }
  19. @Component
  20. struct ChildItem {
  21. @Prop item: string;
  22. build() {
  23. Text(this.item)
  24. .fontSize(50)
  25. }
  26. }

ForEach 循环 项过多 超出屏幕的时候  使用 容器组件 List 

HarmonyOS--容器组件 List-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/326541
推荐阅读
相关标签
  

闽ICP备14008679号