鸿蒙状态管理中V1和V2的区别(3)(HarmonyOS API14版本)

上篇文章讲了@Component,@ComponentV2以及@State和@Local的区别。这篇文章就继续讲常用的几个装饰器在V1和V2中的区别。

管理组件拥有状态的装饰器:组件级别的状态管理,可以观察组件内变化,和不同组件层级的变化,但需要唯一观察同一个组件树上,即同一个页面内。

父子传值

V1:@Prop 装饰的变量可以和父组件建立单向的同步关系。装饰的变量是可变的,但是变化不会同步回其父组件。注意:

1.修改父组件数据,会同步更新子组件

2.修改子组件@Prop 修饰的数据,子组件 UI 更新,更新后的数据不会同步给父组件

3.通过回调函数的方式修改父组件的数据,然后触发@Prop数据的更新

V1:@Link 可以实现父组件和子组件的双向同步,当其中一方改变时,另外一方能够感知到变化。

scss 复制代码
@Component
struct DateComponent {
  @Link selectedDate: Date;
 
  build() {
    Column() {
      Button(`child increase the year by 1`)
      .onClick(() => {
        this.selectedDate.setFullYear(this.selectedDate.getFullYear() + 1);
      })
      Button('child update the new date')
        .margin(10)
        .onClick(() => {
          this.selectedDate = new Date('2023-09-09');
        })
      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2100-1-1'),
        selected: this.selectedDate
      })
    }
 
  }
}
 
@Entry
@Component
struct ParentComponent {
  @State parentSelectedDate: Date = new Date('2021-08-08');
 
  build() {
    Column() {
      Button('parent increase the month by 1')
        .margin(10)
        .onClick(() => {
          this.parentSelectedDate.setMonth(this.parentSelectedDate.getMonth() + 1);
        })
      Button('parent update the new date')
        .margin(10)
        .onClick(() => {
          this.parentSelectedDate = new Date('2023-07-07');
        })
      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2100-1-1'),
        selected: this.parentSelectedDate
      })
 
      DateComponent({ selectedDate:this.parentSelectedDate })
    }
  }
}

V2:@Param表示组件从外部传入的状态,使得父子组件之间的数据能够进行同步。

less 复制代码
@Entry
@ComponentV2
struct Index {
  @Local count: number = 200
  @Local person: Person = new Person('李四', new iToy('奔驰'))
 
  build() {
    Column() {
      Button('修改count值').onClick(() => {
        this.count++
      })
 
      Text(this.person.name)
      Text(this.person.toy.name)
 
      ChildCom({ 
        count: this.count,
      person: this.person
      })
    }
    .height('100%')
    .width('100%')
  }
}
 
@ObservedV2
class Person {
  @Trace name: string
  @Trace toy: iToy
 
  constructor(name: string, toy: iToy) {
    this.name = name
    this.toy = toy
  }
}
 
@ObservedV2
class iToy {
  @Trace name: string;
 
  constructor(name: string) {
    this.name = name
  }
}
 
@ComponentV2
struct ChildCom {
  @Param count: number = 100
  @Param person: Person = new Person('靓仔', new iToy('玩具'))
 
  build() {
    Column() {
      Text(this.count.toString())
      Text(this.person.name)
      Text(this.person.toy.name)
 
 
      Button('修改person.name变量')
        .onClick(() => {
          // 不会更新UI
          this.person.name = '靓仔3'
          // this.person.toy = { name: '玩具3' }
          this.person.toy.name = '玩具3'
        })
 
    }
    .height('100%')
    .width('100%')
  }
}

注意:

1.@Param不仅可以接受组件外部输入,还可以接受@Local的同步变化,@Param装饰的变量变化时,会刷新该变量关联的组件。

2.@Param装饰的变量支持本地初始化,但是不允许在组件内部直接修改变量本身。

3.@Param装饰的变量在子组件中无法进行修改。但当装饰的变量类型为对象时,在子组件中修改对象中属性是允许的。

4.对于复杂类型如类对象,@Param会接受数据源的引用。在组件内可以修改类对象中的属性,该修改会同步到数据源。

V2:@Once装饰器仅在变量初始化时接受外部传入值进行初始化,当后续数据源更改时,不会将修改同步给子组件

less 复制代码
@Entry
@ComponentV2
struct Index {
  @Local count: number = 200
 
  build() {
    Column() {
      Button('修改count值').onClick(() => {
        // 由于ChildCom中的count使用了@Once修饰,因此修改变量值,不会引起ChildCom组件中的count改变
        this.count++
      })
      Text(this.count.toString())
 
      ChildCom({
        count: this.count
      })
    }
    .height('100%')
    .width('100%')
  }
}
 
 
@ComponentV2
struct ChildCom {
  @Param @Once count: number = 100
 
  build() {
    Column() {
      Text(this.count.toString())
        .onClick(()=>{
          // 由于使用了@Once修饰 ,可以本地修改 @Param变量的值
          this.count++
        })
    }
    .height('100%')
    .width('100%')
  }
}

注意:

1.@Once必须搭配@Param使用,单独使用或搭配其他装饰器使用都是不允许的。

2.@Once与@Param搭配使用时,可以在本地修改@Param变量的值,但不会影响到父组件的状态变量(单独@Param修饰的变量是不允许在本地修改变量值的)

相关推荐
木斯佳10 小时前
HarmonyOS 6 三方SDK对接:从半接模式看Share Kit原理——系统分享的运行机制与设计理念
设计模式·harmonyos·架构设计·分享·半接模式
被温水煮的青蛙11 小时前
HarmonyOS openCustomDialog 实战:从入门到理解原理
harmonyos
高一学习c++会秃头吗11 小时前
鸿蒙适应式布局和响应式布局零基础
harmonyos
HwJack2012 小时前
HarmonyOS应用开发中EmbeddedUIExtensionAbility:跨进程 UI 嵌入的“幕后导演“
ui·华为·harmonyos
早點睡39014 小时前
ReactNative项目鸿蒙化三方库集成实战:react-native-calendars(日历展开和日程模块存在兼容性问题)
react native·react.js·harmonyos
云和数据.ChenGuang17 小时前
鸿蒙 + ChromaDB:端侧向量检索,打造全场景智能应用新范式
华为·harmonyos·鸿蒙
前端不太难18 小时前
AI + 鸿蒙游戏,会不会是下一个爆点?
人工智能·游戏·harmonyos
Gorit19 小时前
如何使用 Flutter 开发 HarmonyOS 应用
flutter·华为·harmonyos
键盘鼓手苏苏1 天前
Flutter 三方库 p2plib 的鸿蒙化适配指南 - 实现高性能的端到端(P2P)加密通讯、支持分布式节点发现与去中心化数据流传输实战
flutter·harmonyos·鸿蒙·openharmony
加农炮手Jinx1 天前
Flutter for OpenHarmony:postgrest 直接访问 PostgreSQL 数据库的 RESTful 客户端(Supabase 核心驱动) 深度解析与鸿蒙适配指南
数据库·flutter·华为·postgresql·restful·harmonyos·鸿蒙