ArkTS的状态管理机制(State)

什么是ArkTS的状态管理机制 

声明式UI中,是以状态(State)来驱动视图更新的(View)。

状态是指驱动视图更新的数据(被装饰器标记的变量)。

视图是指UI描述渲染得到的用户页面。

互动事件可以改变状态的值。状态改变以后,又会触发事件,渲染页面。

这就形成了ArkTS的状态管理机制。

ArkTS的装饰器

ArkTS中的状态管理分为很多种不同的装饰器,他们有多种不一样的作用

@state 、 @Prop、 @Link 、@Provide 、 @Consume 、 @Observed 、 @ObjectLink

State装饰器

注意:

  1. @State装饰器标记的变量必须初始化,不能为空值
  2. @State装饰器支持Object、class、string、number、boolean、enum等类型以及这些类型的数据,不可以any、union等复杂类型。
  3. 嵌套类型以及数组中的对象无法触发视图更新。

案例 HelloWorld

@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.message = 'Hello ArkTS'
          })
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
    }

  }
}

我们将@State去掉 就不能产生这个效果。 因为普通变量不能触发状态的渲染。

验证@State装饰器的作用

1.@State装饰器必须初始化

我们发现State装饰器没有初始值会报错。所以State装饰器必须要有初始值。

2.@State装饰器的支持类型

1.基本类型

 我们前面使用的是基本类型string,证明@State是支持基本类型的。

2.对象类型

我们定义一个类,@State使用对象类型来做声明。 


class Person{
  name:string
  age:number

  constructor(name:string,age:number) {
    this.name = name
    this.age = age
  }
}

@Entry
@Component
struct StatePage {
  @State p:Person = new Person('zhangsan',21)
  build() {
    Row() {
      Column() {
        Text(`${this.p.name} : ${this.p.age}`)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.p.age++
          })
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
    }

  }
}

我们会发现对象类型也可以动态的修改状态。

3.嵌套类型

嵌套类型中的属性改变不会触发视图的更新。


class Person{
  name:string
  age:number
  gf: Person

  constructor(name:string,age:number,gf?: Person) {
    this.name = name
    this.age = age
    this.gf = gf
  }
}

@Entry
@Component
struct StatePage {
  @State p:Person = new Person('zhangsan',21,new Person('LiSi',19))
  build() {
    Row() {
      Column() {
        Text(`${this.p.name} : ${this.p.age}`)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.p.age++
          })
        Text(`${this.p.gf.name} : ${this.p.gf.age}`)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.p.gf.age++
          })
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
    }

  }
}

我们会发现使用嵌套类型的时候,嵌套的对象不可以触发视图的更新。但其实值是加上的,在点击对象类型的时候,触发了渲染。 

4.数组

当@State声明的类型是一个数组的时候,数组的增添会触发视图的渲染,但是数组中如果存在对象,对象内的属性的更改不会触发渲染。


class Person{
  name:string
  age:number
  gf: Person

  constructor(name:string,age:number,gf?: Person) {
    this.name = name
    this.age = age
    this.gf = gf
  }
}

@Entry
@Component
struct StatePage {
  idx: number = 1
  @State p:Person = new Person('zhangsan',21,new Person('LiSi',19))
  @State gfs: Person[] = [
    new Person('WangWu',18),
    new Person('LaoLiu',78)
  ]
  build() {
    Row() {
      Column() {
        Text(`${this.p.name} : ${this.p.age}`)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.p.age++
          })
        Button('添加女友')
          .onClick(() => {
            this.gfs.push(new Person(`女友` + this.idx++,20))
          })
       Text('=女友列表=')
         .fontSize(50)
         .fontWeight(FontWeight.Bold)
        ForEach(
          this.gfs,
          (item,index) => {
            Row(){
              Text(`${item.name} : ${item.age}`)
                .fontSize(30)
                .onClick(()=>{
                  item.age++
                })
              Button("删除")
                .onClick(() => {
                  this.gfs.splice(index,1)
                })
            }
            .width('100%')
            .justifyContent(FlexAlign.SpaceAround)

          }
        )
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
    }

  }
}

我们会发现,增添和删除是可以触发页面的刷新呢,但是,数组中的对象的更改不会触发页面的渲染。

  • 13
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web阿成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值