• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# State Management with Application-level Variables
2
3
4The state management module provides data storage, persistent data management, UIAbility data storage, and environment state required by applications.
5
6
7>**NOTE**
8>
9>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
10
11
12The meanings of T and S in this topic are as follows:
13
14
15| Type  | Description                                    |
16| ---- | -------------------------------------- |
17| T    | Class, number, boolean, string, and arras of these types.|
18| S    | number, boolean, string.                |
19
20
21## AppStorage
22
23
24### Link
25
26static Link(propName: string): any
27
28Establishes two-way data binding with the given attribute (specified by **propName**) in AppStorage. If the given attribute exists in AppStorage, the two-way bound data of the attribute in AppStorage is returned.
29
30Any update of the data is synchronized back to AppStorage, which then synchronizes the update to all data and custom components bound to the attribute.
31
32If the given attribute does not exist in AppStorage, **undefined** is returned.
33
34**Parameters**
35
36| Name     | Type    | Mandatory  | Description            |
37| -------- | ------ | ---- | ---------------- |
38| propName | string | Yes   | Attribute name in AppStorage.|
39
40**Return value**
41
42| Type  | Description                                      |
43| ---- | ---------------------------------------- |
44| any  | Returns two-way bound data if specified attribute exists in AppStorage; returns **undefined** otherwise.|
45
46
47```ts
48AppStorage.SetOrCreate('PropA', 47);
49let linkToPropA1 = AppStorage.Link('PropA');
50let linkToPropA2 = AppStorage.Link('PropA'); // linkToPropA2.get() == 47
51linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48
52```
53
54
55### SetAndLink
56
57static SetAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T>
58
59Works in a way similar to the **Link** API. If the given attribute exists in AppStorage, the two-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in AppStorage, and two-way bound data is returned.
60
61**Parameters**
62
63| Name         | Type    | Mandatory  | Description                                    |
64| ------------ | ------ | ---- | ---------------------------------------- |
65| propName     | string | Yes   | Attribute name in AppStorage.                        |
66| defaultValue | T      | Yes   | Default value used to initialize the attribute with the specified attribute name in AppStorage.|
67
68**Return value**
69
70| Type                                 | Description                                      |
71| ----------------------------------- | ---------------------------------------- |
72| SubscribedAbstractProperty<T> | Instance of **SubscribedAbstractProperty<T>** and two-way bound data of the given attribute in AppStorage|
73
74
75```ts
76AppStorage.SetOrCreate('PropA', 47);
77let link1: SubscribedAbstractProperty<number> = AppStorage.SetAndLink('PropB', 49); // Create PropB 49
78let link2: SubscribedAbstractProperty<number> = AppStorage.SetAndLink('PropA', 50); // PropA exists, remains 47
79```
80
81
82### Prop
83
84static Prop(propName: string): any
85
86Establishes one-way data binding with the given attribute (specified by **propName**) in AppStorage. If the given attribute exists in AppStorage, the one-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist in AppStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to AppStorage.
87
88>**NOTE**
89> Prop supports only simple types.
90
91**Parameters**
92
93| Name     | Type    | Mandatory  | Description            |
94| -------- | ------ | ---- | ---------------- |
95| propName | string | Yes   | Attribute name in AppStorage.|
96
97**Return value**
98
99| Type  | Description                                      |
100| ---- | ---------------------------------------- |
101| any  | Returns one-way bound data if specified attribute exists in AppStorage; returns **undefined** otherwise.|
102
103
104```ts
105AppStorage.SetOrCreate('PropA', 47);
106let prop1 = AppStorage.Prop('PropA');
107let prop2 = AppStorage.Prop('PropA');
108prop1.set(1); // one-way sync: prop1.get()=1; but prop2.get() == 47
109```
110
111
112### SetAndProp
113
114static SetAndProp&lt;S&gt;(propName: string, defaultValue: S): SubscribedAbstractProperty&lt;S&gt;
115
116Works in a way similar to the **Prop** API. If the given attribute exists in AppStorage, the one-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in AppStorage, and one-way bound data is returned.
117
118**Parameters**
119
120| Name         | Type    | Mandatory  | Description                                    |
121| ------------ | ------ | ---- | ---------------------------------------- |
122| propName     | string | Yes   | Attribute name in AppStorage.                        |
123| defaultValue | S      | Yes   | Default value used to initialize the attribute with the specified attribute name in AppStorage.|
124
125**Return value**
126
127| Type                                 | Description                                     |
128| ----------------------------------- | --------------------------------------- |
129| SubscribedAbstractProperty&lt;S&gt; | Instance of **SubscribedAbstractProperty&lt;S&gt;**.|
130
131
132```ts
133AppStorage.SetOrCreate('PropA', 47);
134let prop: SubscribedAbstractProperty<number> = AppStorage.SetAndProp('PropB', 49); // PropA -> 47, PropB -> 49
135```
136
137
138### Has
139
140static Has(propName: string): boolean
141
142Checks whether the attribute with the specified attribute name exists in AppStorage.
143
144**Parameters**
145
146| Name     | Type    | Mandatory  | Description            |
147| -------- | ------ | ---- | ---------------- |
148| propName | string | Yes   | Attribute name in AppStorage.|
149
150**Return value**
151
152| Type     | Description                                      |
153| ------- | ---------------------------------------- |
154| boolean | Returns **true** if the attribute with the specified attribute name exists in AppStorage; returns **false** otherwise.|
155
156
157```ts
158AppStorage.Has('simpleProp');
159```
160
161
162### Get
163
164static Get&lt;T&gt;(propName: string): T | undefined
165
166Obtains the attribute with the specified attribute name in AppStorage. If the attribute does not exist, **undefined** is returned.
167
168**Parameters**
169
170| Name     | Type    | Mandatory  | Description            |
171| -------- | ------ | ---- | ---------------- |
172| propName | string | Yes   | Attribute name in AppStorage.|
173
174**Return value**
175
176| Type                      | Description                                      |
177| ------------------------ | ---------------------------------------- |
178| T&nbsp;\|&nbsp;undefined | Returns the attribute with the specified attribute name in AppStorage; returns **undefined** if the attribute does not exist.|
179
180
181```ts
182AppStorage.SetOrCreate('PropA', 47);
183let value: number = AppStorage.Get('PropA'); // 47
184```
185
186
187### Set
188
189static Set&lt;T&gt;(propName: string, newValue: T): boolean
190
191Sets the value for the attribute with the specified attribute name in AppStorage.
192
193**Parameters**
194
195| Name     | Type    | Mandatory  | Description                  |
196| -------- | ------ | ---- | ---------------------- |
197| propName | string | Yes   | Attribute name in AppStorage.      |
198| newValue | T      | Yes   | Attribute value, which cannot be **undefined** or **null**.|
199
200**Return value**
201
202| Type     | Description                                      |
203| ------- | ---------------------------------------- |
204| boolean | Returns **true** if the operation is successful; return **false** if the attribute with the specified attribute name does not exist in AppStorage, or the value to set is **undefined** or **null**.  |
205
206
207```ts
208AppStorage.SetOrCreate('PropA', 48);
209let res: boolean = AppStorage.Set('PropA', 47) // true
210let res1: boolean = AppStorage.Set('PropB', 47) // false
211```
212
213
214### SetOrCreate
215
216static SetOrCreate&lt;T&gt;(propName: string, newValue: T): void
217
218Sets a new value for the attribute with the specified attribute name in AppStorage or, if the attribute does not exist, creates one with the specified attribute name and default value.
219
220**Parameters**
221
222| Name     | Type    | Mandatory  | Description                  |
223| -------- | ------ | ---- | ---------------------- |
224| propName | string | Yes   | Attribute name in AppStorage.      |
225| newValue | T      | Yes   | Attribute value, which cannot be **undefined** or **null**.|
226
227
228```ts
229AppStorage.SetOrCreate('simpleProp', 121);
230```
231
232
233### Delete
234
235static Delete(propName: string): boolean
236
237Deletes the attribute with the specified attribute name from AppStorage
238
239under the prerequisite that the attribute does not have a subscriber. If there is a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
240
241The subscribers of the attribute are attributes with the same name bound to APIs such as **Link** and **Prop**, **\@StorageLink('propName')**, and **\@StorageProp('propName')**. This means that if **\@StorageLink('propName')** and **\@StorageProp('propName')** are used in a custom component or if there is still a **SubscribedAbstractProperty** instance in sync with the attribute, the attribute cannot be deleted from AppStorage.
242
243**Parameters**
244
245| Name     | Type    | Mandatory  | Description            |
246| -------- | ------ | ---- | ---------------- |
247| propName | string | Yes   | Attribute name in AppStorage.|
248
249**Return value**
250
251| Type     | Description                                      |
252| ------- | ---------------------------------------- |
253| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
254
255
256```ts
257AppStorage.SetOrCreate('PropA', 47);
258AppStorage.Link('PropA');
259let res: boolean = AppStorage.Delete('PropA'); // false, PropA still has a subscriber
260
261AppStorage.SetOrCreate('PropB', 48);
262let res1: boolean = AppStorage.Delete('PropB'); // true, PropB is deleted from AppStorage successfully
263```
264
265
266### Keys
267
268static Keys(): IterableIterator&lt;string&gt;
269
270Obtains all attribute names in AppStorage.
271
272**Return value**
273
274| Type                            | Description                |
275| ------------------------------ | ------------------ |
276| IterableIterator&lt;string&gt; | All attribute names in AppStorage.|
277
278
279```ts
280AppStorage.SetOrCreate('PropB', 48);
281let keys: IterableIterator<string> = AppStorage.Keys();
282```
283
284
285### staticClear
286
287static staticClear(): boolean
288
289Deletes all attributes.
290
291This API is deprecated since API version 9. You are advised to use [Clear9+](#clear9) instead.
292
293**Return value**
294
295| Type     | Description                               |
296| ------- | --------------------------------- |
297| boolean | Returns **true** if all attributes are deleted; returns **false** if any of the attributes is being referenced by a state variable.|
298
299
300```ts
301let simple = AppStorage.staticClear();
302```
303
304
305### Clear<sup>9+</sup>
306
307static Clear(): boolean
308
309Deletes all attributes from AppStorage under the prerequisite that none of the attributes has a subscriber. If any of the attributes has a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
310
311For details about the subscriber, see [Delete](#delete).
312
313**Return value**
314
315| Type     | Description                                      |
316| ------- | ---------------------------------------- |
317| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
318
319
320```typescript
321AppStorage.SetOrCreate('PropA', 47);
322let res: boolean = AppStorage.Clear(); // true, there are no subscribers
323```
324
325
326### IsMutable
327
328static IsMutable(propName: string): boolean
329
330Checks whether the given attribute in AppStorage name is mutable.
331
332**Parameters**
333
334| Name     | Type    | Mandatory  | Description            |
335| -------- | ------ | ---- | ---------------- |
336| propName | string | Yes   | Attribute name in AppStorage.|
337
338**Return value**
339
340| Type     | Description                              |
341| ------- | -------------------------------- |
342| boolean | Whether the given attribute in AppStorage name is mutable.|
343
344
345```ts
346AppStorage.SetOrCreate('PropA', 47);
347let res: boolean = AppStorage.IsMutable('simpleProp');
348```
349
350
351### Size
352
353static Size(): number
354
355Obtains the number of attributes in AppStorage.
356
357**Return value**
358
359| Type    | Description                 |
360| ------ | ------------------- |
361| number | Number of attributes in AppStorage.|
362
363
364```ts
365AppStorage.SetOrCreate('PropB', 48);
366let res: number = AppStorage.Size(); // 1
367```
368
369
370## LocalStorage<sup>9+</sup>
371
372
373### constructor<sup>9+</sup>
374
375constructor(initializingProperties?: Object)
376
377Creates a **LocalStorage** instance and initializes it using the attributes and values returned by **Object.keys(initializingProperties)**.
378
379Since API version 9, this API is supported in ArkTS widgets.
380
381**Parameters**
382
383| Name                   | Type    | Mandatory  | Description                                    |
384| ---------------------- | ------ | ---- | ---------------------------------------- |
385| initializingProperties | Object | No   | Attributes and values used to initialize the **LocalStorage** instance. The value cannot be **undefined**.|
386
387
388```ts
389let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
390```
391
392
393### GetShared<sup>9+</sup>
394
395static GetShared(): LocalStorage
396
397Obtains the **LocalStorage** instance shared by the current stage.
398
399Since API version 9, this API is supported in ArkTS widgets.
400
401**Model restriction**: This API can be used only in the stage model.
402
403**Return value**
404
405| Type                            | Description               |
406| ------------------------------ | ----------------- |
407| [LocalStorage](#localstorage9) | **LocalStorage** instance.|
408
409
410```ts
411let storage: LocalStorage = LocalStorage.GetShared();
412```
413
414
415### has<sup>9+</sup>
416
417has(propName: string): boolean
418
419Checks whether the attribute with the specified attribute name exists in LocalStorage.
420
421Since API version 9, this API is supported in ArkTS widgets.
422
423**Parameters**
424
425| Name     | Type    | Mandatory  | Description              |
426| -------- | ------ | ---- | ------------------ |
427| propName | string | Yes   | Attribute name in LocalStorage.|
428
429**Return value**
430
431| Type     | Description                                      |
432| ------- | ---------------------------------------- |
433| boolean | Returns **true** if the target attribute exists in AppStorage; returns **false** otherwise.|
434
435
436```
437let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
438storage.has('PropA'); // true
439```
440
441
442### get<sup>9+</sup>
443
444get&lt;T&gt;(propName: string): T | undefined
445
446Obtains the attribute with the specified attribute name in LocalStorage.
447
448Since API version 9, this API is supported in ArkTS widgets.
449
450**Parameters**
451
452| Name     | Type    | Mandatory  | Description              |
453| -------- | ------ | ---- | ------------------ |
454| propName | string | Yes   | Attribute name in LocalStorage.|
455
456**Return value**
457
458| Type                      | Description                                      |
459| ------------------------ | ---------------------------------------- |
460| T&nbsp;\|&nbsp;undefined | Returns the attribute with the specified attribute name in LocalStorage; returns **undefined** if the attribute does not exist.|
461
462
463```ts
464let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
465let value: number = storage.get('PropA'); // 47
466```
467
468
469### set<sup>9+</sup>
470
471set&lt;T&gt;(propName: string, newValue: T): boolean
472
473Sets the value for the attribute with the specified attribute name in LocalStorage.
474
475Since API version 9, this API is supported in ArkTS widgets.
476
477**Parameters**
478
479| Name     | Type    | Mandatory  | Description                   |
480| -------- | ------ | ---- | ----------------------- |
481| propName | string | Yes   | Attribute name in LocalStorage.     |
482| newValue | T      | Yes   | Attribute value, which cannot be **undefined** or **null**.|
483
484**Return value**
485
486| Type     | Description                                      |
487| ------- | ---------------------------------------- |
488| boolean | Returns **true** if the operation is successful; return **false** if the attribute with the specified attribute name does not exist in LocalStorage, or the value to set is **undefined** or **null**.  |
489
490
491```ts
492let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
493let res: boolean = storage.set('PropA', 47); // true
494let res1: boolean = storage.set('PropB', 47); // false
495```
496
497
498### setOrCreate<sup>9+</sup>
499
500setOrCreate&lt;T&gt;(propName: string, newValue: T): boolean
501
502Sets a new value for the attribute with the specified attribute name in LocalStorage or, if the attribute does not exist, creates one with the specified attribute name and default value.
503
504Since API version 9, this API is supported in ArkTS widgets.
505
506**Parameters**
507
508| Name     | Type    | Mandatory  | Description                   |
509| -------- | ------ | ---- | ----------------------- |
510| propName | string | Yes   | Attribute name in LocalStorage.     |
511| newValue | T      | Yes   | Attribute value, which cannot be **undefined** or **null**.|
512
513**Return value**
514
515| Type     | Description                                      |
516| ------- | ---------------------------------------- |
517| boolean | Returns **false** if **newValue** is set to **undefined** or **null**.<br>Updates the target attribute with the new value and returns **true** if the attribute exists in LocalStorage.<br>Creates an attribute with the specified attribute name and default value if the attribute does not exist in LocalStorage.|
518
519
520```ts
521let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
522let res: boolean =storage.setOrCreate('PropA', 121); // true
523let res1: boolean =storage.setOrCreate('PropB', 111); // true
524let res2: boolean =storage.setOrCreate('PropB', undefined); // false
525```
526
527
528### link<sup>9+</sup>
529
530link&lt;T&gt;(propName: string): SubscribedAbstractProperty&lt;T&gt;
531
532Establishes two-way data binding with the given attribute in this **LocalStorage** instance. If the given attribute exists, the two-way bound data of the attribute in LocalStorage is returned.
533
534Any update of the data is synchronized back to LocalStorage, which then synchronizes the update to all data and custom components bound to the attribute.
535
536If the given attribute does not exist in LocalStorage, **undefined** is returned.
537
538Since API version 9, this API is supported in ArkTS widgets.
539
540**Parameters**
541
542| Name     | Type    | Mandatory  | Description              |
543| -------- | ------ | ---- | ------------------ |
544| propName | string | Yes   | Attribute name in LocalStorage.|
545
546**Return value**
547
548| Type                                 | Description                                      |
549| ----------------------------------- | ---------------------------------------- |
550| SubscribedAbstractProperty&lt;T&gt; | Returns the **SubscribedAbstractProperty<T>** instance if the given attribute exists in AppStorage; returns **undefined** otherwise.|
551
552
553```ts
554let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
555let linkToPropA1: SubscribedAbstractProperty<number> = storage.link('PropA');
556let linkToPropA2: SubscribedAbstractProperty<number> = storage.link('PropA'); // linkToPropA2.get() == 47
557linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48
558```
559
560
561### setAndLink<sup>9+</sup>
562
563setAndLink&lt;T&gt;(propName: string, defaultValue: T): SubscribedAbstractProperty&lt;T&gt;
564
565Works in a way similar to the **Link** API. If the given attribute exists in LocalStorage, the two-way bound data of the attribute in LocalStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in LocalStorage, and two-way bound data is returned.
566
567Since API version 9, this API is supported in ArkTS widgets.
568
569**Parameters**
570
571| Name         | Type    | Mandatory  | Description                                    |
572| ------------ | ------ | ---- | ---------------------------------------- |
573| propName     | string | Yes   | Attribute name in LocalStorage.                      |
574| defaultValue | T      | Yes   | Default value used to initialize the attribute with the specified attribute name in LocalStorage.|
575
576**Return value**
577
578| Type                                 | Description                                      |
579| ----------------------------------- | ---------------------------------------- |
580| SubscribedAbstractProperty&lt;T&gt; | Returns the **SubscribedAbstractProperty&lt;T&gt;** instance if the given attribute exists in LocalStorage; returns **undefined** otherwise.|
581
582
583```ts
584let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
585let link1: SubscribedAbstractProperty<number> = storage.setAndLink('PropB', 49); // Create PropB 49
586var link2: SubscribedAbstractProperty<number> = storage.setAndLink('PropA', 50); // PropA exists, remains 47
587```
588
589
590### prop<sup>9+</sup>
591
592prop&lt;S&gt;(propName: string): SubscribedAbstractProperty&lt;S&gt;
593
594Establishes one-way data binding with the given attribute in this **LocalStorage** instance. If the given attribute exists, the one-way bound data of the attribute in LocalStorage is returned. If the given attribute does not exist in LocalStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to LocalStorage.
595
596Since API version 9, this API is supported in ArkTS widgets.
597
598**Parameters**
599
600| Name     | Type    | Mandatory  | Description              |
601| -------- | ------ | ---- | ------------------ |
602| propName | string | Yes   | Attribute name in LocalStorage.|
603
604**Return value**
605
606| Type                                 | Description                                      |
607| ----------------------------------- | ---------------------------------------- |
608| SubscribedAbstractProperty&lt;S&gt; | Returns the **SubscribedAbstractProperty&lt;S&gt;** instance if the given attribute exists in LocalStorage; returns **undefined** otherwise.|
609
610
611```ts
612let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
613let prop1: SubscribedAbstractProperty<number> = storage.prop('PropA');
614let prop2: SubscribedAbstractProperty<number> = storage.prop('PropA');
615prop1.set(1); // one-way sync: prop1.get()=1; but prop2.get() == 47
616```
617
618
619### setAndProp<sup>9+</sup>
620
621setAndProp&lt;S&gt;(propName: string, defaultValue: S): SubscribedAbstractProperty&lt;S&gt;
622
623Establishes one-way data binding with the given attribute in this **LocalStorage** instance. If the given attribute exists, the one-way bound data of the attribute in LocalStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in LocalStorage, and one-way bound data is returned.
624
625Since API version 9, this API is supported in ArkTS widgets.
626
627**Parameters**
628
629| Name         | Type    | Mandatory  | Description                                    |
630| ------------ | ------ | ---- | ---------------------------------------- |
631| propName     | string | Yes   | Attribute name in LocalStorage.                      |
632| defaultValue | S      | Yes   | Default value used to initialize the attribute with the specified attribute name in LocalStorage.|
633
634**Return value**
635
636| Type                                 | Description                                      |
637| ----------------------------------- | ---------------------------------------- |
638| SubscribedAbstractProperty&lt;S&gt; | Instance of **SubscribedAbstractProperty&lt;T&gt;** and one-way bound data of the given attribute in LocalStorage.|
639
640
641```ts
642let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
643let prop: SubscribedAbstractProperty<number> = storage.setAndProp('PropB', 49); // PropA -> 47, PropB -> 49
644```
645
646
647### delete<sup>9+</sup>
648
649delete(propName: string): boolean
650
651Deletes the attribute with the specified attribute name from LocalStorage under the prerequisite that the attribute does not have a subscriber. If the deletion is successful, **true** is returned.
652
653The subscribers of the attribute are attributes with the same name bound to the **Link** and **Prop** APIs, **\@LocalStorageLink('propName')**, and **\@LocalStorageProp('propName')**. This means that if **\@LocalStorageLink('propName')** and **\@LocalStorageProp('propName')** are used in a custom component or if there is still a **SubscribedAbstractProperty** instance (return type of the **link** and **prop** APIs) in sync with the attribute, the attribute cannot be deleted from LocalStorage.
654
655Since API version 9, this API is supported in ArkTS widgets.
656
657**Parameters**
658
659| Name     | Type    | Mandatory  | Description              |
660| -------- | ------ | ---- | ------------------ |
661| propName | string | Yes   | Attribute name in LocalStorage.|
662
663**Return value**
664
665| Type     | Description                                      |
666| ------- | ---------------------------------------- |
667| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
668
669
670```ts
671let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
672storage.link('PropA');
673let res: boolean = storage.delete('PropA'); // false, PropA still has a subscriber
674let res1: boolean = storage.delete('PropB'); // false, PropB is not in storage
675storage.setOrCreate('PropB', 48);
676let res2: boolean = storage.delete('PropB'); // true, PropB is deleted from storage successfully
677```
678
679
680### keys<sup>9+</sup>
681
682keys(): IterableIterator&lt;string&gt;
683
684Obtains all attribute names in LocalStorage.
685
686Since API version 9, this API is supported in ArkTS widgets.
687
688**Return value**
689
690| Type                            | Description                  |
691| ------------------------------ | -------------------- |
692| IterableIterator&lt;string&gt; | All attribute names in LocalStorage.|
693
694
695```ts
696let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
697let keys: IterableIterator<string> = storage.keys();
698```
699
700
701### size<sup>9+</sup>
702
703size(): number
704
705Obtains the number of attributes in LocalStorage.
706
707Since API version 9, this API is supported in ArkTS widgets.
708
709**Return value**
710
711| Type    | Description       |
712| ------ | --------- |
713| number | Number of attributes in LocalStorage.|
714
715
716```ts
717let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
718let res: number = storage.size(); // 1
719```
720
721
722### clear<sup>9+</sup>
723
724clear(): boolean
725
726
727Deletes all attributes from LocalStorage under the prerequisite that none of the attributes has a subscriber. If any of the attributes has a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
728
729Since API version 9, this API is supported in ArkTS widgets.
730
731**Return value**
732
733
734| Type     | Description                                      |
735| ------- | ---------------------------------------- |
736| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
737
738
739
740```ts
741let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
742let res: boolean = storage.clear(); // true, there are no subscribers
743```
744
745
746## SubscribedAbstractProperty
747
748
749### get<sup>9+</sup>
750
751abstract get(): T
752
753Obtains attribute data synchronized from AppStorage or LocalStorage.
754
755Since API version 9, this API is supported in ArkTS widgets.
756
757**Return value**
758
759| Type  | Description                             |
760| ---- | ------------------------------- |
761| T    | Attribute data synchronized from AppStorage or LocalStorage.|
762
763
764```ts
765AppStorage.SetOrCreate('PropA', 47);
766let prop1 = AppStorage.Prop('PropA');
767prop1.get(); //  prop1.get()=47
768```
769
770
771### set<sup>9+</sup>
772
773abstract set(newValue: T): void
774
775Sets the attribute data synchronized from AppStorage or LocalStorage.
776
777Since API version 9, this API is supported in ArkTS widgets.
778
779
780**Parameters**
781
782
783| Name     | Type  | Mandatory  | Description   |
784| -------- | ---- | ---- | ------- |
785| newValue | T    | Yes   | Data to set.|
786
787
788
789```
790AppStorage.SetOrCreate('PropA', 47);
791let prop1 = AppStorage.Prop('PropA');
792prop1.set(1); //  prop1.get()=1
793```
794
795## PersistentStorage
796
797
798### PersistProp
799
800static PersistProp&lt;T&gt;(key: string, defaultValue: T): void
801
802Persists the attribute with the specified key in AppStorage to a file. This API is usually called before access to AppStorage.
803
804The sequence of determining the type and value of an attribute is as follows:
805
8061. If the PersistentStorage file contains the attribute with the specified key, an attribute with the key as the name is created in AppStorage and initialized with the attribute of the key found in PersistentStorage.
807
8082. If the attribute with the specified key is not found in the PersistentStorage file, AppStorage is searched for the attribute corresponding to the key. If the matching attribute is found, it is persisted.
809
8103. If no matching attribute is found in AppStorage, it is created in AppStorage, initialized with the value of **defaultValue**, and persisted.
811
812According to the preceding initialization process, if AppStorage contains the matching attribute, the value of this attribute is used to overwrite the value in the PersistentStorage file. Because AppStorage stores data in the memory, the attribute value becomes impersistent.
813
814**Parameters**
815
816| Name         | Type    | Mandatory  | Description                                    |
817| ------------ | ------ | ---- | ---------------------------------------- |
818| key          | string | Yes   | Attribute name.                                    |
819| defaultValue | T      | Yes   | Default value used to initialize the created attribute. The value cannot be **undefined** or **null**.|
820
821
822**Example:**
823
824
825
826```ts
827PersistentStorage.PersistProp('highScore', '0');
828```
829
830
831### DeleteProp
832
833static DeleteProp(key: string): void
834
835Performs the reverse operation of **PersistProp**. Specifically, this API deletes the attribute corresponding to the key from PersistentStorage. Subsequent AppStorage operations do not affect data in PersistentStorage.
836
837**Parameters**
838
839| Name | Type    | Mandatory  | Description                   |
840| ---- | ------ | ---- | ----------------------- |
841| key  | string | Yes   | Attribute name in PersistentStorage.|
842
843
844```ts
845PersistentStorage.DeleteProp('highScore');
846```
847
848
849### PersistProps
850
851static PersistProps(properties: {key: string, defaultValue: any;}[]): void
852
853Works in a way similar to the **PersistProp** API, with the difference that it allows for persistence in batches and is therefore ideal for initialization during application startup.
854
855**Parameters**
856
857| Name       | Type                                      | Mandatory  | Description                                    |
858| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
859| properties | {key:&nbsp;string,&nbsp;defaultValue:&nbsp;any}[] | Yes   | Array of attributes to persist.<br>**key**: attribute name.<br>**defaultValue**: default value. The rules are the same as those of **PersistProp**.|
860
861
862```ts
863PersistentStorage.PersistProps([{ key: 'highScore', defaultValue: '0' }, { key: 'wightScore', defaultValue: '1' }]);
864```
865
866
867### Keys
868
869static Keys(): Array&lt;string&gt;
870
871Obtains an array of keys for all persistent attributes.
872
873**Return value**
874
875| Type                 | Description               |
876| ------------------- | ----------------- |
877| Array&lt;string&gt; | Array of keys of all persistent attributes.|
878
879
880```ts
881let keys: Array<string> = PersistentStorage.Keys();
882```
883
884
885## Environment
886
887
888### EnvProp
889
890static EnvProp&lt;S&gt;(key: string, value: S): boolean
891
892Saves the built-in environment variable key in environment to AppStorage. If the value of the environment variable key is not found in AppStorage, the default value is used. If the value is successfully saved, **true** is returned. If the value of the environment variable key is found in AppStorage, **false** is returned.
893
894You are advised to call this API when the application is started.
895
896It is incorrect to use AppStorage to read environment variables without invoking **EnvProp**.
897
898**Parameters**
899
900| Name  | Type    | Mandatory  | Description                                   |
901| ----- | ------ | ---- | --------------------------------------- |
902| key   | string | Yes   | Environment variable name. For details about the value range, see [Built-in Environment Variables](#built-in-environment-variables).   |
903| value | S      | Yes   | Default value used if the value of the environment variable key is not found in AppStorage.|
904
905**Return value**
906
907| Type     | Description                                      |
908| ------- | ---------------------------------------- |
909| boolean | Returns **false** if the attribute corresponding to the key exists in AppStorage; returns **false** otherwise.|
910
911**Example:**
912
913
914```ts
915Environment.EnvProp('accessibilityEnabled', 'default');
916```
917
918
919### Built-in Environment Variables
920
921| key                  | Type             | Description                                      |
922| -------------------- | --------------- | ---------------------------------------- |
923| accessibilityEnabled | string          | Whether to enable accessibility.                            |
924| colorMode            | ColorMode       | Color mode. The options are as follows:<br>- **ColorMode.LIGHT**: light mode.<br>- **ColorMode.DARK**: dark mode.|
925| fontScale            | number          | Font scale.                                 |
926| fontWeightScale      | number          | Font weight scale.                                   |
927| layoutDirection      | LayoutDirection | Layout direction. The options are as follows:<br>- **LayoutDirection.LTR**: from left to right.<br>- **LayoutDirection.RTL**: from right to left.|
928| languageCode         | string          | Current system language. The value is in lowercase, for example, **zh**.                       |
929
930
931### EnvProps
932
933static EnvProps(props: {key: string; defaultValue: any;}[]): void
934
935Works in a way similar to the **EnvProp** API, with the difference that it allows for initialization of multiple attributes in batches. You are advised to call this API during application startup to save system environment variables to AppStorage in batches.
936
937**Parameters**
938
939| Name  | Type                                      | Mandatory  | Description              |
940| ----- | ---------------------------------------- | ---- | ------------------ |
941| props | {key:&nbsp;string,&nbsp;defaultValue:&nbsp;any}[] | Yes   | Array of key-value pairs consisting of system environment variables and default values.|
942
943
944```ts
945Environment.EnvProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, {
946  key: 'languageCode',
947  defaultValue: 'en'
948}, { key: 'prop', defaultValue: 'hhhh' }]);
949```
950
951
952### Keys
953
954static Keys(): Array&lt;string&gt;
955
956Array of keys of environment variables.
957
958**Return value**
959
960| Type                 | Description         |
961| ------------------- | ----------- |
962| Array&lt;string&gt; | Returns an array of associated system attributes.|
963
964
965```ts
966Environment.EnvProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, {
967  key: 'languageCode',
968  defaultValue: 'en'
969}, { key: 'prop', defaultValue: 'hhhh' }]);
970
971let keys: Array<string> = Environment.Keys(); // accessibilityEnabled, languageCode, prop
972```
973