赞
踩
在声明式UI中,是以状态驱动视图的更新:
@State装饰的变量,或称为状态变量,一旦变量拥有了状态属性,就和自定义组件的渲染绑定起来。当状态改变时,UI会发生对应的渲染改变。(就相当于是一个监听器,监听状态变量的变化,状态变量发生变化会重新渲染相应组件)
@State装饰的变量,与声明式范式中的其他被装饰变量一样,是私有的,只能从组件内部访问,在声明时必须指定其类型和本地初始化。初始化也可选择使用命名参数机制从父组件完成初始化。
@State装饰的变量拥有以下特点:
装饰简单类型的变量
以下示例为@State装饰的简单类型,count被@State装饰成为状态变量,count的改变引起Button组件的刷新:
- @Entry
- @Component
- struct MyComponent {
- @State count: number = 0;
-
- build() {
- Button(`click times: ${this.count}`)
- .onClick(() => {
- this.count += 1;
- })
- }
- }
装饰class对象类型的变量
自定义组件MyComponent定义了被@State装饰的状态变量count和title,其中title的类型为自定义类Model。如果count或title的值发生变化,则查询MyComponent中使用该状态变量的UI组件,并进行重新渲染。
EntryComponent中有多个MyComponent组件实例,第一个MyComponent内部状态的更改不会影响第二个MyComponent。
- class Model {
- public value: string;
-
- constructor(value: string) {
- this.value = value;
- }
- }
-
- @Entry
- @Component
- struct EntryComponent {
- build() {
- Column() {
- // 此处指定的参数都将在初始渲染时覆盖本地定义的默认值,并不是所有的参数都需要从父组件初始化
- MyComponent({ count: 1, increaseBy: 2 })
- MyComponent({ title: new Model('Hello, World 2'), count: 7 })
- }
- }
- }
-
- @Component
- struct MyComponent {
- @State title: Model = new Model('Hello World');
- @State count: number = 0;
- private increaseBy: number = 1;
-
- build() {
- Column() {
- Text(`${this.title.value}`)
- Button(`Click to change title`).onClick(() => {
- // @State变量的更新将触发上面的Text组件内容更新
- this.title.value = this.title.value === 'Hello ArkUI' ? 'Hello World' : 'Hello ArkUI';
- })
-
- Button(`Click to increase count=${this.count}`).onClick(() => {
- // @State变量的更新将触发该Button组件的内容更新
- this.count += this.increaseBy;
- })
- }
- }
- }

更多查看官方文档:@State装饰器:组件内状态-管理组件拥有的状态-状态管理-学习ArkTS语言-入门-HarmonyOS应用开发
@Prop装饰的变量可以和父组件建立单向的同步关系。@Prop装饰的变量是可变的,但是变化不会同步回其父组件。
- @Component
- struct Child {
- @Prop value: number;
-
- build() {
- Text(`${this.value}`)
- .fontSize(50)
- .onClick(()=>{this.value++})
- }
- }
-
- @Entry
- @Component
- struct Index {
- @State arr: number[] = [1,2,3];
-
- build() {
- Row() {
- Column() {
- Child({value: this.arr[0]})
- Child({value: this.arr[1]})
- Child({value: this.arr[2]})
-
- Divider().height(5)
-
- ForEach(this.arr,
- item => {
- Child({value: item})
- },
- item => item.toString()
- )
- Text('replace entire arr')
- .fontSize(50)
- .onClick(()=>{
- // 两个数组都包含项“3”。
- this.arr = this.arr[0] == 1 ? [3,4,5] : [1,2,3];
- })
- }
- }
- }
- }

更多查看官方文档:@Prop装饰器:父子单向同步-管理组件拥有的状态-状态管理-学习ArkTS语言-入门-HarmonyOS应用开发
子组件中被@Link装饰的变量与其父组件中对应的数据源建立双向数据绑定。
- @Component
- struct Child {
- @Link items: number[];
-
- build() {
- Column() {
- Button(`Button1: push`)
- .margin(12)
- .width(312)
- .height(40)
- .fontColor('#FFFFFF,90%')
- .onClick(() => {
- this.items.push(this.items.length + 1);
- })
- Button(`Button2: replace whole item`)
- .margin(12)
- .width(312)
- .height(40)
- .fontColor('#FFFFFF,90%')
- .onClick(() => {
- this.items = [100, 200, 300];
- })
- }
- }
- }
-
- @Entry
- @Component
- struct Parent {
- @State arr: number[] = [1, 2, 3];
-
- build() {
- Column() {
- Child({ items: $arr })
- .margin(12)
- ForEach(this.arr,
- (item: void) => {
- Button(`${item}`)
- .margin(12)
- .width(312)
- .height(40)
- .backgroundColor('#11a2a2a2')
- .fontColor('#e6000000')
- },
- (item: ForEachInterface) => item.toString()
- )
- }
- }
- }

更多查看官方文档: @Link装饰器:父子双向同步-管理组件拥有的状态-状态管理-学习ArkTS语言-入门-HarmonyOS应用开发
@Provide和@Consume摆脱参数传递机制的束缚,实现跨层级传递(双向绑定)。
其中@Provide装饰的变量是在祖先节点中,可以理解为被“提供”给后代的状态变量。@Consume装饰的变量是在后代组件中,去“消费(绑定)”祖先节点提供的变量。
- // 通过相同的变量名绑定
- @Provide a: number = 0;
- @Consume a: number;
-
- // 通过相同的变量别名绑定
- @Provide('a') b: number = 0;
- @Consume('a') c: number;
- @Component
- struct CompD {
- // @Consume装饰的变量通过相同的属性名绑定其祖先组件CompA内的@Provide装饰的变量
- @Consume reviewVotes: number;
-
- build() {
- Column() {
- Text(`reviewVotes(${this.reviewVotes})`)
- Button(`reviewVotes(${this.reviewVotes}), give +1`)
- .onClick(() => this.reviewVotes += 1)
- }
- .width('50%')
- }
- }
-
- @Component
- struct CompC {
- build() {
- Row({ space: 5 }) {
- CompD()
- CompD()
- }
- }
- }
-
- @Component
- struct CompB {
- build() {
- CompC()
- }
- }
-
- @Entry
- @Component
- struct CompA {
- // @Provide装饰的变量reviewVotes由入口组件CompA提供其后代组件
- @Provide reviewVotes: number = 0;
-
- build() {
- Column() {
- Button(`reviewVotes(${this.reviewVotes}), give +1`)
- .onClick(() => this.reviewVotes += 1)
- CompB()
- }
- }
- }

更多查看官方文档: @Provide装饰器和@Consume装饰器:与后代组件双向同步-管理组件拥有的状态-状态管理-学习ArkTS语言-入门-HarmonyOS应用开发
@ObjectLink和@Observed类装饰器用于在涉及嵌套对象或数组的场景中进行双向数据同步:
更多查看官方文档: @Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化-管理组件拥有的状态-状态管理-学习ArkTS语言-入门-HarmonyOS应用开发
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。