ts-other-states-observed-objectlink.md 5.3 KB
Newer Older
Z
zengyawen 已提交
1
# Observed和ObjectLink数据管理
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3 4 5
本章将引入两个新的装饰符@Observed和@ObjectLink:


6
- @Observed应用于类,表示该类中的数据变更被UI页面管理,例如:@Observed class ClassA {}。
Z
zengyawen 已提交
7

8
- @ObjectLink应用于被@Observed所装饰类的对象,例如:@ObjectLink a: ClassA。
Z
zengyawen 已提交
9

Z
zengyawen 已提交
10

Z
zengyawen 已提交
11 12 13
## 引入动机

当开发者需要在子组件中针对父组件的一个变量(parent_a)设置双向同步时,开发者可以在父组件中使用\@State装饰变量(parent_a),并在子组件中使用@Link装饰相应的变量(child_a)。这样的话,不仅可以实现父组件与单个子组件之间的数据同步,也可以实现父组件与多个子组件之间的数据同步。如下图所示,可以看到,父子组件针对ClassA类型的变量设置了双向同步,那么当子组件1中变量的属性c的值变化时,会通知父组件同步变化,而当父组件中属性c的值变化时,会通知所有子组件同步变化。
Z
zengyawen 已提交
14 15 16 17 18 19 20 21

![](figures/zh-cn_image_0000001251090821.png)

然而,上述例子是针对某个数据对象进行的整体同步,而当开发者只想针对父组件中某个数据对象的部分信息进行同步时,使用@Link就不能满足要求。如果这些部分信息是一个类对象,就可以使用@ObjectLink配合@Observed来实现,如下图所示。

![](figures/zh-cn_image_0000001206450834.png)


Z
zengyawen 已提交
22 23 24
## 设置要求

- @Observed 用于类,@ObjectLink 用于变量。
Z
zengyawen 已提交
25

Z
zengyawen 已提交
26 27 28
- @ObjectLink装饰的变量类型必须为类(class type)。
  - 类要被\@Observed装饰器所装饰。
  - 不支持简单类型参数,可以使用@Prop进行单向同步。
Z
zengyawen 已提交
29

Z
zengyawen 已提交
30 31
- @ObjectLink装饰的变量是不可变的(immutable)。
  - 属性的改动是被允许的,当改动发生时,如果同一个对象被多个@ObjectLink变量所引用,那么所有拥有这些变量的自定义组件都会被通知去重新渲染。
Z
zengyawen 已提交
32

Z
zengyawen 已提交
33 34
- @ObjectLink装饰的变量不可设置默认值。
  - 必须让父组件中有一个由@State、@Link、@StorageLink、@Provide或@Consume所装饰变量参与的TS表达式进行初始化。
Z
zengyawen 已提交
35

Z
zengyawen 已提交
36
- @ObjectLink装饰的变量是私有变量,只能在组件内访问。
Z
zengyawen 已提交
37

Z
zengyawen 已提交
38 39 40 41 42

## 示例


### 案例1
Z
zengyawen 已提交
43

Z
zengyawen 已提交
44
```
T
tianyu 已提交
45
//父组件ViewB中的类对象ClassB,其包含的对象ClassA与子组件ViewA数据同步时,通过ObjectLink将数据c值的变化状态通知给父组件同步变化。
Z
zengyawen 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
@Observed
class ClassA {
  public name : string;
  public c: number;
  constructor(c: number, name: string = 'OK') {
    this.name = name;
    this.c = c;
  }
}

class ClassB {
  public a: ClassA;
  constructor(a: ClassA) {
    this.a = a;
  }
}

@Component
struct ViewA {
  label : string = "ep1";
  @ObjectLink a : ClassA;
  build() {
    Column() {
      Text(`ViewA [${this.label}]: a.c=${this.a.c}`)
        .fontSize(20)
      Button(`+1`)
        .width(100)
        .margin(2)
        .onClick(() => {
          this.a.c += 1;
        })
      Button(`reset`)
        .width(100)
        .margin(2)
        .onClick(() => {
T
tianyu 已提交
81
          this.a = new ClassA(0); // 错误:ObjectLink装饰的变量a是不可变的
Z
zengyawen 已提交
82
        })
Z
zengyawen 已提交
83
    }
Z
zengyawen 已提交
84
  }
Z
zengyawen 已提交
85 86
}

Z
zengyawen 已提交
87 88 89 90 91 92 93 94
@Entry
@Component
struct ViewB {
  @State b : ClassB = new ClassB(new ClassA(10));
  build() {
    Flex({direction: FlexDirection.Column, alignItems: ItemAlign.Center}) {
      ViewA({label: "ViewA #1", a: this.b.a})
      ViewA({label: "ViewA #2", a: this.b.a})
Z
zengyawen 已提交
95

Z
zengyawen 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
      Button(`ViewB: this.b.a.c += 1` )
        .width(320)
        .margin(4)
        .onClick(() => {
          this.b.a.c += 1;
        })
      Button(`ViewB: this.b.a = new ClassA(0)`)
        .width(240)
        .margin(4)
        .onClick(() => {
          this.b.a = new ClassA(0);
        })
      Button(`ViewB: this.b = new ClassB(ClassA(0))`)
        .width(240)
        .margin(4)
        .onClick(() => {
          this.b = new ClassB(new ClassA(0));
        })
Z
zengyawen 已提交
114
    }
Z
zengyawen 已提交
115
  }
Z
zengyawen 已提交
116 117 118
}
```

Z
zengyawen 已提交
119 120

### 案例2
Z
zengyawen 已提交
121

Z
zengyawen 已提交
122
```
T
tianyu 已提交
123
//父组件ViewB中的类对象ClassA与子组件ViewA保持数据同步时,可以使用@ObjectLink和@Observed,绑定该数据对象的父组件和其他子组件同步更新
Z
zengyawen 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136
var nextID: number = 0;
@Observed
class ClassA {
  public name : string;
  public c: number;
  public id : number;
  constructor(c: number, name: string = 'OK') {
    this.name = name;
    this.c = c;
    this.id = nextID++;
  }
}

Z
zengyawen 已提交
137 138 139
@Component
struct ViewA {
  label : string = "ViewA1";
Z
zengyawen 已提交
140
  @ObjectLink a: ClassA;
Z
zengyawen 已提交
141
  build() {
Z
zengyawen 已提交
142 143
    Row() {
      Button(`ViewA [${this.label}] this.a.c= ${this.a.c} +1`)
Z
zengyawen 已提交
144
        .onClick(() => {
Z
zengyawen 已提交
145
          this.a.c += 1;
Z
zengyawen 已提交
146
        })
T
tianyu 已提交
147
    }.margin({ top: 10 })
Z
zengyawen 已提交
148 149 150 151
  }
}

@Entry
Z
zengyawen 已提交
152
@Component
Z
zengyawen 已提交
153
struct ViewB {
Z
zengyawen 已提交
154
  @State arrA : ClassA[] = [ new ClassA(0), new ClassA(0) ];
Z
zengyawen 已提交
155
  build() {
Z
zengyawen 已提交
156 157 158 159 160 161 162 163
    Column() {
      ForEach (this.arrA, (item) => {
          ViewA({label: `#${item.id}`, a: item})
        },
        (item) => item.id.toString()
      )
      ViewA({label: `ViewA this.arrA[first]`, a: this.arrA[0]})
      ViewA({label: `ViewA this.arrA[last]`, a: this.arrA[this.arrA.length-1]})
Z
zengyawen 已提交
164

Z
zengyawen 已提交
165
      Button(`ViewB: reset array`)
T
tianyu 已提交
166
        .margin({ top: 10 })
Z
zengyawen 已提交
167
        .onClick(() => {
Z
zengyawen 已提交
168 169 170
            this.arrA = [ new ClassA(0), new ClassA(0) ];
        })
      Button(`ViewB: push`)
T
tianyu 已提交
171
      	.margin({ top: 10 })
Z
zengyawen 已提交
172 173 174 175
        .onClick(() => {
            this.arrA.push(new ClassA(0))
        })
      Button(`ViewB: shift`)
T
tianyu 已提交
176
      	.margin({ top: 10 })
Z
zengyawen 已提交
177
        .onClick(() => {
Z
zengyawen 已提交
178 179
            this.arrA.shift()
        })
Z
zengyawen 已提交
180
    }
Z
zengyawen 已提交
181
  }
Z
zengyawen 已提交
182 183
}
```