当前位置:   article > 正文

ArkTS的状态管理机制(State)_arkts state

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

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State message: string = 'Hello World'
  5. build() {
  6. Row() {
  7. Column() {
  8. Text(this.message)
  9. .fontSize(50)
  10. .fontWeight(FontWeight.Bold)
  11. .onClick(()=>{
  12. this.message = 'Hello ArkTS'
  13. })
  14. }
  15. .width('100%')
  16. .height('100%')
  17. .justifyContent(FlexAlign.Center)
  18. }
  19. }
  20. }

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

验证@State装饰器的作用

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

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

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

1.基本类型

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

2.对象类型

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

  1. class Person{
  2. name:string
  3. age:number
  4. constructor(name:string,age:number) {
  5. this.name = name
  6. this.age = age
  7. }
  8. }
  9. @Entry
  10. @Component
  11. struct StatePage {
  12. @State p:Person = new Person('zhangsan',21)
  13. build() {
  14. Row() {
  15. Column() {
  16. Text(`${this.p.name} : ${this.p.age}`)
  17. .fontSize(50)
  18. .fontWeight(FontWeight.Bold)
  19. .onClick(()=>{
  20. this.p.age++
  21. })
  22. }
  23. .width('100%')
  24. .height('100%')
  25. .justifyContent(FlexAlign.Center)
  26. }
  27. }
  28. }

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

3.嵌套类型

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

  1. class Person{
  2. name:string
  3. age:number
  4. gf: Person
  5. constructor(name:string,age:number,gf?: Person) {
  6. this.name = name
  7. this.age = age
  8. this.gf = gf
  9. }
  10. }
  11. @Entry
  12. @Component
  13. struct StatePage {
  14. @State p:Person = new Person('zhangsan',21,new Person('LiSi',19))
  15. build() {
  16. Row() {
  17. Column() {
  18. Text(`${this.p.name} : ${this.p.age}`)
  19. .fontSize(50)
  20. .fontWeight(FontWeight.Bold)
  21. .onClick(()=>{
  22. this.p.age++
  23. })
  24. Text(`${this.p.gf.name} : ${this.p.gf.age}`)
  25. .fontSize(50)
  26. .fontWeight(FontWeight.Bold)
  27. .onClick(()=>{
  28. this.p.gf.age++
  29. })
  30. }
  31. .width('100%')
  32. .height('100%')
  33. .justifyContent(FlexAlign.Center)
  34. }
  35. }
  36. }

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

4.数组

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

  1. class Person{
  2. name:string
  3. age:number
  4. gf: Person
  5. constructor(name:string,age:number,gf?: Person) {
  6. this.name = name
  7. this.age = age
  8. this.gf = gf
  9. }
  10. }
  11. @Entry
  12. @Component
  13. struct StatePage {
  14. idx: number = 1
  15. @State p:Person = new Person('zhangsan',21,new Person('LiSi',19))
  16. @State gfs: Person[] = [
  17. new Person('WangWu',18),
  18. new Person('LaoLiu',78)
  19. ]
  20. build() {
  21. Row() {
  22. Column() {
  23. Text(`${this.p.name} : ${this.p.age}`)
  24. .fontSize(50)
  25. .fontWeight(FontWeight.Bold)
  26. .onClick(()=>{
  27. this.p.age++
  28. })
  29. Button('添加女友')
  30. .onClick(() => {
  31. this.gfs.push(new Person(`女友` + this.idx++,20))
  32. })
  33. Text('=女友列表=')
  34. .fontSize(50)
  35. .fontWeight(FontWeight.Bold)
  36. ForEach(
  37. this.gfs,
  38. (item,index) => {
  39. Row(){
  40. Text(`${item.name} : ${item.age}`)
  41. .fontSize(30)
  42. .onClick(()=>{
  43. item.age++
  44. })
  45. Button("删除")
  46. .onClick(() => {
  47. this.gfs.splice(index,1)
  48. })
  49. }
  50. .width('100%')
  51. .justifyContent(FlexAlign.SpaceAround)
  52. }
  53. )
  54. }
  55. .width('100%')
  56. .height('100%')
  57. .justifyContent(FlexAlign.Center)
  58. }
  59. }
  60. }

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

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/790016
推荐阅读
相关标签
  

闽ICP备14008679号