当前位置:   article > 正文

鸿蒙 - arkTs:状态管理

鸿蒙 - arkTs:状态管理

状态 @State:

在声明式UI中,以状态驱动视图更新

  1. 状态(State):指驱动视图更新的数据(被装饰器标记的变量)
  2. 视图(View):基于UI描述渲染得到的用户界面

使用示例:

  1. @Entry
  2. @Component
  3. struct Index {
  4. // 使用状态装饰器
  5. @State message: string = 'Hello Word'
  6. build() {
  7. Column(){
  8. Text(this.message)
  9. }
  10. };
  11. }

说明:

  • @State装饰器标记的变量初始化必须有值
  • @State支持Object、Class、string、number、boolean、enum类型以及这些类型的数组
  • 嵌套类型以及数组中的对象属性无法触发视图更新(相当于浅层监听)

子组件数据同步 @Prop和@Link:

对比:

@Prop@Link
同步类型单向同步双向同步
允许装饰的变量类型
  • @Prop只支持string、number、boolean、enum类型
  • 父组件对象类型,子组件hi对象类型
  • 不可以是数组,any
  • 父子类型一致:string、number、boolean、enum、object、class以及他们的数组
  • 数组中元素增、删、替换会引起刷新
  • 嵌套类型以及数组中的对象属性无法触发视图更新
初始化方式不允许子组件初始化父子间传递,不允许子组件初始化

@Prop使用示例:

PS:@Prop定义的数据在子组件不能初始化

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State msg: string = 'Hello Word'
  5. build() {
  6. Column() {
  7. MsgModule({msg:this.msg})
  8. Button('更改文案')
  9. .onClick(()=>{
  10. this.msg = 'Hello arkTs'
  11. })
  12. }
  13. }
  14. }
  15. @Component
  16. struct MsgModule {
  17. @Prop msg:string
  18. build(){
  19. Text(this.msg)
  20. .fontSize(30)
  21. .fontColor('red')
  22. }
  23. }

@Link使用示例:

PS:

  • @Link定义的数据在子组件不能初始化
  • @Link定义的数据,父组件在使用时候,去掉"this."且前边加"$"符号
  1. @Entry
  2. @Component
  3. struct Index {
  4. @State msg: string = 'Hello Word'
  5. build() {
  6. Column() {
  7. MsgModule({msg: $msg})
  8. }
  9. }
  10. }
  11. @Component
  12. struct MsgModule {
  13. @Link msg:string
  14. build(){
  15. Row(){
  16. Text(this.msg)
  17. .fontSize(30)
  18. .fontColor('red')
  19. Button('更改文案')
  20. .onClick(()=>{
  21. this.msg = 'Hello arkTs'
  22. })
  23. }
  24. }
  25. }

 @Provide和@Consume:(跨组件提供双向的数据同步)

  @Provide定义的数组,其他组件在使用时候直接使用@Consume定义使用,不需要在调用组件时候进行参数传递

使用示例:

  1. @Entry
  2. @Component
  3. struct Index {
  4. @Provide msg: string = 'Hello Word'
  5. build() {
  6. Column() {
  7. MsgBtnModule()
  8. }
  9. }
  10. }
  11. @Component
  12. struct MsgBtnModule {
  13. build(){
  14. Row(){
  15. MsgModule()
  16. }
  17. }
  18. }
  19. @Component
  20. struct MsgModule {
  21. @Consume msg: string
  22. build(){
  23. Row(){
  24. Text(this.msg)
  25. .fontSize(30)
  26. .fontColor('red')
  27. Button('更改文案')
  28. .onClick(()=>{
  29. this.msg = 'Hello arkTs'
  30. })
  31. }
  32. }
  33. }

@ObjectLink和@Observed:(涉及嵌套对象或数组元素为对象的场景中进行双向数据同步)

使用示例:

  1. @Observed
  2. class ArrInt {
  3. name: string = ""
  4. price: number = 0
  5. }
  6. @Entry
  7. @Component
  8. struct Index {
  9. @State num:number = 0
  10. @State arr: ArrInt[] = [
  11. {name: '华为 Meta 50',price: 7999},
  12. {name: '华为 Meta 60',price: 8999},
  13. {name: '华为 Meta 60 pro',price: 9999},
  14. ]
  15. build() {
  16. Column() {
  17. Text('涨价' + this.num.toString() + '次')
  18. .fontSize(50)
  19. .margin(20)
  20. ArrModule({num: $num, arr: $arr})
  21. }
  22. }
  23. }
  24. @Component
  25. struct ArrModule {
  26. @Link num: number
  27. @Link arr: ArrInt[]
  28. build(){
  29. Row(){
  30. List({space: 10}){
  31. ForEach(
  32. this.arr,
  33. (item: ArrInt) => {
  34. ListItem(){
  35. ArrItemModule({item:item, num: $num})
  36. }
  37. }
  38. )
  39. }
  40. }
  41. }
  42. }
  43. @Component
  44. struct ArrItemModule {
  45. @ObjectLink item: ArrInt
  46. @Link num: number
  47. build(){
  48. Column(){
  49. Text(this.item.name)
  50. .fontSize(30)
  51. .fontColor('red')
  52. Text(this.item.price.toString())
  53. .fontSize(30)
  54. .fontColor('#000')
  55. Button('涨价')
  56. .onClick(()=>{
  57. this.num += 1
  58. })
  59. }
  60. }
  61. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/333612
推荐阅读
相关标签
  

闽ICP备14008679号