当前位置:   article > 正文

梅科尔工作室-张威-鸿蒙笔记三_鸿蒙 foreach

鸿蒙 foreach

list组件

由list容器组件和listitem容器组件构成,list是一个大容器,listitem是大容器里的小容器

代码
  1. List(){
  2.     ListItem(){
  3. }
  4. }
示例
  1. List( ) {
  2. ListItem(){
  3. Text("示例1")
  4. .fontSize(30).width("90%").height("10%")
  5. }
  6. ListItem(){
  7. Text("示例2")
  8. fontSize(30).width("90%").height("10%")
  9. }
  10. ListItem(){
  11. Text("示例3")
  12. .fontSize(30) .width("90%").height("10%")
  13. }
  14. ListItem(){
  15. Text("示例4")
  16. .fontSize(30).width("90%").height("10%")
  17. }
  18. }
运行结果

父子组件(自定义组件)

子组件在另一个文件夹中新建一个,导出用export语句

父组件调用了子组件,导入用import {子组件文件名称} from "子组件文件相对路径"

双向数据绑定

改变任何一方数据时,两方数据都会变为改变的一方数据

子组件中数据用@Link修饰

父组件中用@State修饰,在子组件接口中数据用$修饰

if-else渲染

使用语法

if/else渲染可以改变组件的渲染状态,即决定组件是否在页面中被渲染。if括号内的变量是true的话,则对应下的组件都被渲染,否则都不被渲染。

注意事项

必须在容器组件内使用。

使用if/else可以使子组件的渲染依赖条件语句。

示例

使用if条件语句:

  1. Column(){
  2. if(this.count>0){
  3. Text('count is positive')
  4. }}

使用if、else if、else条件语句:

  1. Column(){
  2. if(this.count<0){
  3. Text('count is negative')
  4. }
  5. else if(this.count%2===0){
  6. Divider
  7. Text('even')
  8. } else{
  9. Divider
  10. Text('odd')
  11. }
  12. }

for循环渲染

开发框架提供循环渲染(ForEach组件)来迭代数组,并为每个数组项创建相应的组件。ForEach定义如下:

使用语法

  1. ForEach(
  2. arr: any[],
  3. itemGenerator: (item: any, index?: number) => void,
  4. keyGenerator?: (item: any, index?: number) => string
  5. )

注意事项

·必须在容器组件内使用;

·生成的子组件允许在ForEach的父容器组件中

·允许子组件生成器函数中包含if/else条件渲染

·同时也允许ForEach包含在if/else条件渲染语句中;

示例

简单类型数组示例:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct MyComponent {
  5. @State arr: number[] = [10, 20, 30]
  6. build() {
  7. Column({ space: 5 }) {
  8. Button('Reverse Array')
  9. .onClick(() => {
  10. this.arr.reverse()
  11. })
  12. ForEach(this.arr, (item: number) => {
  13. Text(`item value: ${item}`).fontSize(18)
  14. Divider().strokeWidth(2)
  15. }, (item: number) => item.toString())
  16. }
  17. }
  18. }

运用

循环列表
代码
  1. // @ts{child} from "../common/child"
  2. @Entry
  3. @Component
  4. struct Index {
  5. @State fathernum: string = '222'
  6. @State numlist:number[] = [0,1,2,3,4,5,6,7,8,9]
  7. @State num:number=1
  8. build() {
  9. Column() {
  10. Text("你心目中的男神")
  11. .fontSize(40)
  12. List({space:20}){
  13. ForEach(this.numlist,(item,index)=>{
  14. ListItem(){
  15. Row() {
  16. Text(item.toString())
  17. .fontSize(25).width("100%").height("10%")
  18. .backgroundColor("#DCDCDC").textAlign(TextAlign.Center)
  19. .borderRadius(25)
  20. }
  21. }
  22. })
  23. }
  24. }
  25. .width('100%').height('100%')
  26. .justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
  27. }
  28. }
效果‘

可滑动,有弹性

父子代码
子代码
  1. @Component
  2. export struct child {
  3. @Link childnum:number
  4. @Link childstatu:boolean
  5. build(){
  6. Column({space:10}) {
  7. if (this.childstatu) {
  8. Text(this.childnum.toString())
  9. .fontSize(30)
  10. }
  11. Button("增加")
  12. .width(100).height(50)
  13. .onClick(()=>{
  14. this.childnum++
  15. })
  16. Button("减少")
  17. .width(100).height(50)
  18. .onClick(()=>{
  19. this.childnum--
  20. })
  21. Button("显示")
  22. .width(100).height(50)
  23. .onClick(()=>{
  24. this.childstatu =! this.childstatu
  25. })
  26. }
  27. }
  28. }
父代码
  1. import {child} from "../common/child"
  2. @Entry
  3. @Component
  4. struct Second{
  5. @State fathernum:number=0
  6. @State fatherstatu:boolean = true
  7. build(){
  8. Row(){
  9. Column(){
  10. child({childnum:$fathernum,childstatu:$fatherstatu})
  11. }
  12. .width('100%')
  13. }
  14. .height('100%')
  15. }
  16. }
效果

点增加数字+1,点减少数字-1,点显示可以切换数字是否显示

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

闽ICP备14008679号