android2harmony 0.1.0 → 0.1.2
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 +407 -407
- package/agents/code-reviewer.md +16 -17
- package/agents/scripts/hmos-precheck/index.mjs +0 -8
- package/dist/index.js +10 -70
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
- package/skills/hmos-batch-ui-align/SKILL.md +5 -5
- package/skills/hmos-batch-ui-align/references/mvvm-v2/!!/350/257/255/346/263/225/357/274/232/345/217/214/345/220/221/347/273/221/345/256/232.md +206 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Computed/350/243/205/351/245/260/345/231/250/357/274/232/350/256/241/347/256/227/345/261/236/346/200/247.md +373 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Event/350/243/205/351/245/260/345/231/250/357/274/232/350/247/204/350/214/203/347/273/204/344/273/266/350/276/223/345/207/272.md +158 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Local/350/243/205/351/245/260/345/231/250/357/274/232/347/273/204/344/273/266/345/206/205/351/203/250/347/212/266/346/200/201.md +750 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Monitor/350/243/205/351/245/260/345/231/250/357/274/232/347/212/266/346/200/201/345/217/230/351/207/217/344/277/256/346/224/271/345/274/202/346/255/245/347/233/221/345/220/254.md +1704 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@ObservedV2/350/243/205/351/245/260/345/231/250/345/222/214@Trace/350/243/205/351/245/260/345/231/250/357/274/232/347/261/273/345/261/236/346/200/247/345/217/230/345/214/226/350/247/202/346/265/213.md +1012 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Once/350/243/205/351/245/260/345/231/250/357/274/232/345/210/235/345/247/213/345/214/226/345/220/214/346/255/245/344/270/200/346/254/241.md +164 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Param/350/243/205/351/245/260/345/231/250/357/274/232/347/273/204/344/273/266/345/244/226/351/203/250/350/276/223/345/205/245.md +840 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Provider/350/243/205/351/245/260/345/231/250/345/222/214@Consumer/350/243/205/351/245/260/345/231/250/357/274/232/350/267/250/347/273/204/344/273/266/345/261/202/347/272/247/345/217/214/345/220/221/345/220/214/346/255/245.md +856 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/@Type/350/243/205/351/245/260/345/231/250/357/274/232/346/240/207/350/256/260/347/261/273/345/261/236/346/200/247/347/232/204/347/261/273/345/236/213.md +83 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/AppStorageV2/357/274/232/345/272/224/347/224/250/345/205/250/345/261/200UI/347/212/266/346/200/201/345/255/230/345/202/250.md +294 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/MVVM/346/250/241/345/274/217/357/274/210V2/357/274/211.md +1407 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/PersistenceV2/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 +1220 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2/_/350/214/203/345/274/217/351/200/211/346/213/251/350/257/264/346/230/216.md +47 -0
- package/skills/hmos-batch-ui-align/references/mvvm-v2//347/212/266/346/200/201/347/256/241/347/220/206V1/345/220/221V2/350/277/201/347/247/273/344/270/216/346/267/267/347/224/250/346/214/207/345/257/274.md +840 -0
- package/skills/hmos-convert-pipeline/SKILL.md +37 -42
- package/skills/hmos-fix-build-errors/SKILL.md +20 -35
- package/skills/hmos-resources-convert/SKILL.md +2 -2
- package/skills/hmos-spec-generate/SKILL.md +10 -21
- package/agents/build-fixer.md +0 -385
- package/skills/hmos-spec-generate/references/android-platform-tokens.md +0 -105
- package/skills/hmos-spec-generate/references/spec-sample-1.md +0 -78
- package/skills/hmos-spec-generate/references/spec-sample-2.md +0 -58
- package/skills/hmos-spec-generate/references/spec-sample-3.md +0 -116
- package/skills/hmos-spec-generate/references/step4-report-template.md +0 -33
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# @Type装饰器:标记类属性的类型
|
|
2
|
+
---
|
|
3
|
+
# @Type装饰器:标记类属性的类型
|
|
4
|
+
为了实现序列化类时不丢失属性的复杂类型,开发者可以使用@Type装饰器装饰类属性。
|
|
5
|
+
@Type的目的是标记类属性,配合PersistenceV2使用,防止序列化时类丢失。在阅读本文档前,建议提前阅读: [PersistenceV2](D:/code/APIDevice/output/md_output/harmonyos-guides/应用框架/ArkUI(方舟UI框架)/UI开发 (ArkTS声明式开发范式)/学习UI范式状态管理/状态管理(V2)/管理应用拥有的状态/PersistenceV2_ 持久化存储UI状态/arkts-new-persistencev2.md) 。
|
|
6
|
+

|
|
7
|
+
@Type从API version 12开始支持。
|
|
8
|
+
从API version 12开始,该装饰器支持在元服务中使用。
|
|
9
|
+
#### 概述
|
|
10
|
+
@Type标记类属性,使得类属性序列化时不丢失类型信息,便于类的反序列化。
|
|
11
|
+
#### 装饰器说明
|
|
12
|
+
| @Type装饰器 | 说明 |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| 装饰器参数 | type:类型。 |
|
|
15
|
+
| 可装饰的类型 | Object class以及Array、Date、Map、Set等内嵌类型。 |
|
|
16
|
+
#### 使用限制
|
|
17
|
+
1.
|
|
18
|
+
只能用在 [@ObservedV2](D:/code/APIDevice/output/md_output/harmonyos-guides/应用框架/ArkUI(方舟UI框架)/UI开发 (ArkTS声明式开发范式)/学习UI范式状态管理/状态管理(V2)/管理数据对象的状态/@ObservedV2装饰器和@Trace装饰器:类属性变化观测/arkts-new-observedv2-and-trace.md) 装饰的类中,不能用在自定义组件中。
|
|
19
|
+
```
|
|
20
|
+
class Sample {
|
|
21
|
+
private data: number = 0;
|
|
22
|
+
}
|
|
23
|
+
@ObservedV2
|
|
24
|
+
class Info {
|
|
25
|
+
@Type(Sample)
|
|
26
|
+
@Trace public sample: Sample = new Sample(); // 正确用法
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
```
|
|
30
|
+
@Observed
|
|
31
|
+
class Info2 {
|
|
32
|
+
@Type(Sample)
|
|
33
|
+
sample: Sample = new Sample(); // 错误用法,不能用在@Observed装饰的类中,编译时报错
|
|
34
|
+
}
|
|
35
|
+
@ComponentV2
|
|
36
|
+
struct Index {
|
|
37
|
+
@Type(Sample)
|
|
38
|
+
sample: Sample = new Sample(); // 错误用法,不能用在自定义组件中,编译时报错
|
|
39
|
+
build() {
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
2.
|
|
44
|
+
不支持collections.Set、collections.Map等类型。
|
|
45
|
+
3.
|
|
46
|
+
不支持非built-in类型。如 [PixelMap](D:/code/APIDevice/output/md_output/harmonyos-references/媒体/Image Kit(图片处理服务)/ArkTS API/@ohos.multimedia.image (图片处理)/arkts-apis-image-pixelmap.md) 、NativePointer、 [ArrayList](D:/code/APIDevice/output/md_output/harmonyos-references/应用框架/ArkTS(方舟编程语言)/ArkTS API/js-apis-arraylist.md) 等Native类型。
|
|
47
|
+
4.
|
|
48
|
+
不支持简单类型。如string、number、boolean等。
|
|
49
|
+
5.
|
|
50
|
+
不支持构造函数含参的类。
|
|
51
|
+
#### 使用场景
|
|
52
|
+
#### 持久化数据
|
|
53
|
+
```
|
|
54
|
+
import { PersistenceV2, Type } from '@kit.ArkUI';
|
|
55
|
+
@ObservedV2
|
|
56
|
+
class SampleChild {
|
|
57
|
+
@Trace childNumber: number = 1;
|
|
58
|
+
}
|
|
59
|
+
@ObservedV2
|
|
60
|
+
class Sample {
|
|
61
|
+
// 对于复杂对象需要@Type修饰,确保反序列化成功,去掉@Type会反序列化值失败。
|
|
62
|
+
@Type(SampleChild)
|
|
63
|
+
// 对于没有初值的类属性,经过@Type修饰后,需要手动保存,否则持久化失败。
|
|
64
|
+
// 无法使用@Type修饰的类属性,必须要有初值才能持久化。
|
|
65
|
+
@Trace sampleChild?: SampleChild = undefined;
|
|
66
|
+
}
|
|
67
|
+
@Entry
|
|
68
|
+
@ComponentV2
|
|
69
|
+
struct TestCase {
|
|
70
|
+
@Local sample: Sample = PersistenceV2.connect(Sample, () => new Sample)!;
|
|
71
|
+
build() {
|
|
72
|
+
Column() {
|
|
73
|
+
Text('childNumber value:' + this.sample.sampleChild?.childNumber)
|
|
74
|
+
.onClick(() => {
|
|
75
|
+
this.sample.sampleChild = new SampleChild();
|
|
76
|
+
this.sample.sampleChild.childNumber = 2;
|
|
77
|
+
PersistenceV2.save(Sample);
|
|
78
|
+
})
|
|
79
|
+
.fontSize(30)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
为了增强状态管理框架对应用全局UI状态变量的共享能力,开发者可以使用AppStorageV2存储应用全局UI的状态变量数据。
|
|
2
|
+
|
|
3
|
+
AppStorageV2提供应用级全局共享状态变量的能力,开发者可以通过connect绑定同一个key,进行跨ability的数据共享。
|
|
4
|
+
|
|
5
|
+
在阅读本文档前,建议提前阅读:[@ComponentV2](../../../../学习UI范式基本语法/自定义组件/创建自定义组件/arkts-create-custom-components.md#componentv2),[@ObservedV2和@Trace](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/arkts-new-observedv2-and-trace),配合阅读:[AppStorageV2-API文档](<../../../../../../../../harmonyos-references/ArkUI(方舟UI框架)/ArkTS API/UI界面/@ohos.arkui.StateManagement (状态管理)/js-apis-statemanagement.md#appstoragev2>)。
|
|
6
|
+
|
|
7
|
+
说明
|
|
8
|
+
|
|
9
|
+
AppStorageV2从API version 12开始支持。
|
|
10
|
+
|
|
11
|
+
## 概述
|
|
12
|
+
|
|
13
|
+
AppStorageV2是在应用UI启动时会被创建的单例。它用于提供应用状态数据的中心存储,这些状态数据在应用级别都是可访问的。AppStorageV2将在应用运行过程保留其数据。数据通过唯一的键字符串值访问。需要注意的是,AppStorage与AppStorageV2之间的数据互不共享。
|
|
14
|
+
|
|
15
|
+
AppStorageV2可以修改connect的返回值,实现与UI组件的同步。
|
|
16
|
+
|
|
17
|
+
AppStorageV2支持应用的[主线程](<../../../../../../Ability Kit(程序框架服务)/Stage模型开发指导/线程模型/thread-model-stage.md>)内多个UIAbility实例间的状态共享。
|
|
18
|
+
|
|
19
|
+
## 使用说明
|
|
20
|
+
|
|
21
|
+
* connect:创建或获取存储的数据。
|
|
22
|
+
|
|
23
|
+
说明
|
|
24
|
+
|
|
25
|
+
1、若未指定key,使用第二个参数作为默认构造器;否则使用第三个参数作为默认构造器(第二个参数非法也使用第三个参数作为默认构造器)。
|
|
26
|
+
|
|
27
|
+
2、确保数据已经存储在AppStorageV2中,可省略默认构造器,获取存储的数据;否则必须指定默认构造器,不指定将导致应用异常。
|
|
28
|
+
|
|
29
|
+
3、同一个key,connect不同类型的数据会导致应用异常,应用需要确保类型匹配。
|
|
30
|
+
|
|
31
|
+
4、key建议使用有意义的值,可由字母、数字、下划线组成,长度不超过255,使用非法字符或空字符的行为是未定义的。
|
|
32
|
+
|
|
33
|
+
5、关联[@Observed](../../../状态管理(V1)/管理组件拥有的状态/@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化/arkts-observed-and-objectlink.md)对象时,由于该类型的name属性未定义,需要指定key或者自定义name属性。
|
|
34
|
+
|
|
35
|
+
* remove:删除指定key的存储数据。
|
|
36
|
+
|
|
37
|
+
说明
|
|
38
|
+
|
|
39
|
+
删除AppStorageV2中不存在的key会报警告。
|
|
40
|
+
|
|
41
|
+
* keys:返回所有AppStorageV2中的key。
|
|
42
|
+
|
|
43
|
+
以上接口详细描述请参考状态管理API文档中的[AppStorageV2](<../../../../../../../../harmonyos-references/ArkUI(方舟UI框架)/ArkTS API/UI界面/@ohos.arkui.StateManagement (状态管理)/js-apis-statemanagement.md#appstoragev2>)。
|
|
44
|
+
|
|
45
|
+
## 使用限制
|
|
46
|
+
|
|
47
|
+
1、只支持class类型,否则会抛出运行时报错,从API version 23开始,将返回错误码[140103](../../../../../../../../harmonyos-references/ArkUI(方舟UI框架)/错误码/UI界面/状态管理错误码/errorcode-statemanagement.md#section140103-appstoragev2和persistencev2使用不支持的数据类型)。
|
|
48
|
+
|
|
49
|
+
2、需要配合UI使用(UI线程),不能在其他线程使用,如不支持[@Sendable](../../../../../../ArkTS(方舟编程语言)/ArkTS并发/并发线程间通信/线程间通信对象/Sendable对象/Sendable对象简介/arkts-sendable.md)。
|
|
50
|
+
|
|
51
|
+
3、不支持[collections.Set](<../../../../../../../../harmonyos-references/ArkTS(方舟编程语言)/ArkTS API/@arkts.collections (ArkTS容器集)/Class (Set)/arkts-apis-arkts-collections-set.md>)、[collections.Map](<../../../../../../../../harmonyos-references/ArkTS(方舟编程语言)/ArkTS API/@arkts.collections (ArkTS容器集)/Class (Map)/arkts-apis-arkts-collections-map.md>)等类型。
|
|
52
|
+
|
|
53
|
+
4、不支持非built-in类型,如[PixelMap](<../../../../../../../../harmonyos-references/Image Kit(图片处理服务)/ArkTS API/@ohos.multimedia.image (图片处理)/Interface (PixelMap)/arkts-apis-image-pixelmap.md>)、NativePointer、[ArrayList](<../../../../../../../../harmonyos-references/ArkTS(方舟编程语言)/ArkTS API/@ohos.util.ArrayList (线性容器ArrayList)/js-apis-arraylist.md>)等Native类型。
|
|
54
|
+
|
|
55
|
+
5、不支持存储基本类型,如string、number、boolean等。注意:不支持存储基本类型意味着connect接口传入的类型不能是基本类型,但connect传入的class中可以包含基本类型。
|
|
56
|
+
|
|
57
|
+
## 使用场景
|
|
58
|
+
|
|
59
|
+
### 使用AppStorageV2
|
|
60
|
+
|
|
61
|
+
AppStorageV2使用connect接口即可实现对AppStorageV2中数据的修改和同步,如果修改的数据被@Trace装饰,该数据的修改会同步更新UI。需要注意的是,使用remove接口只会将数据从AppStorageV2中删除,不影响组件中已创建的数据,详见以下示例代码:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
1. import { AppStorageV2 } from '@kit.ArkUI';
|
|
65
|
+
|
|
66
|
+
3. @ObservedV2
|
|
67
|
+
4. class Message {
|
|
68
|
+
5. @Trace public userID: number;
|
|
69
|
+
6. public userName: string;
|
|
70
|
+
|
|
71
|
+
8. constructor(userID?: number, userName?: string) {
|
|
72
|
+
9. this.userID = userID ?? 1;
|
|
73
|
+
10. this.userName = userName ?? 'Jack';
|
|
74
|
+
11. }
|
|
75
|
+
12. }
|
|
76
|
+
|
|
77
|
+
14. @Entry
|
|
78
|
+
15. @ComponentV2
|
|
79
|
+
16. struct Index {
|
|
80
|
+
17. // 使用connect在AppStorageV2中创建一个key为Message的对象
|
|
81
|
+
18. // 修改connect的返回值即可同步回AppStorageV2
|
|
82
|
+
19. @Local message: Message = AppStorageV2.connect<Message>(Message, () => new Message())!;
|
|
83
|
+
|
|
84
|
+
21. build() {
|
|
85
|
+
22. Column() {
|
|
86
|
+
23. // 修改@Trace装饰的类属性,UI能同步刷新
|
|
87
|
+
24. Button(`Index userID: ${this.message.userID}`)
|
|
88
|
+
25. .onClick(() => {
|
|
89
|
+
26. this.message.userID += 1;
|
|
90
|
+
27. })
|
|
91
|
+
28. // 修改非@Trace装饰的类属性,UI不会同步刷新,但修改的类属性已同步回AppStorageV2
|
|
92
|
+
29. Button(`Index userName: ${this.message.userName}`)
|
|
93
|
+
30. .onClick(() => {
|
|
94
|
+
31. this.message.userName += 'suf';
|
|
95
|
+
32. })
|
|
96
|
+
33. // remove key Message, 会从AppStorageV2中删除key为Message的对象
|
|
97
|
+
34. // remove之后,修改父组件的userId,子组件能同步变化,因为remove只是从AppStorageV2删除,不会影响组件中已存在的数据
|
|
98
|
+
35. Button('remove key: Message')
|
|
99
|
+
36. .onClick(() => {
|
|
100
|
+
37. AppStorageV2.remove<Message>(Message);
|
|
101
|
+
38. })
|
|
102
|
+
39. // connect key Message, 会从AppStorageV2中添加key为Message的对象
|
|
103
|
+
40. // remove之后,重新添加,修改父子组件的userID,可以发现数据已经不同步,子组件重新connect之后,数据一致
|
|
104
|
+
41. Button('connect key: Message')
|
|
105
|
+
42. .onClick(() => {
|
|
106
|
+
43. this.message = AppStorageV2.connect<Message>(Message, () => new Message(5, 'Rose'))!;
|
|
107
|
+
44. })
|
|
108
|
+
45. Divider()
|
|
109
|
+
46. Child()
|
|
110
|
+
47. }
|
|
111
|
+
48. .width('100%')
|
|
112
|
+
49. .height('100%')
|
|
113
|
+
50. }
|
|
114
|
+
51. }
|
|
115
|
+
|
|
116
|
+
53. @ComponentV2
|
|
117
|
+
54. struct Child {
|
|
118
|
+
55. // 使用connect在AppStorageV2中取出一个key为Message的对象,已在父组件中创建
|
|
119
|
+
56. @Local message: Message = AppStorageV2.connect<Message>(Message, () => new Message())!;
|
|
120
|
+
57. @Local name: string = this.message.userName;
|
|
121
|
+
|
|
122
|
+
59. build() {
|
|
123
|
+
60. Column() {
|
|
124
|
+
61. // 修改@Trace装饰的类属性,UI同步刷新,父组件能感知该变化
|
|
125
|
+
62. Button(`Child userID: ${this.message.userID}`)
|
|
126
|
+
63. .onClick(() => {
|
|
127
|
+
64. this.message.userID += 5;
|
|
128
|
+
65. })
|
|
129
|
+
66. // 修改父组件中的userName属性,点击name可以同步父组件的类属性修改
|
|
130
|
+
67. Button(`Child name: ${this.name}`)
|
|
131
|
+
68. .onClick(() => {
|
|
132
|
+
69. this.name = this.message.userName;
|
|
133
|
+
70. })
|
|
134
|
+
71. // remove key Message, 会从AppStorageV2中删除key为Message的对象
|
|
135
|
+
72. Button('remove key: Message')
|
|
136
|
+
73. .onClick(() => {
|
|
137
|
+
74. AppStorageV2.remove<Message>(Message);
|
|
138
|
+
75. })
|
|
139
|
+
76. // connect key Message, 会从AppStorageV2中添加key为Message的对象
|
|
140
|
+
77. Button('connect key: Message')
|
|
141
|
+
78. .onClick(() => {
|
|
142
|
+
79. this.message = AppStorageV2.connect<Message>(Message, () => new Message(10, 'Lucy'))!;
|
|
143
|
+
80. })
|
|
144
|
+
81. }
|
|
145
|
+
82. .width('100%')
|
|
146
|
+
83. .height('100%')
|
|
147
|
+
84. }
|
|
148
|
+
85. }
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
[AppStorageV2.ets](https://gitcode.com/HarmonyOS_Samples/guide-snippets/blob/HarmonyOS-feature-20260402/ArkUISample/AppStorageV2/entry/src/main/ets/pages/AppStorageV2.ets#L15-L103)
|
|
152
|
+
|
|
153
|
+
### 在两个页面之间存储数据
|
|
154
|
+
|
|
155
|
+
数据页面
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
1. // 数据中心
|
|
159
|
+
2. // Sample.ets
|
|
160
|
+
3. @ObservedV2
|
|
161
|
+
4. export class Sample {
|
|
162
|
+
5. @Trace public p1: number = 0;
|
|
163
|
+
6. public p2: number = 10;
|
|
164
|
+
7. }
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
[Sample.ets](https://gitcode.com/HarmonyOS_Samples/guide-snippets/blob/HarmonyOS-feature-20260402/ArkUISample/AppStorageV2/entry/src/main/ets/pages/Sample.ets#L15-L23)
|
|
168
|
+
|
|
169
|
+
页面1
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
1. import { AppStorageV2 } from '@kit.ArkUI';
|
|
173
|
+
2. import { Sample } from './Sample';
|
|
174
|
+
|
|
175
|
+
4. @Entry
|
|
176
|
+
5. @ComponentV2
|
|
177
|
+
6. struct PageOne {
|
|
178
|
+
7. // 在AppStorageV2中创建一个key为Sample的键值对(如果存在,则返回AppStorageV2中的数据),并且和prop关联
|
|
179
|
+
8. @Local prop: Sample = AppStorageV2.connect(Sample, () => new Sample())!;
|
|
180
|
+
9. pageStack: NavPathStack = new NavPathStack();
|
|
181
|
+
|
|
182
|
+
11. build() {
|
|
183
|
+
12. Navigation(this.pageStack) {
|
|
184
|
+
13. Column() {
|
|
185
|
+
14. Button('Go to pageTwo')
|
|
186
|
+
15. .onClick(() => {
|
|
187
|
+
16. this.pageStack.pushPathByName('PageTwo', null);
|
|
188
|
+
17. })
|
|
189
|
+
|
|
190
|
+
19. Button('PageOne connect the key Sample')
|
|
191
|
+
20. .onClick(() => {
|
|
192
|
+
21. // 在AppStorageV2中创建一个key为Sample的键值对(如果存在,则返回AppStorageV2中的数据),并且和prop关联
|
|
193
|
+
22. this.prop = AppStorageV2.connect(Sample, 'Sample', () => new Sample())!;
|
|
194
|
+
23. })
|
|
195
|
+
|
|
196
|
+
25. Button('PageOne remove the key Sample')
|
|
197
|
+
26. .onClick(() => {
|
|
198
|
+
27. // 从AppStorageV2中删除后,prop将不会再与key为Sample的值关联
|
|
199
|
+
28. AppStorageV2.remove(Sample);
|
|
200
|
+
29. })
|
|
201
|
+
|
|
202
|
+
31. Text(`PageOne add 1 to prop.p1: ${this.prop.p1}`)
|
|
203
|
+
32. .fontSize(30)
|
|
204
|
+
33. .onClick(() => {
|
|
205
|
+
34. this.prop.p1++;
|
|
206
|
+
35. })
|
|
207
|
+
|
|
208
|
+
37. Text(`PageOne add 1 to prop.p2: ${this.prop.p2}`)
|
|
209
|
+
38. .fontSize(30)
|
|
210
|
+
39. .onClick(() => {
|
|
211
|
+
40. // 页面不刷新,但是p2的值改变了
|
|
212
|
+
41. this.prop.p2++;
|
|
213
|
+
42. })
|
|
214
|
+
|
|
215
|
+
44. // 获取当前AppStorageV2里面的所有key
|
|
216
|
+
45. Text(`all keys in AppStorage: ${AppStorageV2.keys()}`)
|
|
217
|
+
46. .fontSize(30)
|
|
218
|
+
47. }
|
|
219
|
+
48. }
|
|
220
|
+
49. }
|
|
221
|
+
50. }
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
[PageOne.ets](https://gitcode.com/HarmonyOS_Samples/guide-snippets/blob/HarmonyOS-feature-20260402/ArkUISample/AppStorageV2/entry/src/main/ets/pages/PageOne.ets#L15-L66)
|
|
225
|
+
|
|
226
|
+
页面2
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
1. import { AppStorageV2 } from '@kit.ArkUI';
|
|
230
|
+
2. import { Sample } from './Sample';
|
|
231
|
+
|
|
232
|
+
4. @Builder
|
|
233
|
+
5. export function PageTwoBuilder() {
|
|
234
|
+
6. PageTwo()
|
|
235
|
+
7. }
|
|
236
|
+
|
|
237
|
+
9. @ComponentV2
|
|
238
|
+
10. struct PageTwo {
|
|
239
|
+
11. // 在AppStorageV2中创建一个key为Sample的键值对(如果存在,则返回AppStorageV2中的数据),并且和prop关联
|
|
240
|
+
12. @Local prop: Sample = AppStorageV2.connect(Sample, () => new Sample())!;
|
|
241
|
+
13. pathStack: NavPathStack = new NavPathStack();
|
|
242
|
+
|
|
243
|
+
15. build() {
|
|
244
|
+
16. NavDestination() {
|
|
245
|
+
17. Column() {
|
|
246
|
+
18. Button('PageTwo connect the key Sample1')
|
|
247
|
+
19. .onClick(() => {
|
|
248
|
+
20. // 在AppStorageV2中创建一个key为Sample1的键值对(如果存在,则返回AppStorageV2中的数据),并且和prop关联
|
|
249
|
+
21. this.prop = AppStorageV2.connect(Sample, 'Sample1', () => new Sample())!;
|
|
250
|
+
22. })
|
|
251
|
+
|
|
252
|
+
24. Text(`PageTwo add 1 to prop.p1: ${this.prop.p1}`)
|
|
253
|
+
25. .fontSize(30)
|
|
254
|
+
26. .onClick(() => {
|
|
255
|
+
27. this.prop.p1++;
|
|
256
|
+
28. })
|
|
257
|
+
|
|
258
|
+
30. Text(`PageTwo add 1 to prop.p2: ${this.prop.p2}`)
|
|
259
|
+
31. .fontSize(30)
|
|
260
|
+
32. .onClick(() => {
|
|
261
|
+
33. // 页面不刷新,但是p2的值改变了;只有重新初始化才会改变
|
|
262
|
+
34. this.prop.p2++;
|
|
263
|
+
35. })
|
|
264
|
+
|
|
265
|
+
37. // 获取当前AppStorageV2里面的所有key
|
|
266
|
+
38. Text(`all keys in AppStorage: ${AppStorageV2.keys()}`)
|
|
267
|
+
39. .fontSize(30)
|
|
268
|
+
40. }
|
|
269
|
+
41. }
|
|
270
|
+
42. .onReady((context: NavDestinationContext) => {
|
|
271
|
+
43. this.pathStack = context.pathStack;
|
|
272
|
+
44. })
|
|
273
|
+
45. }
|
|
274
|
+
46. }
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
[PageTwo.ets](https://gitcode.com/HarmonyOS_Samples/guide-snippets/blob/HarmonyOS-feature-20260402/ArkUISample/AppStorageV2/entry/src/main/ets/pages/PageTwo.ets#L15-L62)
|
|
278
|
+
|
|
279
|
+
使用Navigation时,需要添加配置系统路由表文件src/main/resources/base/profile/route\_map.json,并替换pageSourceFile为PageTwo页面的路径,并且在module.json5中添加:"routerMap": "$profile:route\_map"。
|
|
280
|
+
|
|
281
|
+
```
|
|
282
|
+
1. {
|
|
283
|
+
2. "routerMap": [
|
|
284
|
+
3. {
|
|
285
|
+
4. "name": "PageTwo",
|
|
286
|
+
5. "pageSourceFile": "src/main/ets/pages/PageTwo.ets",
|
|
287
|
+
6. "buildFunction": "PageTwoBuilder",
|
|
288
|
+
7. "data": {
|
|
289
|
+
8. "description" : "AppStorageV2 example"
|
|
290
|
+
9. }
|
|
291
|
+
10. }
|
|
292
|
+
11. ]
|
|
293
|
+
12. }
|
|
294
|
+
```
|