HarmonyOS ArkTS 学习-状态管理-@Prop
使用场景
@Prop装饰的变量可与父组件进行单项同步。父组件中@State装饰的变量会影响子组件中@Prop装饰的变量,但是子组件中@Prop装饰的变量不会影响父组件中@State装饰的变量
限制条件
@Prop装饰器不能在@Entry装饰的自定义组件中使用。
父组件同步子组件简单数据类型
// 使用@Prop和@State 单向父同步子组件
// 同步简单类型
// 参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/arkts-prop-0000001473537702-V2#section614118685518
@Entry
@Component
struct ParentComponent {
@State numberValue: number = 1;
add() {
this.numberValue += 1
}
sub() {
this.numberValue -= 1
}
build() {
Column() {
Text(`Parent value:${
this.numberValue}`)
.fontSize(50)
Button('add 1')
.onClick(this.add.bind(this))
.type(ButtonType.Capsule)
.backgroundColor('#ff436ec3')
Button('sub 1')
.onClick(this.sub.bind(this))
.type(ButtonType.Capsule)
.backgroundColor('#ff436ec3')
Child