@buaa_smat/hometrans 0.1.13 → 0.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +164 -112
- package/agents/build-fixer.md +384 -394
- package/agents/code-reviewer.md +240 -240
- package/agents/logic-coder.md +199 -199
- package/agents/logic-context-builder.md +194 -194
- package/agents/review-fixer.md +405 -405
- package/agents/self-test-fixer.md +296 -296
- package/agents/self-tester.md +393 -392
- package/agents/spec-generator.md +540 -540
- package/dist/cli/config-store.js +84 -8
- package/dist/cli/config.js +3 -3
- package/dist/cli/env-vars.js +129 -0
- package/dist/cli/init.js +272 -272
- package/dist/cli/uninstall.js +152 -17
- package/dist/context/index.js +10 -197
- package/env-requirements.json +3 -3
- package/package.json +1 -1
- package/resource/choose_editor.png +0 -0
- package/resource/common_config.png +0 -0
- package/resource/integration_test_config.png +0 -0
- package/resource/set_env.png +0 -0
- package/resource/ui_align_config.png +0 -0
- package/skills/hmos-batch-ui-align/SKILL.md +108 -98
- package/skills/hmos-batch-ui-align/references/conversion-procedure.md +180 -180
- package/skills/hmos-batch-ui-align/references/mappings/android-to-harmonyOS-ui-atomic-component-mapping-reference.md +2533 -2533
- package/skills/hmos-batch-ui-align/references/mappings/android-to-harmonyOS-ui-interaction-mapping-reference.md +555 -555
- package/skills/hmos-batch-ui-align/references/mappings/android-to-harmonyOS-ui-layout-mapping-reference.md +117 -117
- package/skills/hmos-batch-ui-align/references/mvvm/@Link/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/217/214/345/220/221/345/220/214/346/255/245.md +648 -648
- package/skills/hmos-batch-ui-align/references/mvvm/@Observed/350/243/205/351/245/260/345/231/250/345/222/214@ObjectLink/350/243/205/351/245/260/345/231/250/357/274/232/345/265/214/345/245/227/347/261/273/345/257/271/350/261/241/345/261/236/346/200/247/345/217/230/345/214/226.md +2088 -2088
- package/skills/hmos-batch-ui-align/references/mvvm/@Prop/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/215/225/345/220/221/345/220/214/346/255/245.md +1033 -1033
- package/skills/hmos-batch-ui-align/references/mvvm/@Provide/350/243/205/351/245/260/345/231/250/345/222/214@Consume/350/243/205/351/245/260/345/231/250/357/274/232/344/270/216/345/220/216/344/273/243/347/273/204/344/273/266/345/217/214/345/220/221/345/220/214/346/255/245.md +1183 -1183
- package/skills/hmos-batch-ui-align/references/mvvm/@State/350/243/205/351/245/260/345/231/250/357/274/232/347/273/204/344/273/266/345/206/205/347/212/266/346/200/201.md +576 -576
- package/skills/hmos-batch-ui-align/references/mvvm/@Track/350/243/205/351/245/260/345/231/250/357/274/232class/345/257/271/350/261/241/345/261/236/346/200/247/347/272/247/346/233/264/346/226/260.md +297 -297
- package/skills/hmos-batch-ui-align/references/mvvm/@Watch/350/243/205/351/245/260/345/231/250/357/274/232/347/212/266/346/200/201/345/217/230/351/207/217/346/233/264/346/224/271/351/200/232/347/237/245.md +395 -395
- package/skills/hmos-batch-ui-align/references/mvvm/AppStorage/357/274/232/345/272/224/347/224/250/345/205/250/345/261/200/347/232/204UI/347/212/266/346/200/201/345/255/230/345/202/250.md +902 -902
- package/skills/hmos-batch-ui-align/references/mvvm/Environment/357/274/232/350/256/276/345/244/207/347/216/257/345/242/203/346/237/245/350/257/242.md +106 -106
- package/skills/hmos-batch-ui-align/references/mvvm/LocalStorage/357/274/232/351/241/265/351/235/242/347/272/247UI/347/212/266/346/200/201/345/255/230/345/202/250.md +1178 -1178
- package/skills/hmos-batch-ui-align/references/mvvm/MVVM/346/250/241/345/274/217/357/274/210V1/357/274/211.md +911 -911
- package/skills/hmos-batch-ui-align/references/mvvm/PersistentStorage/357/274/232/346/214/201/344/271/205/345/214/226/345/255/230/345/202/250UI/347/212/266/346/200/201.md +354 -354
- package/skills/hmos-batch-ui-align/references/mvvm//347/256/241/347/220/206/345/272/224/347/224/250/346/213/245/346/234/211/347/232/204/347/212/266/346/200/201/346/246/202/350/277/260.md +11 -11
- package/skills/hmos-convert-pipeline/SKILL.md +429 -415
- package/skills/hmos-fix-build-errors/SKILL.md +272 -273
- package/skills/hmos-fix-build-errors/references/arkts-strict-patterns.md +219 -219
- package/skills/hmos-fix-build-errors/references/known-patterns.md +157 -157
- package/skills/hmos-fix-build-errors/references/rdb-entity-pattern.md +131 -131
- package/skills/hmos-incremental-ui-align/SKILL.md +219 -200
- package/skills/hmos-incremental-ui-align/diff_analysis.md +52 -52
- package/skills/hmos-incremental-ui-align/page_align.md +62 -62
- package/skills/hmos-incremental-ui-align/readme.md +237 -230
- package/skills/hmos-incremental-ui-align/references/Comparison_Template.md +2 -2
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Link/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/217/214/345/220/221/345/220/214/346/255/245.md +648 -648
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Observed/350/243/205/351/245/260/345/231/250/345/222/214@ObjectLink/350/243/205/351/245/260/345/231/250/357/274/232/345/265/214/345/245/227/347/261/273/345/257/271/350/261/241/345/261/236/346/200/247/345/217/230/345/214/226.md +2088 -2088
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Prop/350/243/205/351/245/260/345/231/250/357/274/232/347/210/266/345/255/220/345/215/225/345/220/221/345/220/214/346/255/245.md +1033 -1033
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Provide/350/243/205/351/245/260/345/231/250/345/222/214@Consume/350/243/205/351/245/260/345/231/250/357/274/232/344/270/216/345/220/216/344/273/243/347/273/204/344/273/266/345/217/214/345/220/221/345/220/214/346/255/245.md +1183 -1183
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@State/350/243/205/351/245/260/345/231/250/357/274/232/347/273/204/344/273/266/345/206/205/347/212/266/346/200/201.md +576 -576
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Track/350/243/205/351/245/260/345/231/250/357/274/232class/345/257/271/350/261/241/345/261/236/346/200/247/347/272/247/346/233/264/346/226/260.md +297 -297
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/@Watch/350/243/205/351/245/260/345/231/250/357/274/232/347/212/266/346/200/201/345/217/230/351/207/217/346/233/264/346/224/271/351/200/232/347/237/245.md +395 -395
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/AppStorage/357/274/232/345/272/224/347/224/250/345/205/250/345/261/200/347/232/204UI/347/212/266/346/200/201/345/255/230/345/202/250.md +902 -902
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/Environment/357/274/232/350/256/276/345/244/207/347/216/257/345/242/203/346/237/245/350/257/242.md +106 -106
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/LocalStorage/357/274/232/351/241/265/351/235/242/347/272/247UI/347/212/266/346/200/201/345/255/230/345/202/250.md +1178 -1178
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/MVVM/346/250/241/345/274/217V1.md +911 -911
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243/PersistentStorage/357/274/232/346/214/201/344/271/205/345/214/226/345/255/230/345/202/250UI/347/212/266/346/200/201.md +354 -354
- package/skills/hmos-incremental-ui-align/references/MVVM/345/274/200/345/217/221/346/226/207/346/241/243//347/256/241/347/220/206/345/272/224/347/224/250/346/213/245/346/234/211/347/232/204/347/212/266/346/200/201/346/246/202/350/277/260.md +11 -11
- package/skills/hmos-incremental-ui-align/references/UI_Analysis_Template.md +3 -3
- package/skills/hmos-incremental-ui-align/references/android-to-harmonyOS-ui-atomic-component-mapping-reference.md +2533 -2533
- package/skills/hmos-incremental-ui-align/references/android-to-harmonyOS-ui-interaction-mapping-reference.md +555 -555
- package/skills/hmos-incremental-ui-align/references/android-to-harmonyOS-ui-layout-mapping-reference.md +117 -117
- package/skills/hmos-incremental-ui-align/scripts/navigation-capure.md +37 -37
- package/skills/hmos-integration-test/SKILL.md +380 -369
- package/skills/hmos-integration-test/readme.md +309 -309
- package/skills/hmos-resources-convert/SKILL.md +623 -623
- package/skills/hmos-resources-convert/references/conversion-rules.md +663 -663
- package/skills/hmos-resources-convert/references/dependency-analysis-rules.md +388 -388
- package/skills/hmos-resources-convert/references/resource-mapping-rules.md +457 -457
- package/skills/hmos-resources-convert/references/xml-drawable-to-svg-rules.md +513 -513
- package/skills/hmos-spec-generate/SKILL.md +331 -331
- package/skills/hmos-spec-generate/references/android-platform-tokens.md +105 -105
- package/skills/hmos-spec-generate/references/spec-sample-1.md +78 -78
- package/skills/hmos-spec-generate/references/spec-sample-2.md +58 -58
- package/skills/hmos-spec-generate/references/spec-sample-3.md +116 -116
- package/skills/hmos-spec-generate/references/step4-report-template.md +33 -33
- package/tools/test-tools/autotest/README.md +33 -17
- package/tools/test-tools/autotest/self_test_runner.py +109 -15
- package/resource/hometrans_config.png +0 -0
- package/skills/hmos-incremental-ui-align/config-example.json +0 -11
- package/tools/test-tools/autotest/config.yaml.example +0 -58
|
@@ -1,297 +1,297 @@
|
|
|
1
|
-
# @Track装饰器:class对象属性级更新
|
|
2
|
-
|
|
3
|
-
@Track应用于class对象的属性级更新。@Track装饰的属性变化时,只会触发该属性关联的UI更新。
|
|
4
|
-
|
|
5
|
-
在阅读本文档之前,建议开发者对状态管理基本观察能力有基本的了解。建议提前阅读:[@State](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-state)。
|
|
6
|
-
|
|
7
|
-
> **说明:**
|
|
8
|
-
>
|
|
9
|
-
> 从API version 11开始,该装饰器支持在ArkTS卡片中使用。
|
|
10
|
-
>
|
|
11
|
-
> 从API version 12开始,该装饰器支持在元服务中使用。
|
|
12
|
-
|
|
13
|
-
## 概述
|
|
14
|
-
|
|
15
|
-
@Track是class对象的属性装饰器。当一个class对象是状态变量时,@Track装饰的属性发生变化,只会触发该属性关联的UI更新;如果class类中使用了@Track装饰器,则未被@Track装饰器装饰的属性不能在UI中使用,如果使用,会发生运行时报错。
|
|
16
|
-
|
|
17
|
-
## class属性级更新说明
|
|
18
|
-
|
|
19
|
-
状态管理V1中@State等装饰器默认支持观察第一层属性的变化,第一层属性的变化虽然可以触发更新,但无法做到类属性级的观察,下面的例子就展示了这一限制:
|
|
20
|
-
|
|
21
|
-
```TypeScript
|
|
22
|
-
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
23
|
-
const DOMAIN_NUMBER: number = 0XFF00;
|
|
24
|
-
const TAG: string = '[Sample_StateTrack]';
|
|
25
|
-
class Info {
|
|
26
|
-
public name: string = 'Jack';
|
|
27
|
-
public age: number = 12;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@Entry
|
|
31
|
-
@Component
|
|
32
|
-
struct Index {
|
|
33
|
-
@State info: Info = new Info();
|
|
34
|
-
|
|
35
|
-
// 借助getFontSize的日志打印,可以分辨哪个组件触发了渲染
|
|
36
|
-
getFontSize(id: number): number {
|
|
37
|
-
hilog.info(DOMAIN_NUMBER, TAG, `Component ${id} render`);
|
|
38
|
-
return 30;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
build() {
|
|
42
|
-
Column() {
|
|
43
|
-
Text(`name: ${this.info.name}`)
|
|
44
|
-
.fontSize(this.getFontSize(1))
|
|
45
|
-
Text(`age: ${this.info.age}`)
|
|
46
|
-
.fontSize(this.getFontSize(2))
|
|
47
|
-
|
|
48
|
-
// 点击当前Button,可以发现当前虽然仅改变了name属性
|
|
49
|
-
// 但是依旧会触发两个Text的刷新
|
|
50
|
-
// Text(`age: ${this.info.age}`)是冗余刷新
|
|
51
|
-
Button('change name').onClick(() => {
|
|
52
|
-
this.info.name = 'Jane';
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
// 点击当前Button,可以发现当前虽然仅改变了age属性
|
|
56
|
-
// 但是依旧会触发两个Text的刷新
|
|
57
|
-
// Text(`name: ${this.info.name}`)是冗余刷新
|
|
58
|
-
Button('change age').onClick(() => {
|
|
59
|
-
this.info.age++;
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
.height('100%')
|
|
63
|
-
.width('100%')
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
> **说明:**
|
|
69
|
-
>
|
|
70
|
-
> 当UI刷新时,会执行组件的属性设置方法,利用这一机制可以通过观察getFontSize方法是否被调用来判断当前组件是否刷新。
|
|
71
|
-
|
|
72
|
-
- UI首次渲染完成,观察到输出如下日志:
|
|
73
|
-
|
|
74
|
-
```text
|
|
75
|
-
Component 1 render
|
|
76
|
-
Component 2 render
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
- 当点击Button('change name')时,即使只修改了info.name,观察日志发现两个Text组件仍会重新渲染。组件Text(`age: ${this.info.age}`)并未使用name属性,但仍因为info.name的改变而刷新,因此这次刷新是冗余的。日志输出如下:
|
|
80
|
-
|
|
81
|
-
```text
|
|
82
|
-
Component 1 render
|
|
83
|
-
Component 2 render
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
- 同理,点击Button('change age'),也会触发Text(`name: ${this.info.name}`)的刷新。日志输出如下:
|
|
87
|
-
|
|
88
|
-
```text
|
|
89
|
-
Component 1 render
|
|
90
|
-
Component 2 render
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
造成上述冗余刷新的根本原因是:状态管理V1中@State等装饰器无法精准观察类属性的访问与变更。为了实现类对象属性的精准观察,引入@Track装饰器。
|
|
94
|
-
|
|
95
|
-
## 装饰器说明
|
|
96
|
-
|
|
97
|
-
| @Track变量装饰器 | 说明 |
|
|
98
|
-
| --- | --- |
|
|
99
|
-
| 装饰器参数 | 无 |
|
|
100
|
-
| 可装饰的变量 | class对象的非静态成员属性。 |
|
|
101
|
-
|
|
102
|
-
## 观察变化和行为表现
|
|
103
|
-
|
|
104
|
-
当一个class对象是状态变量时,@Track装饰的属性发生变化,该属性关联的UI触发更新。
|
|
105
|
-
|
|
106
|
-
> **说明:**
|
|
107
|
-
>
|
|
108
|
-
> 当class对象中没有一个属性被标记@Track,行为与原先保持不变。@Track没有深度观测的功能。
|
|
109
|
-
|
|
110
|
-
使用@Track装饰器可以避免冗余刷新。
|
|
111
|
-
|
|
112
|
-
```TypeScript
|
|
113
|
-
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
114
|
-
const DOMAIN_NUMBER: number = 0XFF00;
|
|
115
|
-
const TAG: string = '[Sample_StateTrack]';
|
|
116
|
-
|
|
117
|
-
class LogTrack {
|
|
118
|
-
@Track public str1: string;
|
|
119
|
-
@Track public str2: string;
|
|
120
|
-
|
|
121
|
-
constructor(str1: string) {
|
|
122
|
-
this.str1 = str1;
|
|
123
|
-
this.str2 = 'World';
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
class LogNotTrack {
|
|
128
|
-
public str1: string;
|
|
129
|
-
public str2: string;
|
|
130
|
-
|
|
131
|
-
constructor(str1: string) {
|
|
132
|
-
this.str1 = str1;
|
|
133
|
-
this.str2 = 'World';
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
@Entry
|
|
138
|
-
@Component
|
|
139
|
-
struct AddLog {
|
|
140
|
-
@State logTrack: LogTrack = new LogTrack('Hello');
|
|
141
|
-
@State logNotTrack: LogNotTrack = new LogNotTrack('Hello');
|
|
142
|
-
|
|
143
|
-
isRender(index: number) {
|
|
144
|
-
hilog.info(DOMAIN_NUMBER, TAG, `Text ${index} is rendered`);
|
|
145
|
-
return 50;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
build() {
|
|
149
|
-
Row() {
|
|
150
|
-
Column() {
|
|
151
|
-
Text(this.logTrack.str1) // Text1
|
|
152
|
-
.id('str1')
|
|
153
|
-
.fontSize(this.isRender(1))
|
|
154
|
-
.fontWeight(FontWeight.Bold)
|
|
155
|
-
Text(this.logTrack.str2) // Text2
|
|
156
|
-
.fontSize(this.isRender(2))
|
|
157
|
-
.fontWeight(FontWeight.Bold)
|
|
158
|
-
Button('change logTrack.str1')
|
|
159
|
-
.id('str2')
|
|
160
|
-
.onClick(() => {
|
|
161
|
-
this.logTrack.str1 = 'Bye';
|
|
162
|
-
})
|
|
163
|
-
Text(this.logNotTrack.str1) // Text3
|
|
164
|
-
.fontSize(this.isRender(3))
|
|
165
|
-
.fontWeight(FontWeight.Bold)
|
|
166
|
-
Text(this.logNotTrack.str2) // Text4
|
|
167
|
-
.fontSize(this.isRender(4))
|
|
168
|
-
.fontWeight(FontWeight.Bold)
|
|
169
|
-
Button('change logNotTrack.str1')
|
|
170
|
-
.onClick(() => {
|
|
171
|
-
this.logNotTrack.str1 = 'Bye';
|
|
172
|
-
})
|
|
173
|
-
}
|
|
174
|
-
.width('100%')
|
|
175
|
-
}
|
|
176
|
-
.height('100%')
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
在上面的示例中:
|
|
182
|
-
|
|
183
|
-
1. 类LogTrack中的属性均被@Track装饰器装饰,点击按钮"change logTrack.str1",此时Text1刷新,Text2不刷新,只有一条日志输出,避免了冗余刷新。
|
|
184
|
-
|
|
185
|
-
```ts
|
|
186
|
-
Text 1 is rendered
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
2. 类logNotTrack中的属性均未被@Track装饰器装饰,点击按钮"change logNotTrack.str1",此时Text3、Text4均会刷新,有两条日志输出,存在冗余刷新。
|
|
190
|
-
|
|
191
|
-
```ts
|
|
192
|
-
Text 3 is rendered
|
|
193
|
-
Text 4 is rendered
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
## 限制条件
|
|
197
|
-
|
|
198
|
-
- 如果class类中使用了@Track装饰器,那么该class类中非@Track装饰的属性不能在@Component UI中使用,包括不能绑定在组件上、不能用于初始化子组件,错误的使用将导致运行时报错,详见[在UI中使用非@Track装饰的属性发生运行时报错](#在ui中使用非track装饰的属性发生运行时报错);可以在非UI中使用非@Track装饰的属性,如事件回调函数中、生命周期函数中等。
|
|
199
|
-
|
|
200
|
-
- API version 19及以后,@Track使用在[@ComponentV2](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-create-custom-components#componentv2)的UI中,不会引起运行时报错,但依旧不会刷新,详见[@Observed+@Track装饰的class(V1->V2)](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-v1-v2-mixusage#传递class类型v1-v2)、[@Observed+@Track装饰的class(V2->V1)](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-v1-v2-mixusage#传递class类型v2-v1)。
|
|
201
|
-
|
|
202
|
-
- 建议开发者不要混用包含@Track的class对象和不包含@Track的class对象,如联合类型中、类继承中等,容易在UI中误用非@Track装饰的属性,导致运行时报错。
|
|
203
|
-
|
|
204
|
-
## 使用场景
|
|
205
|
-
|
|
206
|
-
### @Track和自定义组件更新
|
|
207
|
-
|
|
208
|
-
以下示例展示组件更新和@Track的处理步骤。对象log是@State装饰的状态变量,logInfo是@Track装饰的成员属性,其余成员属性都是非@Track装饰的,而且也不准备在UI中更新它们的值。
|
|
209
|
-
|
|
210
|
-
```TypeScript
|
|
211
|
-
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
212
|
-
const DOMAIN_NUMBER: number = 0XFF00;
|
|
213
|
-
const TAG: string = '[Sample_StateTrack]';
|
|
214
|
-
class Log {
|
|
215
|
-
@Track public logInfo: string;
|
|
216
|
-
public owner: string;
|
|
217
|
-
public id: number;
|
|
218
|
-
public time: Date;
|
|
219
|
-
public location: string;
|
|
220
|
-
public reason: string;
|
|
221
|
-
|
|
222
|
-
constructor(logInfo: string) {
|
|
223
|
-
this.logInfo = logInfo;
|
|
224
|
-
this.owner = 'OH';
|
|
225
|
-
this.id = 0;
|
|
226
|
-
this.time = new Date();
|
|
227
|
-
this.location = 'CN';
|
|
228
|
-
this.reason = 'NULL';
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
@Entry
|
|
233
|
-
@Component
|
|
234
|
-
struct AddLog {
|
|
235
|
-
@State log: Log = new Log('origin info.');
|
|
236
|
-
|
|
237
|
-
build() {
|
|
238
|
-
Row() {
|
|
239
|
-
Column() {
|
|
240
|
-
Text(this.log.logInfo)
|
|
241
|
-
.fontSize(50)
|
|
242
|
-
.fontWeight(FontWeight.Bold)
|
|
243
|
-
.onClick(() => {
|
|
244
|
-
// 没有被@Track装饰的属性可以在点击事件中使用。
|
|
245
|
-
hilog.info(DOMAIN_NUMBER, TAG, 'owner: ' + this.log.owner +
|
|
246
|
-
' id: ' + this.log.id +
|
|
247
|
-
' time: ' + this.log.time +
|
|
248
|
-
' location: ' + this.log.location +
|
|
249
|
-
' reason: ' + this.log.reason);
|
|
250
|
-
this.log.time = new Date();
|
|
251
|
-
this.log.id++;
|
|
252
|
-
this.log.logInfo += ' info.';
|
|
253
|
-
})
|
|
254
|
-
}
|
|
255
|
-
.width('100%')
|
|
256
|
-
}
|
|
257
|
-
.height('100%')
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
处理步骤:
|
|
263
|
-
|
|
264
|
-
1. AddLog自定义组件的Text.onClick点击事件自增字符串' info.'。
|
|
265
|
-
|
|
266
|
-
2. 由于@State log变量的@Track属性logInfo更改,Text重新渲染。
|
|
267
|
-
|
|
268
|
-
## 常见问题
|
|
269
|
-
|
|
270
|
-
### 在UI中使用非@Track装饰的属性发生运行时报错
|
|
271
|
-
|
|
272
|
-
在UI中使用非@Track装饰的属性,运行时会报错。
|
|
273
|
-
|
|
274
|
-
```ts
|
|
275
|
-
class Person {
|
|
276
|
-
// id被@Track装饰
|
|
277
|
-
@Track id: number;
|
|
278
|
-
// age未被@Track装饰
|
|
279
|
-
age: number;
|
|
280
|
-
|
|
281
|
-
constructor(id: number, age: number) {
|
|
282
|
-
this.id = id;
|
|
283
|
-
this.age = age;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
@Entry
|
|
288
|
-
@Component
|
|
289
|
-
struct Parent {
|
|
290
|
-
@State parent: Person = new Person(2, 30);
|
|
291
|
-
|
|
292
|
-
build() {
|
|
293
|
-
// 没有被@Track装饰的属性不可以在UI中使用,运行时会报错
|
|
294
|
-
Text(`Parent id is: ${this.parent.id} and Parent age is: ${this.parent.age}`)
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
```
|
|
1
|
+
# @Track装饰器:class对象属性级更新
|
|
2
|
+
|
|
3
|
+
@Track应用于class对象的属性级更新。@Track装饰的属性变化时,只会触发该属性关联的UI更新。
|
|
4
|
+
|
|
5
|
+
在阅读本文档之前,建议开发者对状态管理基本观察能力有基本的了解。建议提前阅读:[@State](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-state)。
|
|
6
|
+
|
|
7
|
+
> **说明:**
|
|
8
|
+
>
|
|
9
|
+
> 从API version 11开始,该装饰器支持在ArkTS卡片中使用。
|
|
10
|
+
>
|
|
11
|
+
> 从API version 12开始,该装饰器支持在元服务中使用。
|
|
12
|
+
|
|
13
|
+
## 概述
|
|
14
|
+
|
|
15
|
+
@Track是class对象的属性装饰器。当一个class对象是状态变量时,@Track装饰的属性发生变化,只会触发该属性关联的UI更新;如果class类中使用了@Track装饰器,则未被@Track装饰器装饰的属性不能在UI中使用,如果使用,会发生运行时报错。
|
|
16
|
+
|
|
17
|
+
## class属性级更新说明
|
|
18
|
+
|
|
19
|
+
状态管理V1中@State等装饰器默认支持观察第一层属性的变化,第一层属性的变化虽然可以触发更新,但无法做到类属性级的观察,下面的例子就展示了这一限制:
|
|
20
|
+
|
|
21
|
+
```TypeScript
|
|
22
|
+
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
23
|
+
const DOMAIN_NUMBER: number = 0XFF00;
|
|
24
|
+
const TAG: string = '[Sample_StateTrack]';
|
|
25
|
+
class Info {
|
|
26
|
+
public name: string = 'Jack';
|
|
27
|
+
public age: number = 12;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@Entry
|
|
31
|
+
@Component
|
|
32
|
+
struct Index {
|
|
33
|
+
@State info: Info = new Info();
|
|
34
|
+
|
|
35
|
+
// 借助getFontSize的日志打印,可以分辨哪个组件触发了渲染
|
|
36
|
+
getFontSize(id: number): number {
|
|
37
|
+
hilog.info(DOMAIN_NUMBER, TAG, `Component ${id} render`);
|
|
38
|
+
return 30;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
build() {
|
|
42
|
+
Column() {
|
|
43
|
+
Text(`name: ${this.info.name}`)
|
|
44
|
+
.fontSize(this.getFontSize(1))
|
|
45
|
+
Text(`age: ${this.info.age}`)
|
|
46
|
+
.fontSize(this.getFontSize(2))
|
|
47
|
+
|
|
48
|
+
// 点击当前Button,可以发现当前虽然仅改变了name属性
|
|
49
|
+
// 但是依旧会触发两个Text的刷新
|
|
50
|
+
// Text(`age: ${this.info.age}`)是冗余刷新
|
|
51
|
+
Button('change name').onClick(() => {
|
|
52
|
+
this.info.name = 'Jane';
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// 点击当前Button,可以发现当前虽然仅改变了age属性
|
|
56
|
+
// 但是依旧会触发两个Text的刷新
|
|
57
|
+
// Text(`name: ${this.info.name}`)是冗余刷新
|
|
58
|
+
Button('change age').onClick(() => {
|
|
59
|
+
this.info.age++;
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
.height('100%')
|
|
63
|
+
.width('100%')
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
> **说明:**
|
|
69
|
+
>
|
|
70
|
+
> 当UI刷新时,会执行组件的属性设置方法,利用这一机制可以通过观察getFontSize方法是否被调用来判断当前组件是否刷新。
|
|
71
|
+
|
|
72
|
+
- UI首次渲染完成,观察到输出如下日志:
|
|
73
|
+
|
|
74
|
+
```text
|
|
75
|
+
Component 1 render
|
|
76
|
+
Component 2 render
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- 当点击Button('change name')时,即使只修改了info.name,观察日志发现两个Text组件仍会重新渲染。组件Text(`age: ${this.info.age}`)并未使用name属性,但仍因为info.name的改变而刷新,因此这次刷新是冗余的。日志输出如下:
|
|
80
|
+
|
|
81
|
+
```text
|
|
82
|
+
Component 1 render
|
|
83
|
+
Component 2 render
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- 同理,点击Button('change age'),也会触发Text(`name: ${this.info.name}`)的刷新。日志输出如下:
|
|
87
|
+
|
|
88
|
+
```text
|
|
89
|
+
Component 1 render
|
|
90
|
+
Component 2 render
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
造成上述冗余刷新的根本原因是:状态管理V1中@State等装饰器无法精准观察类属性的访问与变更。为了实现类对象属性的精准观察,引入@Track装饰器。
|
|
94
|
+
|
|
95
|
+
## 装饰器说明
|
|
96
|
+
|
|
97
|
+
| @Track变量装饰器 | 说明 |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| 装饰器参数 | 无 |
|
|
100
|
+
| 可装饰的变量 | class对象的非静态成员属性。 |
|
|
101
|
+
|
|
102
|
+
## 观察变化和行为表现
|
|
103
|
+
|
|
104
|
+
当一个class对象是状态变量时,@Track装饰的属性发生变化,该属性关联的UI触发更新。
|
|
105
|
+
|
|
106
|
+
> **说明:**
|
|
107
|
+
>
|
|
108
|
+
> 当class对象中没有一个属性被标记@Track,行为与原先保持不变。@Track没有深度观测的功能。
|
|
109
|
+
|
|
110
|
+
使用@Track装饰器可以避免冗余刷新。
|
|
111
|
+
|
|
112
|
+
```TypeScript
|
|
113
|
+
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
114
|
+
const DOMAIN_NUMBER: number = 0XFF00;
|
|
115
|
+
const TAG: string = '[Sample_StateTrack]';
|
|
116
|
+
|
|
117
|
+
class LogTrack {
|
|
118
|
+
@Track public str1: string;
|
|
119
|
+
@Track public str2: string;
|
|
120
|
+
|
|
121
|
+
constructor(str1: string) {
|
|
122
|
+
this.str1 = str1;
|
|
123
|
+
this.str2 = 'World';
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class LogNotTrack {
|
|
128
|
+
public str1: string;
|
|
129
|
+
public str2: string;
|
|
130
|
+
|
|
131
|
+
constructor(str1: string) {
|
|
132
|
+
this.str1 = str1;
|
|
133
|
+
this.str2 = 'World';
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@Entry
|
|
138
|
+
@Component
|
|
139
|
+
struct AddLog {
|
|
140
|
+
@State logTrack: LogTrack = new LogTrack('Hello');
|
|
141
|
+
@State logNotTrack: LogNotTrack = new LogNotTrack('Hello');
|
|
142
|
+
|
|
143
|
+
isRender(index: number) {
|
|
144
|
+
hilog.info(DOMAIN_NUMBER, TAG, `Text ${index} is rendered`);
|
|
145
|
+
return 50;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
build() {
|
|
149
|
+
Row() {
|
|
150
|
+
Column() {
|
|
151
|
+
Text(this.logTrack.str1) // Text1
|
|
152
|
+
.id('str1')
|
|
153
|
+
.fontSize(this.isRender(1))
|
|
154
|
+
.fontWeight(FontWeight.Bold)
|
|
155
|
+
Text(this.logTrack.str2) // Text2
|
|
156
|
+
.fontSize(this.isRender(2))
|
|
157
|
+
.fontWeight(FontWeight.Bold)
|
|
158
|
+
Button('change logTrack.str1')
|
|
159
|
+
.id('str2')
|
|
160
|
+
.onClick(() => {
|
|
161
|
+
this.logTrack.str1 = 'Bye';
|
|
162
|
+
})
|
|
163
|
+
Text(this.logNotTrack.str1) // Text3
|
|
164
|
+
.fontSize(this.isRender(3))
|
|
165
|
+
.fontWeight(FontWeight.Bold)
|
|
166
|
+
Text(this.logNotTrack.str2) // Text4
|
|
167
|
+
.fontSize(this.isRender(4))
|
|
168
|
+
.fontWeight(FontWeight.Bold)
|
|
169
|
+
Button('change logNotTrack.str1')
|
|
170
|
+
.onClick(() => {
|
|
171
|
+
this.logNotTrack.str1 = 'Bye';
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
.width('100%')
|
|
175
|
+
}
|
|
176
|
+
.height('100%')
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
在上面的示例中:
|
|
182
|
+
|
|
183
|
+
1. 类LogTrack中的属性均被@Track装饰器装饰,点击按钮"change logTrack.str1",此时Text1刷新,Text2不刷新,只有一条日志输出,避免了冗余刷新。
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
Text 1 is rendered
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
2. 类logNotTrack中的属性均未被@Track装饰器装饰,点击按钮"change logNotTrack.str1",此时Text3、Text4均会刷新,有两条日志输出,存在冗余刷新。
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
Text 3 is rendered
|
|
193
|
+
Text 4 is rendered
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## 限制条件
|
|
197
|
+
|
|
198
|
+
- 如果class类中使用了@Track装饰器,那么该class类中非@Track装饰的属性不能在@Component UI中使用,包括不能绑定在组件上、不能用于初始化子组件,错误的使用将导致运行时报错,详见[在UI中使用非@Track装饰的属性发生运行时报错](#在ui中使用非track装饰的属性发生运行时报错);可以在非UI中使用非@Track装饰的属性,如事件回调函数中、生命周期函数中等。
|
|
199
|
+
|
|
200
|
+
- API version 19及以后,@Track使用在[@ComponentV2](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-create-custom-components#componentv2)的UI中,不会引起运行时报错,但依旧不会刷新,详见[@Observed+@Track装饰的class(V1->V2)](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-v1-v2-mixusage#传递class类型v1-v2)、[@Observed+@Track装饰的class(V2->V1)](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-v1-v2-mixusage#传递class类型v2-v1)。
|
|
201
|
+
|
|
202
|
+
- 建议开发者不要混用包含@Track的class对象和不包含@Track的class对象,如联合类型中、类继承中等,容易在UI中误用非@Track装饰的属性,导致运行时报错。
|
|
203
|
+
|
|
204
|
+
## 使用场景
|
|
205
|
+
|
|
206
|
+
### @Track和自定义组件更新
|
|
207
|
+
|
|
208
|
+
以下示例展示组件更新和@Track的处理步骤。对象log是@State装饰的状态变量,logInfo是@Track装饰的成员属性,其余成员属性都是非@Track装饰的,而且也不准备在UI中更新它们的值。
|
|
209
|
+
|
|
210
|
+
```TypeScript
|
|
211
|
+
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
212
|
+
const DOMAIN_NUMBER: number = 0XFF00;
|
|
213
|
+
const TAG: string = '[Sample_StateTrack]';
|
|
214
|
+
class Log {
|
|
215
|
+
@Track public logInfo: string;
|
|
216
|
+
public owner: string;
|
|
217
|
+
public id: number;
|
|
218
|
+
public time: Date;
|
|
219
|
+
public location: string;
|
|
220
|
+
public reason: string;
|
|
221
|
+
|
|
222
|
+
constructor(logInfo: string) {
|
|
223
|
+
this.logInfo = logInfo;
|
|
224
|
+
this.owner = 'OH';
|
|
225
|
+
this.id = 0;
|
|
226
|
+
this.time = new Date();
|
|
227
|
+
this.location = 'CN';
|
|
228
|
+
this.reason = 'NULL';
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
@Entry
|
|
233
|
+
@Component
|
|
234
|
+
struct AddLog {
|
|
235
|
+
@State log: Log = new Log('origin info.');
|
|
236
|
+
|
|
237
|
+
build() {
|
|
238
|
+
Row() {
|
|
239
|
+
Column() {
|
|
240
|
+
Text(this.log.logInfo)
|
|
241
|
+
.fontSize(50)
|
|
242
|
+
.fontWeight(FontWeight.Bold)
|
|
243
|
+
.onClick(() => {
|
|
244
|
+
// 没有被@Track装饰的属性可以在点击事件中使用。
|
|
245
|
+
hilog.info(DOMAIN_NUMBER, TAG, 'owner: ' + this.log.owner +
|
|
246
|
+
' id: ' + this.log.id +
|
|
247
|
+
' time: ' + this.log.time +
|
|
248
|
+
' location: ' + this.log.location +
|
|
249
|
+
' reason: ' + this.log.reason);
|
|
250
|
+
this.log.time = new Date();
|
|
251
|
+
this.log.id++;
|
|
252
|
+
this.log.logInfo += ' info.';
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
.width('100%')
|
|
256
|
+
}
|
|
257
|
+
.height('100%')
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
处理步骤:
|
|
263
|
+
|
|
264
|
+
1. AddLog自定义组件的Text.onClick点击事件自增字符串' info.'。
|
|
265
|
+
|
|
266
|
+
2. 由于@State log变量的@Track属性logInfo更改,Text重新渲染。
|
|
267
|
+
|
|
268
|
+
## 常见问题
|
|
269
|
+
|
|
270
|
+
### 在UI中使用非@Track装饰的属性发生运行时报错
|
|
271
|
+
|
|
272
|
+
在UI中使用非@Track装饰的属性,运行时会报错。
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
class Person {
|
|
276
|
+
// id被@Track装饰
|
|
277
|
+
@Track id: number;
|
|
278
|
+
// age未被@Track装饰
|
|
279
|
+
age: number;
|
|
280
|
+
|
|
281
|
+
constructor(id: number, age: number) {
|
|
282
|
+
this.id = id;
|
|
283
|
+
this.age = age;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
@Entry
|
|
288
|
+
@Component
|
|
289
|
+
struct Parent {
|
|
290
|
+
@State parent: Person = new Person(2, 30);
|
|
291
|
+
|
|
292
|
+
build() {
|
|
293
|
+
// 没有被@Track装饰的属性不可以在UI中使用,运行时会报错
|
|
294
|
+
Text(`Parent id is: ${this.parent.id} and Parent age is: ${this.parent.age}`)
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|