当前位置:   article > 正文

状态管理-@State

@state

目录

概述

装饰器的使用规则

相关案例

@State装饰string、number、boolean

@State装饰enum

@State装饰class,Object及其嵌套

@State装饰数组,包含嵌套属性


概述

@State装饰的变量,可以称为状态变量,一旦变量拥有了状态属性,就和组件的渲染绑定起来。当状态改变时,UI会发生对应的渲染改变。

@State装饰的变量,与声明式范式中的其他被装饰变量一样,是私有的,只能从组件内部访问,在声明时必须指定其类型和本地初始化。初始化也可选择使用命名参数机制从父组件完成初始化。

声明式范式icon-default.png?t=N7T8https://developer.harmonyos.com/cn/docs/documentation/doc-guides-V3/arkts-ui-development-overview-0000001438467628-V3

@State装饰的变量拥有以下特点:

1、@State装饰的变量与子组件中的@Prop、@Link或@ObjectLink装饰变量之间建立单向或双向数据同步。

2、@State装饰的变量生命周期与其所属自定义组件的生命周期相同。

装饰器的使用规则

@State装饰器说明
同步类型不与父组件中任何类型的变量同步,即不提供父子组件之间数据同步
允许装饰的变量类型Object、class、string、number、boolean、enum以及这些类型的数组。进行嵌套时,被嵌套的属性变化不能实时更新。(比如对象里边嵌套了一个对象,被嵌套的对象属性发生改变时不会触发渲染更新,但实际上数据会随业务逻辑更新,当其他值的修改或其他组件触发页面渲染时会将更新后的数据重新渲染
被装饰变量的初始值必须本地初始化
是否支持组件外访问不支持,只能在组件内访问

以上原理内容部分引自鸿蒙开发文档-@State装饰器

相关案例

@State装饰string、number、boolean

示例代码及运行结果演示:

State装饰string、number、boolean代码运行demo

  1. @Entry
  2. @Component
  3. struct StatePage {
  4. @State msg: string = 'Hello World'
  5. @State num: number = 10
  6. @State isEven: boolean = true
  7. build() {
  8. Row() {
  9. Column({space: 10}) {
  10. Text(this.msg)
  11. .fontSize(50)
  12. .fontWeight(FontWeight.Bold)
  13. .onClick(() => {
  14. this.msg = 'Hello ArkTS!'
  15. })
  16. Text('num:' + this.num)
  17. .fontSize(50)
  18. .fontWeight(FontWeight.Bold)
  19. .onClick(() => {
  20. this.num += 1
  21. })
  22. Text('num是否是偶数:' + this.isEven)
  23. .fontSize(30)
  24. .fontWeight(FontWeight.Bold)
  25. .onClick(() => {
  26. if (this.num % 2 === 0) {
  27. this.isEven = true
  28. }else {
  29. this.isEven = false
  30. }
  31. })
  32. }
  33. .width('100%')
  34. }
  35. .height('100%')
  36. }
  37. }

@State装饰enum

示例运行demo

@State装饰enum类型

  1. enum AnimalName{
  2. Dog = 'DOG',
  3. Cat = 'CAT',
  4. Monkey = 'MONKEY',
  5. Lion = 'LION',
  6. Tiger = 'Tiger'
  7. }
  8. @Entry
  9. @Component
  10. struct StatePage {
  11. @State animal: AnimalName = AnimalName.Dog
  12. build() {
  13. Row() {
  14. Column({space: 10}) {
  15. Text('动物名字是:' + this.animal)
  16. .fontSize(50)
  17. .fontWeight(FontWeight.Bold)
  18. .onClick(() => {
  19. this.animal = AnimalName.Cat
  20. })
  21. }
  22. .width('100%')
  23. }
  24. .height('100%')
  25. }
  26. }

@State装饰class,Object及其嵌套

代码运行demo

@State装饰class,Object及其嵌套演示

  1. class Person{
  2. name: string
  3. age: number
  4. father: Person
  5. constructor(name: string, age: number, father?: Person) {
  6. this.name = name
  7. this.age = age
  8. this.father = father
  9. }
  10. }
  11. @Entry
  12. @Component
  13. struct StatePage {
  14. @State person: Person = new Person('王五', 20)
  15. @State person1: Person = new Person('张三', 20, new Person('李四', 43))
  16. @State person2: Person = new Person('Jack', 20, new Person('Tom', 43))
  17. build() {
  18. Row() {
  19. Column({space: 30}) {
  20. Text(this.person.name + ':' + this.person.age)
  21. .fontSize(25)
  22. .fontWeight(FontWeight.Bold)
  23. .onClick(() => {
  24. this.person.age += 1
  25. })
  26. //对象的嵌套渲染在一个组件中时,且属性值同时发生改变,也会触发嵌套对象的渲染
  27. //因为非嵌套对象的属性发生了变化触发了页面渲染,因此嵌套对象属性值的改变也会同时渲染出来
  28. Text(this.person1.name + ':' + this.person1.age + '\n' +
  29. 'my father is:' + this.person1.father.name + ':' + this.person1.father.age)
  30. .fontSize(25)
  31. .fontWeight(FontWeight.Bold)
  32. .onClick(() => {
  33. this.person1.age += 1
  34. this.person1.father.age += 1
  35. }).fontColor('#0000ff')
  36. .width('100%')
  37. Text(this.person2.name + ':' + this.person2.age)
  38. .fontSize(25)
  39. .fontWeight(FontWeight.Bold)
  40. .onClick(() => {
  41. this.person2.age += 1
  42. }).fontColor('#00ff00')
  43. Text(this.person2.father.name + ':' + this.person2.father.age)
  44. .fontSize(25)
  45. .fontWeight(FontWeight.Bold)
  46. .onClick(() => {
  47. this.person2.father.age += 1
  48. }).fontColor('#00ff00')
  49. }
  50. .width('100%')
  51. .padding(30)
  52. }
  53. .height('100%')
  54. }
  55. }

@State装饰数组,包含嵌套属性

当数组中是对象时,对象的属性变更不会触发页面渲染,对数组本身的更改,可以触发页面渲染,可以理解为不可穿透渲染。

State装饰数组

  1. class Person{
  2. rank: string
  3. name: string
  4. age: number
  5. father: Person
  6. constructor(rank: string, name: string, age: number, father?: Person) {
  7. this.rank = rank
  8. this.name = name
  9. this.age = age
  10. this.father = father
  11. }
  12. }
  13. @Entry
  14. @Component
  15. struct StatePage {
  16. empId: number = 1
  17. @State array: Array<number> = [0,1,2,3,4]
  18. @State person: Person = new Person('老板', '张三', 45)
  19. @State employee: Array<Person> = []
  20. build() {
  21. Row() {
  22. Column({space: 10}) {
  23. Text(this.array.toString())
  24. .fontSize(25)
  25. .fontWeight(FontWeight.Bold)
  26. .onClick(() => {
  27. this.array.push(8)
  28. })
  29. Text(`${this.person.rank}-${this.person.name}:${this.person.age}`)
  30. .fontSize(25)
  31. .fontWeight(FontWeight.Bold)
  32. .onClick(() => {
  33. this.person.age++
  34. })
  35. Button('添加员工')
  36. .onClick(() => {
  37. this.employee.push(new Person('员工', '员工' + this.empId++, 20))
  38. })
  39. ForEach(
  40. this.employee,
  41. (employee: Person, index) =>{
  42. Row({space: 10}){
  43. Text(`${employee.rank}-${employee.name}:${employee.age}`)
  44. .fontSize(25)
  45. .fontColor('#00ff00')
  46. .onClick(() =>{
  47. //不会触发页面渲染,因此点击不会发生改变,但实际属性值已经变更
  48. // employee.age++
  49. //如果重新给该员工赋值,也就是改变改索引处的值,则会触发页面渲染,更新页面
  50. this.employee[index] = new Person('经理', employee.name, employee.age+1)
  51. })
  52. Button('删除')
  53. .onClick(() =>{
  54. this.employee.splice(index)
  55. })
  56. }
  57. }
  58. )
  59. }
  60. .width('100%')
  61. .padding(30)
  62. }
  63. .height('100%')
  64. }
  65. }

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

闽ICP备14008679号