1# State Management with Application-level Variables 2 3The state management module provides data storage, persistent data management, UIAbility data storage, and environment state required by applications. 4 5>**NOTE** 6> 7>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. 8 9T and S in this topic represent the types as described below. 10 11| Type | Description | 12| ---- | -------------------------------------- | 13| T | Class, number, boolean, string, and arrays of these types.| 14| S | number, boolean, string. | 15 16## AppStorage 17 18For details about how to use AppStorage, see [AppStorage: Storing Application-wide UI State](../../../ui/state-management/arkts-appstorage.md). 19 20**Atomic service API**: This API can be used in atomic services since API version 11. 21 22**System capability**: SystemCapability.ArkUI.ArkUI.Full 23 24### ref<sup>12+</sup> 25 26static ref\<T\>(propName: string): AbstractProperty\<T\> | undefined 27 28Returns a reference to the data corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the provided **propName** does not exist, this API returns **undefined**. 29 30This API is similar to [link](#link10) but does not require manually releasing the returned variable of the [AbstractProperty](#abstractproperty) type. 31 32**Atomic service API**: This API can be used in atomic services since API version 12. 33 34**System capability**: SystemCapability.ArkUI.ArkUI.Full 35 36**Parameters** 37 38| Name | Type | Mandatory| Description | 39| -------- | ------ | ---- | ---------------------- | 40| propName | string | Yes | Property name in AppStorage.| 41 42**Return value** 43 44| Type | Description | 45| -------------------------------------- | ------------------------------------------------------------ | 46| [AbstractProperty<T>](#abstractproperty) \| undefined | A reference to the property in AppStorage, or **undefined** if the property does not exist.| 47 48**Example** 49 50```ts 51AppStorage.setOrCreate('PropA', 47); 52let refToPropA1: AbstractProperty<number> | undefined = AppStorage.ref('PropA'); 53let refToPropA2: AbstractProperty<number> | undefined = AppStorage.ref('PropA'); // refToPropA2.get() == 47 54refToPropA1?.set(48); // Synchronously updates AppStorage: refToPropA1.get() == refToPropA2.get() == 48. 55``` 56 57### setAndRef<sup>12+</sup> 58 59static setAndRef<T>(propName: string, defaultValue: T): AbstractProperty<T> 60 61Similar to the [ref](#ref12) API, returns a reference to the data corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If **propName** does not exist, this API creates and initializes the property in AppStorage using **defaultValue** and returns its reference. **defaultValue** must be of the **T** type and can be **null** or **undefined**. 62 63This API is similar to [setAndLink](#setandlink10) but does not require manually releasing the returned variable of the [AbstractProperty](#abstractproperty) type. 64 65> **NOTE** 66> 67> Since API version 12, AppStorage supports [Map](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-appstorage.md#using-union-types-in-appstorage) types. 68 69**Atomic service API**: This API can be used in atomic services since API version 12. 70 71**System capability**: SystemCapability.ArkUI.ArkUI.Full 72 73**Parameters** 74 75| Name | Type | Mandatory| Description | 76| ------------ | ------ | ---- | ------------------------------------------------------------ | 77| propName | string | Yes | Property name in AppStorage. | 78| defaultValue | T | Yes | Default value used to initialize **propName** in AppStorage if it does not exist. The value can be **null** or **undefined**.| 79 80**Return value** 81 82| Type | Description | 83| ------------------------- | ------------------------------------------------------------ | 84| [AbstractProperty<T>](#abstractproperty) | Instance of **AbstractProperty<T>**, which is a reference to the property in AppStorage corresponding to **propName**.| 85 86**Example** 87 88```ts 89AppStorage.setOrCreate('PropA', 47); 90let ref1: AbstractProperty<number> = AppStorage.setAndRef('PropB', 49); // Create PropB with the default value 49. 91let ref2: AbstractProperty<number> = AppStorage.setAndRef('PropA', 50); // PropA already exists with the value 47. 92``` 93 94### link<sup>10+</sup> 95 96static link<T>(propName: string): SubscribedAbstractProperty<T> 97 98Establishes a two-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, the two-way bound data of the property in AppStorage is returned. 99 100Any update of the data is synchronized back to AppStorage, which then synchronizes the update to all data and custom components bound to the property. 101 102If the given property does not exist in AppStorage, **undefined** is returned. 103 104**Atomic service API**: This API can be used in atomic services since API version 11. 105 106**System capability**: SystemCapability.ArkUI.ArkUI.Full 107 108**Parameters** 109 110| Name | Type | Mandatory | Description | 111| -------- | ------ | ---- | ---------------- | 112| propName | string | Yes | Property name in AppStorage.| 113 114**Return value** 115 116| Type | Description | 117| ----------------------------------- | ------------------------------------------------------------ | 118| [SubscribedAbstractProperty<T>](#subscribedabstractproperty) | Two-way bound data of the specified property in AppStorage, or **undefined** if the property does not exist.| 119 120**Example** 121```ts 122AppStorage.setOrCreate('PropA', 47); 123let linkToPropA1: SubscribedAbstractProperty<number> = AppStorage.link('PropA'); 124let linkToPropA2: SubscribedAbstractProperty<number> = AppStorage.link('PropA'); // linkToPropA2.get() == 47 125linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48 126``` 127 128### setAndLink<sup>10+</sup> 129 130static setAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T> 131 132Similar to the [link](#link10) API, establishes a two-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, this API returns the two-way bound data for the property. If the given property does not exist, this API creates and initializes the property in AppStorage using **defaultValue** and returns its two-way bound data. The value of **defaultValue** must be of the **T** type. Since API version 12, it can be **null** or **undefined**. 133 134> **NOTE** 135> 136> Since API version 12, AppStorage supports [Map](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-appstorage.md#using-union-types-in-appstorage) types. 137 138**Atomic service API**: This API can be used in atomic services since API version 11. 139 140**System capability**: SystemCapability.ArkUI.ArkUI.Full 141 142**Parameters** 143 144| Name | Type | Mandatory| Description | 145| ------------ | ------ | ---- | ------------------------------------------------------------ | 146| propName | string | Yes | Property name in AppStorage. | 147| defaultValue | T | Yes | Default value used to initialize **propName** in AppStorage if it does not exist. Since API version 12, **defaultValue** can be **null** or **undefined**.| 148 149**Return value** 150 151| Type | Description | 152| ----------------------------------- | ---------------------------------------- | 153| [SubscribedAbstractProperty<T>](#subscribedabstractproperty) | Instance of **SubscribedAbstractProperty<T>**, which is two-way bound data of the given property in AppStorage.| 154 155**Example** 156```ts 157AppStorage.setOrCreate('PropA', 47); 158let link1: SubscribedAbstractProperty<number> = AppStorage.setAndLink('PropB', 49); // Create PropB with the default value 49. 159let link2: SubscribedAbstractProperty<number> = AppStorage.setAndLink('PropA', 50); // PropA already exists with the value 47. 160``` 161 162### prop<sup>10+</sup> 163 164static prop<T>(propName: string): SubscribedAbstractProperty<T> 165 166Establishes a one-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, the one-way bound data of the property in AppStorage is returned. If the given property does not exist in AppStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to AppStorage. 167 168**Atomic service API**: This API can be used in atomic services since API version 11. 169 170**System capability**: SystemCapability.ArkUI.ArkUI.Full 171 172**Parameters** 173 174| Name | Type | Mandatory | Description | 175| -------- | ------ | ---- | ---------------- | 176| propName | string | Yes | Property name in AppStorage.| 177 178**Return value** 179 180| Type | Description | 181| ----------------------------------- | ------------------------------------------------------------ | 182| [SubscribedAbstractProperty<T>](#subscribedabstractproperty) | One-way bound data of the specified property in AppStorage, or **undefined** if the property does not exist.| 183 184**Example** 185 186```ts 187AppStorage.setOrCreate('PropA', 47); 188let prop1: SubscribedAbstractProperty<number> = AppStorage.prop('PropA'); 189let prop2: SubscribedAbstractProperty<number> = AppStorage.prop('PropA'); 190prop1.set(1); // One-way synchronization: prop1.get() returns 1, while prop2.get() returns 47. 191``` 192 193### setAndProp<sup>10+</sup> 194 195static setAndProp<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T> 196 197Similar to the [prop](#prop10) API, establishes a one-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, this API returns the one-way bound data for the property. If the given property does not exist, this API creates and initializes the property in AppStorage using **defaultValue** and returns its one-way bound data. The value of **defaultValue** must be of the **T** type. Since API version 12, it can be **null** or **undefined**. 198 199> **NOTE** 200> 201> Since API version 12, AppStorage supports [Map](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-appstorage.md#using-union-types-in-appstorage) types. 202 203**Atomic service API**: This API can be used in atomic services since API version 11. 204 205**System capability**: SystemCapability.ArkUI.ArkUI.Full 206 207**Parameters** 208 209| Name | Type | Mandatory| Description | 210| ------------ | ------ | ---- | ------------------------------------------------------------ | 211| propName | string | Yes | Property name in AppStorage. | 212| defaultValue | T | Yes | Default value used to initialize **propName** in AppStorage if it does not exist. Since API version 12, **defaultValue** can be **null** or **undefined**.| 213 214**Return value** 215 216| Type | Description | 217| ----------------------------------- | --------------------------------------- | 218| [SubscribedAbstractProperty<T>](#subscribedabstractproperty) | Instance of **SubscribedAbstractProperty<T>**.| 219 220**Example** 221```ts 222AppStorage.setOrCreate('PropA', 47); 223let prop: SubscribedAbstractProperty<number> = AppStorage.setAndProp('PropB', 49); // PropA -> 47, PropB -> 49 224``` 225 226### has<sup>10+</sup> 227 228static has(propName: string): boolean 229 230Checks whether the property corresponding to **propName** exists in [AppStorage](../../../ui/state-management/arkts-appstorage.md). 231 232**Atomic service API**: This API can be used in atomic services since API version 11. 233 234**System capability**: SystemCapability.ArkUI.ArkUI.Full 235 236**Parameters** 237 238| Name | Type | Mandatory | Description | 239| -------- | ------ | ---- | ---------------- | 240| propName | string | Yes | Property name in AppStorage.| 241 242**Return value** 243 244| Type | Description | 245| ------- | ---------------------------------------- | 246| boolean | Returns **true** if the property exists in AppStorage; returns **false** otherwise.| 247 248**Example** 249```ts 250AppStorage.has('simpleProp'); 251``` 252 253### get<sup>10+</sup> 254 255static get<T>(propName: string): T | undefined 256 257Obtains the value of the property corresponding to **propName** from [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the property does not exist, this API returns **undefined**. 258 259**Atomic service API**: This API can be used in atomic services since API version 11. 260 261**System capability**: SystemCapability.ArkUI.ArkUI.Full 262 263**Parameters** 264 265| Name | Type | Mandatory | Description | 266| -------- | ------ | ---- | ---------------- | 267| propName | string | Yes | Property name in AppStorage.| 268 269**Return value** 270 271| Type | Description | 272| ------------------------ | ----------------------------------------------------------- | 273| T \| undefined | Value of the property corresponding to **propName** in AppStorage, or **undefined** if it does not exist.| 274 275**Example** 276```ts 277AppStorage.setOrCreate('PropA', 47); 278let value: number = AppStorage.get('PropA') as number; // 47 279``` 280 281### set<sup>10+</sup> 282 283static set<T>(propName: string, newValue: T): boolean 284 285Sets the value of the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the value of **newValue** is the same as the current value of the property, no assignment is performed, and the state variable does not instruct the UI to update the value of the property. Since API version 12, **newValue** can be **null** or **undefined**. 286 287> **NOTE** 288> 289> Since API version 12, AppStorage supports [Map](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-appstorage.md#using-union-types-in-appstorage) types. 290 291**Atomic service API**: This API can be used in atomic services since API version 11. 292 293**System capability**: SystemCapability.ArkUI.ArkUI.Full 294 295**Parameters** 296 297| Name | Type | Mandatory | Description | 298| -------- | ------ | ---- | ---------------------- | 299| propName | string | Yes | Property name in AppStorage. | 300| newValue | T | Yes | Property value. Since API version 12, the value can be **null** or **undefined**.| 301 302**Return value** 303 304| Type | Description | 305| ------- | ------------------------------------------------------------ | 306| boolean | Returns **false** if the property corresponding to **propName** does not exist in AppStorage or if the assignment fails. Returns **true** if the assignment is successful.| 307 308**Example** 309```ts 310AppStorage.setOrCreate('PropA', 48); 311let res: boolean = AppStorage.set('PropA', 47) // true 312let res1: boolean = AppStorage.set('PropB', 47) // false 313``` 314 315### setOrCreate<sup>10+</sup> 316 317static setOrCreate<T>(propName: string, newValue: T): void 318 319Sets the value of the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md) to a new value, if the property exists and the new value is different from the current value. If the new value is the same as the current value of the property, no assignment is performed, and the state variable does not instruct the UI to update the value of the property. 320If the property does not exist, this API creates it with the value of **newValue**. This **setOrCreate** API can create only one AppStorage key-value pair each time. To create multiple key-value pairs, call this API multiple times. Since API version 12, **newValue** can be **null** or **undefined**. 321 322> **NOTE** 323> 324> Since API version 12, AppStorage supports [Map](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-appstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-appstorage.md#using-union-types-in-appstorage) types. 325 326**Atomic service API**: This API can be used in atomic services since API version 11. 327 328**System capability**: SystemCapability.ArkUI.ArkUI.Full 329 330**Parameters** 331 332| Name | Type | Mandatory | Description | 333| -------- | ------ | ---- | ---------------------- | 334| propName | string | Yes | Property name in AppStorage. | 335| newValue | T | Yes | Property value. Since API version 12, the value can be **null** or **undefined**.| 336 337**Example** 338```ts 339AppStorage.setOrCreate('simpleProp', 121); 340``` 341 342### delete<sup>10+</sup> 343 344static delete(propName: string): boolean 345 346Deletes the property corresponding to **propName** from [AppStorage](../../../ui/state-management/arkts-appstorage.md). 347 348The deletion is only successful if the property has no subscribers. If there is a subscriber, the deletion fails and **false** is returned. If the deletion is successful, **true** is returned. 349 350The property subscribers include the following: 351 3521. Variables decorated by [\@StorageLink](../../../ui/state-management/arkts-appstorage.md#storagelink) or [\@StorageProp](../../../ui/state-management/arkts-appstorage.md#storageprop) 353 3542. Instances of [SubscribedAbstractProperty](#subscribedabstractproperty) returned by [link](#link10), [prop](#prop10), [setAndLink](#setandlink10), or [setAndProp](#setandprop10) 355 356To delete these subscribers: 357 3581. Remove the custom component containing \@StorageLink or \@StorageProp. For details, see [Custom Component Deletion](../../../ui/state-management/arkts-page-custom-components-lifecycle.md#custom-component-deletion). 359 3602. Call the [aboutToBeDeleted](#abouttobedeleted10) API on instances of **SubscribedAbstractProperty** returned by **link**, **prop**, **setAndLink**, or **setAndProp**. 361 362**Atomic service API**: This API can be used in atomic services since API version 11. 363 364**System capability**: SystemCapability.ArkUI.ArkUI.Full 365 366**Parameters** 367 368| Name | Type | Mandatory | Description | 369| -------- | ------ | ---- | ---------------- | 370| propName | string | Yes | Property name in AppStorage.| 371 372**Return value** 373 374| Type | Description | 375| ------- | ---------------------------------------- | 376| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 377 378**Example** 379```ts 380AppStorage.setOrCreate('PropA', 47); 381AppStorage.link<number>('PropA'); 382let res: boolean = AppStorage.delete('PropA'); // false: PropA still has subscribers. 383 384AppStorage.setOrCreate('PropB', 48); 385let res1: boolean = AppStorage.delete('PropB'); // true: PropB is successfully deleted from AppStorage. 386``` 387 388### keys<sup>10+</sup> 389 390static keys(): IterableIterator<string> 391 392Obtains all property names in [AppStorage](../../../ui/state-management/arkts-appstorage.md). 393 394**Atomic service API**: This API can be used in atomic services since API version 11. 395 396**System capability**: SystemCapability.ArkUI.ArkUI.Full 397 398**Return value** 399 400| Type | Description | 401| ------------------------------ | ------------------ | 402| IterableIterator<string> | All property names in AppStorage.| 403 404**Example** 405```ts 406AppStorage.setOrCreate('PropB', 48); 407let keys: IterableIterator<string> = AppStorage.keys(); 408``` 409 410### clear<sup>10+</sup> 411 412static clear(): boolean 413 414Deletes all properties from [AppStorage](../../../ui/state-management/arkts-appstorage.md). The deletion is only successful if none of the properties in AppStorage have any subscribers. If there are subscribers, this API does not take effect and **false** is returned. If the deletion is successful, **true** is returned. 415 416For details about the subscriber, see [delete](#delete10). 417 418**Atomic service API**: This API can be used in atomic services since API version 11. 419 420**System capability**: SystemCapability.ArkUI.ArkUI.Full 421 422**Return value** 423 424| Type | Description | 425| ------- | ------------------------------------------------------------ | 426| boolean | Returns **true** if the properties in AppStorage have no subscribers and the deletion is successful; returns **false** if there are still subscribers.| 427 428**Example** 429```ts 430AppStorage.setOrCreate('PropA', 47); 431let res: boolean = AppStorage.clear(); // true: There are no subscribers. 432``` 433 434### size<sup>10+</sup> 435 436static size(): number 437 438Obtains the number of properties in [AppStorage](../../../ui/state-management/arkts-appstorage.md). 439 440**Atomic service API**: This API can be used in atomic services since API version 11. 441 442**System capability**: SystemCapability.ArkUI.ArkUI.Full 443 444**Return value** 445 446| Type | Description | 447| ------ | ------------------- | 448| number | Number of properties in AppStorage.| 449 450**Example** 451```ts 452AppStorage.setOrCreate('PropB', 48); 453let res: number = AppStorage.size(); // 1 454``` 455 456### Link<sup>(deprecated)</sup> 457 458static Link(propName: string): any 459 460Establishes a two-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, the two-way bound data of the property in AppStorage is returned. 461 462Any update of the data is synchronized back to AppStorage, which then synchronizes the update to all data and custom components bound to the property. 463 464If the given property does not exist in AppStorage, **undefined** is returned. 465 466> **NOTE** 467> 468> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [link10+](#link10) instead. 469 470**System capability**: SystemCapability.ArkUI.ArkUI.Full 471 472**Parameters** 473 474| Name | Type | Mandatory | Description | 475| -------- | ------ | ---- | ---------------- | 476| propName | string | Yes | Property name in AppStorage.| 477 478**Return value** 479 480| Type | Description | 481| -------------------------------- | ------------------------------------------------------------ | 482| any | Two-way bound data of the specified property in AppStorage, or **undefined** if the property does not exist.| 483 484**Example** 485```ts 486AppStorage.SetOrCreate('PropA', 47); 487let linkToPropA1: SubscribedAbstractProperty<number> = AppStorage.Link('PropA'); 488let linkToPropA2: SubscribedAbstractProperty<number> = AppStorage.Link('PropA'); // linkToPropA2.get() == 47 489linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48 490``` 491 492### SetAndLink<sup>(deprecated)</sup> 493 494static SetAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T> 495 496Similar to the [Link](#linkdeprecated) API, establishes a two-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, this API returns the two-way bound data for the property. If the given property does not exist, this API creates and initializes the property in AppStorage using **defaultValue** and returns its two-way bound data. The value of **defaultValue** must be of the **T** type and cannot be **null** or **undefined**. 497 498> **NOTE** 499> 500> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [setAndLink10+](#setandlink10) instead. 501 502**System capability**: SystemCapability.ArkUI.ArkUI.Full 503 504**Parameters** 505 506| Name | Type | Mandatory| Description | 507| ------------ | ------ | ---- | ------------------------------------------------------------ | 508| propName | string | Yes | Property name in AppStorage. | 509| defaultValue | T | Yes | Default value used to initialize **propName** in AppStorage if it does not exist. The value cannot be **null** or **undefined**.| 510 511**Return value** 512 513| Type | Description | 514| ----------------------------------- | ---------------------------------------- | 515| [SubscribedAbstractProperty<T>](#subscribedabstractproperty) | Instance of **SubscribedAbstractProperty<T>** and two-way bound data of the given property in AppStorage.| 516 517**Example** 518```ts 519AppStorage.SetOrCreate('PropA', 47); 520let link1: SubscribedAbstractProperty<number> = AppStorage.SetAndLink('PropB', 49); // Create PropB with the default value 49. 521let link2: SubscribedAbstractProperty<number> = AppStorage.SetAndLink('PropA', 50); // PropA already exists with the value 47. 522``` 523 524### Prop<sup>(deprecated)</sup> 525 526static Prop(propName: string): any 527 528Establishes a one-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, the one-way bound data of the property in AppStorage is returned. If the given property does not exist in AppStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to AppStorage. 529 530> **NOTE** 531> 532> Prop supports only simple types. 533> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [prop10+](#prop10) instead. 534 535**System capability**: SystemCapability.ArkUI.ArkUI.Full 536 537**Parameters** 538 539| Name | Type | Mandatory | Description | 540| -------- | ------ | ---- | ---------------- | 541| propName | string | Yes | Property name in AppStorage.| 542 543**Return value** 544 545| Type | Description | 546| -------------------------------- | ------------------------------------------------------------ | 547| any | One-way bound data of the specified property in AppStorage, or **undefined** if the property does not exist.| 548 549**Example** 550```ts 551AppStorage.SetOrCreate('PropA', 47); 552let prop1: SubscribedAbstractProperty<number> = AppStorage.Prop('PropA'); 553let prop2: SubscribedAbstractProperty<number> = AppStorage.Prop('PropA'); 554prop1.set(1); // One-way synchronization: prop1.get() returns 1, while prop2.get() returns 47. 555``` 556 557### SetAndProp<sup>(deprecated)</sup> 558 559static SetAndProp<S>(propName: string, defaultValue: S): SubscribedAbstractProperty<S> 560 561Similar to the [Prop](#propdeprecated) API, establishes a one-way data binding with the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the given property exists in AppStorage, this API returns the one-way bound data for the property. If the given property does not exist, this API creates and initializes the property in AppStorage using **defaultValue** and returns its one-way bound data. The value of **defaultValue** must be of the **S** type and cannot be **null** or **undefined**. 562 563> **NOTE** 564> 565> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [setAndProp10+](#setandprop10) instead. 566 567**System capability**: SystemCapability.ArkUI.ArkUI.Full 568 569**Parameters** 570 571| Name | Type | Mandatory| Description | 572| ------------ | ------ | ---- | ------------------------------------------------------------ | 573| propName | string | Yes | Property name in AppStorage. | 574| defaultValue | S | Yes | Default value used to initialize **propName** in AppStorage if it does not exist. The value cannot be **null** or **undefined**.| 575 576**Return value** 577 578| Type | Description | 579| ----------------------------------- | --------------------------------------- | 580| [SubscribedAbstractProperty<S>](#subscribedabstractproperty) | Instance of **SubscribedAbstractProperty<S>**.| 581 582**Example** 583```ts 584AppStorage.SetOrCreate('PropA', 47); 585let prop: SubscribedAbstractProperty<number> = AppStorage.SetAndProp('PropB', 49); // PropA -> 47, PropB -> 49 586``` 587 588### Has<sup>(deprecated)</sup> 589 590static Has(propName: string): boolean 591 592Checks whether the property corresponding to **propName** exists in [AppStorage](../../../ui/state-management/arkts-appstorage.md). 593 594> **NOTE** 595> 596> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [has10+](#has10) instead. 597 598**System capability**: SystemCapability.ArkUI.ArkUI.Full 599 600**Parameters** 601 602| Name | Type | Mandatory | Description | 603| -------- | ------ | ---- | ---------------- | 604| propName | string | Yes | Property name in AppStorage.| 605 606**Return value** 607 608| Type | Description | 609| ------- | ---------------------------------------- | 610| boolean | Returns **true** if the property exists in AppStorage; returns **false** otherwise.| 611 612**Example** 613```ts 614AppStorage.Has('simpleProp'); 615``` 616 617### Get<sup>(deprecated)</sup> 618 619static Get<T>(propName: string): T | undefined 620 621Obtains the value of the property corresponding to **propName** from [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the property does not exist, this API returns **undefined**. 622 623> **NOTE** 624> 625> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [get10+](#get10) instead. 626 627**System capability**: SystemCapability.ArkUI.ArkUI.Full 628 629**Parameters** 630 631| Name | Type | Mandatory | Description | 632| -------- | ------ | ---- | ---------------- | 633| propName | string | Yes | Property name in AppStorage.| 634 635**Return value** 636 637| Type | Description | 638| ------------------------ | ------------------------------------------------------------ | 639| T \| undefined | Value of the property corresponding to **propName** in AppStorage, or **undefined** if it does not exist.| 640 641**Example** 642```ts 643AppStorage.SetOrCreate('PropA', 47); 644let value: number = AppStorage.Get('PropA') as number; // 47 645``` 646 647### Set<sup>(deprecated)</sup> 648 649static Set<T>(propName: string, newValue: T): boolean 650 651Sets the value of the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md). 652 653> **NOTE** 654> 655> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [set10+](#set10) instead. 656 657**System capability**: SystemCapability.ArkUI.ArkUI.Full 658 659**Parameters** 660 661| Name | Type | Mandatory| Description | 662| -------- | ------ | ---- | ------------------------------- | 663| propName | string | Yes | Property name in AppStorage. | 664| newValue | T | Yes | Property value, which cannot be **null** or **undefined**.| 665 666**Return value** 667 668| Type | Description | 669| ------- | ------------------------------------------------------------ | 670| boolean | Returns **true** if the operation is successful; returns **false** if the property corresponding to **propName** does not exist in AppStorage, or the value to set is **undefined** or **null**. | 671 672**Example** 673```ts 674AppStorage.SetOrCreate('PropA', 48); 675let res: boolean = AppStorage.Set('PropA', 47) // true 676let res1: boolean = AppStorage.Set('PropB', 47) // false 677``` 678 679### SetOrCreate<sup>(deprecated)</sup> 680 681static SetOrCreate<T>(propName: string, newValue: T): void 682 683Sets the value of the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md) to a new value, if the property exists. If the property does not exist, this API creates it with the value of **newValue**. 684 685The value of **newValue** cannot be **null** or **undefined**. 686 687> **NOTE** 688> 689> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [setOrCreate10+](#setorcreate10) instead. 690 691**System capability**: SystemCapability.ArkUI.ArkUI.Full 692 693**Parameters** 694 695| Name | Type | Mandatory| Description | 696| -------- | ------ | ---- | ------------------------------- | 697| propName | string | Yes | Property name in AppStorage. | 698| newValue | T | Yes | Property value, which cannot be **null** or **undefined**.| 699 700**Example** 701```ts 702AppStorage.SetOrCreate('simpleProp', 121); 703``` 704 705### Delete<sup>(deprecated)</sup> 706 707static Delete(propName: string): boolean 708 709Deletes the property corresponding to **propName** from [AppStorage](../../../ui/state-management/arkts-appstorage.md). 710 711The deletion is only successful if the property has no subscribers. If there is a subscriber, the deletion fails and **false** is returned. If the deletion is successful, **true** is returned. 712 713Subscribers include properties bound using [Link](#linkdeprecated) and [Prop](#propdeprecated) APIs, as well as those decorated with [\@StorageLink('propName')](../../../ui/state-management/arkts-appstorage.md#storagelink) and [\@StorageProp('propName')](../../../ui/state-management/arkts-appstorage.md#storageprop). This means that if @StorageLink('propName') and @StorageProp('propName') are used in a custom component or if there is still a **SubscribedAbstractProperty** instance in a synchronization relationship with the property, the property cannot be deleted from AppStorage. 714 715> **NOTE** 716> 717> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [delete10+](#delete10) instead. 718 719**System capability**: SystemCapability.ArkUI.ArkUI.Full 720 721**Parameters** 722 723| Name | Type | Mandatory | Description | 724| -------- | ------ | ---- | ---------------- | 725| propName | string | Yes | Property name in AppStorage.| 726 727**Return value** 728 729| Type | Description | 730| ------- | ---------------------------------------- | 731| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 732 733**Example** 734```ts 735AppStorage.SetOrCreate('PropA', 47); 736AppStorage.Link('PropA'); 737let res: boolean = AppStorage.Delete('PropA'); // false: PropA still has subscribers. 738 739AppStorage.SetOrCreate('PropB', 48); 740let res1: boolean = AppStorage.Delete('PropB'); // true: PropB is successfully deleted from AppStorage. 741``` 742 743### Keys<sup>(deprecated)</sup> 744 745static Keys(): IterableIterator<string> 746 747Obtains all property names in [AppStorage](../../../ui/state-management/arkts-appstorage.md). 748 749> **NOTE** 750> 751> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [keys10+](#keys10) instead. 752 753**System capability**: SystemCapability.ArkUI.ArkUI.Full 754 755**Return value** 756 757| Type | Description | 758| ------------------------------ | ------------------ | 759| IterableIterator<string> | All property names in AppStorage.| 760 761**Example** 762```ts 763AppStorage.SetOrCreate('PropB', 48); 764let keys: IterableIterator<string> = AppStorage.Keys(); 765``` 766 767 768### staticClear<sup>(deprecated)</sup> 769 770static staticClear(): boolean 771 772Deletes all properties. 773 774> **NOTE** 775> 776> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [clear10+](#clear10) instead. 777 778**System capability**: SystemCapability.ArkUI.ArkUI.Full 779 780**Return value** 781 782| Type | Description | 783| ------- | --------------------------------- | 784| boolean | Deletes all properties. Returns **true** if all properties are deleted; returns **false** if any of the properties is being referenced by a state variable.| 785 786**Example** 787```ts 788let simple = AppStorage.staticClear(); 789``` 790 791 792### Clear<sup>(deprecated)</sup> 793 794static Clear(): boolean 795 796Deletes all properties from [AppStorage](../../../ui/state-management/arkts-appstorage.md). The deletion is only successful if none of the properties in AppStorage have any subscribers. If there are subscribers, this API does not take effect and **false** is returned. If the deletion is successful, **true** is returned. 797 798For details about the subscriber, see [delete](#delete10). 799 800> **NOTE** 801> 802> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [clear10+](#clear10) instead. 803 804**System capability**: SystemCapability.ArkUI.ArkUI.Full 805 806**Return value** 807 808| Type | Description | 809| ------- | ------------------------------------------------------------ | 810| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 811 812**Example** 813```typescript 814AppStorage.SetOrCreate('PropA', 47); 815let res: boolean = AppStorage.Clear(); // true: There are no subscribers. 816``` 817 818 819### IsMutable<sup>(deprecated)</sup> 820 821static IsMutable(propName: string): boolean 822 823Checks whether the property corresponding to **propName** in [AppStorage](../../../ui/state-management/arkts-appstorage.md) is mutable. 824 825> **NOTE** 826> 827> This API is supported since API version 7 and deprecated since API version 10. 828 829**System capability**: SystemCapability.ArkUI.ArkUI.Full 830 831**Parameters** 832 833| Name | Type | Mandatory | Description | 834| -------- | ------ | ---- | ---------------- | 835| propName | string | Yes | Property name in AppStorage.| 836 837**Return value** 838 839| Type | Description | 840| ------- | -------------------------------- | 841| boolean | Whether the property corresponding to **propName** is mutable. Currently, this return value is always **true**.| 842 843**Example** 844```ts 845AppStorage.SetOrCreate('PropA', 47); 846let res: boolean = AppStorage.IsMutable('simpleProp'); 847``` 848 849 850### Size<sup>(deprecated)</sup> 851 852static Size(): number 853 854Obtains the number of properties in [AppStorage](../../../ui/state-management/arkts-appstorage.md). 855 856> **NOTE** 857> 858> This API is supported since API version 7 and deprecated since API version 10. You are advised to use [size10+](#size10) instead. 859 860**System capability**: SystemCapability.ArkUI.ArkUI.Full 861 862**Return value** 863 864| Type | Description | 865| ------ | ------------------- | 866| number | Number of properties in AppStorage.| 867 868**Example** 869```ts 870AppStorage.SetOrCreate('PropB', 48); 871let res: number = AppStorage.Size(); // 1 872``` 873 874 875## LocalStorage<sup>9+</sup> 876 877 878For details about how to use LocalStorage on the UI, see [LocalStorage: UI State Storage](../../../ui/state-management/arkts-localstorage.md). 879 880**Widget capability**: This API can be used in ArkTS widgets since API version 9. 881 882**Atomic service API**: This API can be used in atomic services since API version 11. 883 884**System capability**: SystemCapability.ArkUI.ArkUI.Full 885 886### constructor<sup>9+</sup> 887 888constructor(initializingProperties?: Object) 889 890Creates a [LocalStorage](../../../ui/state-management/arkts-localstorage.md) instance and initializes it using the properties and values returned by **Object.keys(initializingProperties)**. 891 892**Widget capability**: This API can be used in ArkTS widgets since API version 9. 893 894**Atomic service API**: This API can be used in atomic services since API version 11. 895 896**System capability**: SystemCapability.ArkUI.ArkUI.Full 897 898**Parameters** 899 900| Name | Type | Mandatory | Description | 901| ---------------------- | ------ | ---- | ---------------------------------------- | 902| initializingProperties | Object | No | Properties and values used to initialize the **LocalStorage** instance. The value cannot be **undefined**.| 903 904**Example** 905```ts 906let para: Record<string, number> = { 'PropA': 47 }; 907let storage: LocalStorage = new LocalStorage(para); 908``` 909 910 911### getShared<sup>(deprecated)</sup> 912 913static getShared(): LocalStorage 914 915Obtains the [LocalStorage](../../../ui/state-management/arkts-localstorage.md) instance shared across the current stage. 916 917> **NOTE** 918> 919> This API is supported since API version 10 and deprecated since API version 18. You are advised to use [getSharedLocalStorage](../js-apis-arkui-UIContext.md#getsharedlocalstorage12) in [UIContext](../js-apis-arkui-UIContext.md#uicontext) instead. 920> 921> The [getSharedLocalStorage](../js-apis-arkui-UIContext.md#getsharedlocalstorage12) API in [UIContext](../js-apis-arkui-UIContext.md#uicontext) is supported since API version 12. 922 923**Widget capability**: This API can be used in ArkTS widgets since API version 10. 924 925**Atomic service API**: This API can be used in atomic services since API version 11. 926 927**System capability**: SystemCapability.ArkUI.ArkUI.Full 928 929**Model restriction**: This API can be used only in the stage model. 930 931**Return value** 932 933| Type | Description | 934| ------------------------------ | ----------------- | 935| [LocalStorage](#localstorage9) | **LocalStorage** instance.| 936 937### has<sup>9+</sup> 938 939has(propName: string): boolean 940 941Checks whether the property corresponding to **propName** exists in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). 942 943**Widget capability**: This API can be used in ArkTS widgets since API version 9. 944 945**Atomic service API**: This API can be used in atomic services since API version 11. 946 947**System capability**: SystemCapability.ArkUI.ArkUI.Full 948 949**Parameters** 950 951| Name | Type | Mandatory | Description | 952| -------- | ------ | ---- | ------------------ | 953| propName | string | Yes | Property name in LocalStorage.| 954 955**Return value** 956 957| Type | Description | 958| ------- | ------------------------------------------------------------ | 959| boolean | Returns **true** if the property exists in LocalStorage; returns **false** otherwise.| 960 961**Example** 962```ts 963let para: Record<string, number> = { 'PropA': 47 }; 964let storage: LocalStorage = new LocalStorage(para); 965storage.has('PropA'); // true 966``` 967 968 969### get<sup>9+</sup> 970 971get<T>(propName: string): T | undefined 972 973Obtains the value of the property corresponding to **propName** from [LocalStorage](../../../ui/state-management/arkts-localstorage.md). 974 975**Widget capability**: This API can be used in ArkTS widgets since API version 9. 976 977**Atomic service API**: This API can be used in atomic services since API version 11. 978 979**System capability**: SystemCapability.ArkUI.ArkUI.Full 980 981**Parameters** 982 983| Name | Type | Mandatory | Description | 984| -------- | ------ | ---- | ------------------ | 985| propName | string | Yes | Property name in LocalStorage.| 986 987**Return value** 988 989| Type | Description | 990| ------------------------ | ------------------------------------------------------------ | 991| T \| undefined | Value of the property corresponding to **propName** in LocalStorage, or **undefined** if it does not exist.| 992 993**Example** 994```ts 995let para: Record<string, number> = { 'PropA': 47 }; 996let storage: LocalStorage = new LocalStorage(para); 997let value: number = storage.get('PropA') as number; // 47 998``` 999 1000 1001### set<sup>9+</sup> 1002 1003set<T>(propName: string, newValue: T): boolean 1004 1005Sets the value of the property corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). If the value of **newValue** is the same as the current value of the property, no assignment is performed, and the state variable does not instruct the UI to update the value of the property. Since API version 12, **newValue** can be **null** or **undefined**. 1006 1007> **NOTE** 1008> 1009> Since API version 12, LocalStorage supports [Map](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-localstorage.md#using-union-types-in-localstorage) types. 1010 1011**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1012 1013**Atomic service API**: This API can be used in atomic services since API version 11. 1014 1015**System capability**: SystemCapability.ArkUI.ArkUI.Full 1016 1017**Parameters** 1018 1019| Name | Type | Mandatory | Description | 1020| -------- | ------ | ---- | ----------------------- | 1021| propName | string | Yes | Property name in LocalStorage. | 1022| newValue | T | Yes | Property value. Since API version 12, the value can be **null** or **undefined**.| 1023 1024**Return value** 1025 1026| Type | Description | 1027| ------- | ------------------------------------------------------------ | 1028| boolean | Returns **true** if the operation is successful; returns **false** if the property corresponding to **propName** does not exist in LocalStorage. | 1029 1030**Example** 1031 1032```ts 1033let para: Record<string, number> = { 'PropA': 47 }; 1034let storage: LocalStorage = new LocalStorage(para); 1035let res: boolean = storage.set('PropA', 47); // true 1036let res1: boolean = storage.set('PropB', 47); // false 1037``` 1038 1039 1040### setOrCreate<sup>9+</sup> 1041 1042setOrCreate<T>(propName: string, newValue: T): boolean 1043 1044Sets the value of the property corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md) to a new value, if the property exists and the new value is different from the current value. If the new value is the same as the current value of the property, no assignment is performed, and the state variable does not instruct the UI to update the value of the property. 1045If the property does not exist, this API creates it with the value of **newValue**. This **setOrCreate** method creates only one LocalStorage key-value pair. To create multiple key-value pairs, call this method multiple times. Since API version 12, **newValue** can be **null** or **undefined**. 1046 1047> **NOTE** 1048> 1049> Since API version 12, LocalStorage supports [Map](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-localstorage.md#using-union-types-in-localstorage) types. 1050 1051**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1052 1053**Atomic service API**: This API can be used in atomic services since API version 11. 1054 1055**System capability**: SystemCapability.ArkUI.ArkUI.Full 1056 1057**Parameters** 1058 1059| Name | Type | Mandatory | Description | 1060| -------- | ------ | ---- | ----------------------- | 1061| propName | string | Yes | Property name in LocalStorage. | 1062| newValue | T | Yes | Property value. Since API version 12, the value can be **null** or **undefined**.| 1063 1064**Return value** 1065 1066| Type | Description | 1067| ------- | ------------------------------------------------------------ | 1068| boolean | Returns **true** if the property corresponding to **propName** exists and its value is updated to the value of **newValue**,<br>or if **propName** is created with the value of **newValue**.| 1069 1070**Example** 1071 1072```ts 1073let para: Record<string, number> = { 'PropA': 47 }; 1074let storage: LocalStorage = new LocalStorage(para); 1075let res: boolean = storage.setOrCreate('PropA', 121); // true 1076let res1: boolean = storage.setOrCreate('PropB', 111); // true 1077let res2: boolean = storage.setOrCreate('PropB', null); // true (API version 12 and later) or false (API version 11 and earlier) 1078``` 1079 1080### ref<sup>12+</sup> 1081 1082public ref\<T\>(propName: string): AbstractProperty\<T\> | undefined 1083 1084Returns a reference to the data corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). If the provided **propName** does not exist, this API returns **undefined**. 1085 1086This API is similar to [link](#link9) but does not require manually releasing the returned variable of the [AbstractProperty](#abstractproperty) type. 1087 1088**Atomic service API**: This API can be used in atomic services since API version 12. 1089 1090**System capability**: SystemCapability.ArkUI.ArkUI.Full 1091 1092**Parameters** 1093 1094| Name | Type | Mandatory| Description | 1095| -------- | ------ | ---- | ------------------------ | 1096| propName | string | Yes | Property name in LocalStorage.| 1097 1098**Return value** 1099 1100| Type | Description | 1101| -------------------------------------- | ------------------------------------------------------------ | 1102| [AbstractProperty<T>](#abstractproperty) \| undefined | A reference to the property in LocalStorage, or **undefined** if the property does not exist.| 1103 1104**Example** 1105 1106```ts 1107let para: Record<string, number> = { 'PropA': 47 }; 1108let storage: LocalStorage = new LocalStorage(para); 1109let refToPropA1: AbstractProperty<number> | undefined = storage.ref('PropA'); 1110let refToPropA2: AbstractProperty<number> | undefined = storage.ref('PropA'); // refToPropA2.get() == 47 1111refToPropA1?.set(48); // refToPropA1.get() == refToPropA2.get() == 48 1112``` 1113 1114### setAndRef<sup>12+</sup> 1115 1116public setAndRef<T>(propName: string, defaultValue: T): AbstractProperty<T> 1117 1118Similar to the [ref](#ref12-1) API, returns a reference to the data corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). If **propName** does not exist, this API creates and initializes the property in LocalStorage using **defaultValue** and returns its reference. **defaultValue** must be of the **T** type and can be **null** or **undefined**. 1119 1120This API is similar to [setAndLink](#setandlink9) but does not require manually releasing the returned variable of the [AbstractProperty](#abstractproperty) type. 1121 1122> **NOTE** 1123> 1124> Since API version 12, LocalStorage supports [Map](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-localstorage.md#using-union-types-in-localstorage) types. 1125 1126**Atomic service API**: This API can be used in atomic services since API version 12. 1127 1128**System capability**: SystemCapability.ArkUI.ArkUI.Full 1129 1130**Parameters** 1131 1132| Name | Type | Mandatory| Description | 1133| ------------ | ------ | ---- | ------------------------------------------------------------ | 1134| propName | string | Yes | Property name in LocalStorage. | 1135| defaultValue | T | Yes | Default value used to initialize **propName** in LocalStorage if it does not exist. The value can be **null** or **undefined**.| 1136 1137**Return value** 1138 1139| Type | Description | 1140| ------------------------- | ------------------------------------------------------------ | 1141| [AbstractProperty<T>](#abstractproperty) | Instance of **AbstractProperty<T>**, which is a reference to the property in LocalStorage corresponding to **propName**.| 1142 1143**Example** 1144 1145```ts 1146let para: Record<string, number> = { 'PropA': 47 }; 1147let storage: LocalStorage = new LocalStorage(para); 1148let ref1: AbstractProperty<number> = storage.setAndRef('PropB', 49); // Create PropB with the default value 49. 1149let ref2: AbstractProperty<number> = storage.setAndRef('PropA', 50); // PropA already exists with the value 47. 1150``` 1151 1152### link<sup>9+</sup> 1153 1154link<T>(propName: string): SubscribedAbstractProperty<T> 1155 1156Establishes a two-way data binding with the property corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). If the given property exists in LocalStorage, this API returns the two-way bound data for the property. 1157 1158Any update of the data is synchronized back to LocalStorage, which then synchronizes the update to all data and custom components bound to the property. 1159 1160If the given property does not exist in LocalStorage, **undefined** is returned. 1161 1162**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1163 1164**Atomic service API**: This API can be used in atomic services since API version 11. 1165 1166**System capability**: SystemCapability.ArkUI.ArkUI.Full 1167 1168**Parameters** 1169 1170| Name | Type | Mandatory | Description | 1171| -------- | ------ | ---- | ------------------ | 1172| propName | string | Yes | Property name in LocalStorage.| 1173 1174**Return value** 1175 1176| Type | Description | 1177| ----------------------------------- | ------------------------------------------------------------ | 1178| [SubscribedAbstractProperty<T>](#subscribedabstractproperty) | Returns the **SubscribedAbstractProperty<T>** instance if the given property exists in LocalStorage; returns undefined otherwise.| 1179 1180**Example** 1181```ts 1182let para: Record<string, number> = { 'PropA': 47 }; 1183let storage: LocalStorage = new LocalStorage(para); 1184let linkToPropA1: SubscribedAbstractProperty<number> = storage.link('PropA'); 1185let linkToPropA2: SubscribedAbstractProperty<number> = storage.link('PropA'); // linkToPropA2.get() == 47 1186linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48 1187``` 1188 1189 1190### setAndLink<sup>9+</sup> 1191 1192setAndLink<T>(propName: string, defaultValue: T): SubscribedAbstractProperty<T> 1193 1194Similar to the [link](#link9) API, establishes a two-way data binding with the property corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). If the given property exists in LocalStorage, this API returns the two-way bound data for the property. If the given property does not exist, this API creates and initializes the property in LocalStorage using **defaultValue** and returns its two-way bound data. The value of **defaultValue** must be of the **T** type. Since API version 12, it can be **null** or **undefined**. 1195 1196> **NOTE** 1197> 1198> Since API version 12, LocalStorage supports [Map](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-localstorage.md#using-union-types-in-localstorage) types. 1199 1200**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1201 1202**Atomic service API**: This API can be used in atomic services since API version 11. 1203 1204**System capability**: SystemCapability.ArkUI.ArkUI.Full 1205 1206**Parameters** 1207 1208| Name | Type | Mandatory| Description | 1209| ------------ | ------ | ---- | ------------------------------------------------------------ | 1210| propName | string | Yes | Property name in LocalStorage. | 1211| defaultValue | T | Yes | Default value used to initialize **propName** in LocalStorage if it does not exist. Since API version 12, **defaultValue** can be **null** or **undefined**.| 1212 1213**Return value** 1214 1215| Type | Description | 1216| ----------------------------------- | ------------------------------------------------------------ | 1217| [SubscribedAbstractProperty<T>](#subscribedabstractproperty) | Instance of **SubscribedAbstractProperty<T>**, which is two-way bound data of the given property in LocalStorage.| 1218 1219**Example** 1220```ts 1221let para: Record<string, number> = { 'PropA': 47 }; 1222let storage: LocalStorage = new LocalStorage(para); 1223let link1: SubscribedAbstractProperty<number> = storage.setAndLink('PropB', 49); // Create PropB with the default value 49. 1224let link2: SubscribedAbstractProperty<number> = storage.setAndLink('PropA', 50); // PropA already exists with the value 47. 1225``` 1226 1227 1228### prop<sup>9+</sup> 1229 1230prop<S>(propName: string): SubscribedAbstractProperty<S> 1231 1232Establishes a one-way data binding with the property corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). If the given property exists in LocalStorage, this API returns the one-way bound data for the property. If the given property does not exist in LocalStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to LocalStorage. 1233 1234**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1235 1236**Atomic service API**: This API can be used in atomic services since API version 11. 1237 1238**System capability**: SystemCapability.ArkUI.ArkUI.Full 1239 1240**Parameters** 1241 1242| Name | Type | Mandatory | Description | 1243| -------- | ------ | ---- | ------------------ | 1244| propName | string | Yes | Property name in LocalStorage.| 1245 1246**Return value** 1247 1248| Type | Description | 1249| ----------------------------------- | ------------------------------------------------------------ | 1250| [SubscribedAbstractProperty<S>](#subscribedabstractproperty) | Instance of **SubscribedAbstractProperty<S>**, which is one-way bound data of the given property in LocalStorage, or **undefined** if the provided **propName** does not exist in LocalStorage.| 1251 1252**Example** 1253```ts 1254let para: Record<string, number> = { 'PropA': 47 }; 1255let storage: LocalStorage = new LocalStorage(para); 1256let prop1: SubscribedAbstractProperty<number> = storage.prop('PropA'); 1257let prop2: SubscribedAbstractProperty<number> = storage.prop('PropA'); 1258prop1.set(1); // One-way synchronization: prop1.get() returns 1, while prop2.get() returns 47. 1259``` 1260 1261 1262### setAndProp<sup>9+</sup> 1263 1264setAndProp<S>(propName: string, defaultValue: S): SubscribedAbstractProperty<S> 1265 1266Similar to the [prop](#prop9) API, establishes a one-way data binding with the property corresponding to **propName** in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). If the given property exists in LocalStorage, this API returns the one-way bound data for the property. If the given property does not exist, this API creates and initializes the property in LocalStorage using **defaultValue** and returns its one-way bound data. The value of **defaultValue** must be of the **S** type. Since API version 12, it can be **null** or **undefined**. 1267 1268> **NOTE** 1269> 1270> Since API version 12, LocalStorage supports [Map](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-map-type), [Set](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-set-type), [Date](../../../ui/state-management/arkts-localstorage.md#decorating-variables-of-the-date-type), **null**, **undefined**, and [union](../../../ui/state-management/arkts-localstorage.md#using-union-types-in-localstorage) types. 1271 1272**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1273 1274**Atomic service API**: This API can be used in atomic services since API version 11. 1275 1276**System capability**: SystemCapability.ArkUI.ArkUI.Full 1277 1278**Parameters** 1279 1280| Name | Type | Mandatory| Description | 1281| ------------ | ------ | ---- | ------------------------------------------------------------ | 1282| propName | string | Yes | Property name in LocalStorage. | 1283| defaultValue | S | Yes | Default value used to initialize **propName** in LocalStorage if it does not exist. Since API version 12, **defaultValue** can be **null** or **undefined**.| 1284 1285**Return value** 1286 1287| Type | Description | 1288| ----------------------------------- | ------------------------------------------------------------ | 1289| [SubscribedAbstractProperty<S>](#subscribedabstractproperty) | Instance of **SubscribedAbstractProperty<S>**, which is one-way bound data of the given property in LocalStorage.| 1290 1291**Example** 1292 1293```ts 1294let para: Record<string, number> = { 'PropA': 47 }; 1295let storage: LocalStorage = new LocalStorage(para); 1296let prop: SubscribedAbstractProperty<number> = storage.setAndProp('PropB', 49); // PropA -> 47, PropB -> 49 1297``` 1298 1299 1300### delete<sup>9+</sup> 1301 1302delete(propName: string): boolean 1303 1304Deletes the property corresponding to **propName** from [LocalStorage](../../../ui/state-management/arkts-localstorage.md). The deletion is only successful if the property has no subscribers. If there is a subscriber, the deletion fails and **false** is returned. If the deletion is successful, **true** is returned. 1305 1306The property subscribers include the following: 1307 13081. Variables decorated by [\@LocalStorageLink](../../../ui/state-management/arkts-localstorage.md#localstoragelink) or [\@LocalStorageProp](../../../ui/state-management/arkts-localstorage.md#localstorageprop) 1309 13102. Instances of [SubscribedAbstractProperty](#subscribedabstractproperty) returned by [link](#link9), [prop](#prop9), [setAndLink](#setandlink9), or [setAndProp](#setandprop9) 1311 1312To delete these subscribers: 1313 13141. Remove the custom component containing \@LocalStorageLink or \@LocalStorageProp. For details, see [Custom Component Deletion](../../../ui/state-management/arkts-page-custom-components-lifecycle.md#custom-component-deletion). 1315 13162. Call the [aboutToBeDeleted](#abouttobedeleted10) API on instances of **SubscribedAbstractProperty** returned by **link**, **prop**, **setAndLink**, or **setAndProp**. 1317 1318**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1319 1320**Atomic service API**: This API can be used in atomic services since API version 11. 1321 1322**System capability**: SystemCapability.ArkUI.ArkUI.Full 1323 1324**Parameters** 1325 1326| Name | Type | Mandatory | Description | 1327| -------- | ------ | ---- | ------------------ | 1328| propName | string | Yes | Property name in LocalStorage.| 1329 1330**Return value** 1331 1332| Type | Description | 1333| ------- | ------------------------------------------------------------ | 1334| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1335 1336**Example** 1337```ts 1338let para: Record<string, number> = { 'PropA': 47 }; 1339let storage: LocalStorage = new LocalStorage(para); 1340storage.link<number>('PropA'); 1341let res: boolean = storage.delete('PropA'); // false: PropA still has subscribers. 1342let res1: boolean = storage.delete('PropB'); // false: PropB does not exist in LocalStorage. 1343storage.setOrCreate('PropB', 48); 1344let res2: boolean = storage.delete('PropB'); // true: PropB is successfully deleted from LocalStorage. 1345``` 1346 1347 1348### keys<sup>9+</sup> 1349 1350keys(): IterableIterator<string> 1351 1352Obtains all property names in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). 1353 1354**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1355 1356**Atomic service API**: This API can be used in atomic services since API version 11. 1357 1358**System capability**: SystemCapability.ArkUI.ArkUI.Full 1359 1360**Return value** 1361 1362| Type | Description | 1363| ------------------------------ | -------------------- | 1364| IterableIterator<string> | All property names in LocalStorage.| 1365 1366**Example** 1367```ts 1368let para: Record<string, number> = { 'PropA': 47 }; 1369let storage: LocalStorage = new LocalStorage(para); 1370let keys: IterableIterator<string> = storage.keys(); 1371``` 1372 1373 1374### size<sup>9+</sup> 1375 1376size(): number 1377 1378Obtains the number of properties in [LocalStorage](../../../ui/state-management/arkts-localstorage.md). 1379 1380**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1381 1382**Atomic service API**: This API can be used in atomic services since API version 11. 1383 1384**System capability**: SystemCapability.ArkUI.ArkUI.Full 1385 1386**Return value** 1387 1388| Type | Description | 1389| ------ | ---------------------------- | 1390| number | Number of properties in LocalStorage.| 1391 1392**Example** 1393```ts 1394let para: Record<string, number> = { 'PropA': 47 }; 1395let storage: LocalStorage = new LocalStorage(para); 1396let res: number = storage.size(); // 1 1397``` 1398 1399 1400### clear<sup>9+</sup> 1401 1402clear(): boolean 1403 1404Deletes all properties from [LocalStorage](../../../ui/state-management/arkts-localstorage.md). The deletion is only successful if none of the properties in LocalStorage have any subscribers. If there are subscribers, this API does not take effect and **false** is returned. If the deletion is successful, **true** is returned. 1405 1406For details about the subscriber, see [delete](#delete9). 1407 1408**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1409 1410**Atomic service API**: This API can be used in atomic services since API version 11. 1411 1412**System capability**: SystemCapability.ArkUI.ArkUI.Full 1413 1414**Return value** 1415 1416 1417| Type | Description | 1418| ------- | ------------------------------------------------------------ | 1419| boolean | Returns **true** if the operation is successful; returns **false** otherwise.| 1420 1421 1422**Example** 1423```ts 1424let para: Record<string, number> = { 'PropA': 47 }; 1425let storage: LocalStorage = new LocalStorage(para); 1426let res: boolean = storage.clear(); // true: There are no subscribers. 1427``` 1428 1429 1430### GetShared<sup>(deprecated)</sup> 1431 1432static GetShared(): LocalStorage 1433 1434Obtains the [LocalStorage](../../../ui/state-management/arkts-localstorage.md) instance shared across the current stage. 1435 1436> **NOTE** 1437> 1438> This API is deprecated since API version 10. You are advised to use [getSharedLocalStorage](../js-apis-arkui-UIContext.md#getsharedlocalstorage12) in [UIContext](../js-apis-arkui-UIContext.md#uicontext) instead. 1439 1440**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1441 1442**System capability**: SystemCapability.ArkUI.ArkUI.Full 1443 1444**Model restriction**: This API can be used only in the stage model. 1445 1446**Return value** 1447 1448| Type | Description | 1449| ------------------------------ | ----------------- | 1450| [LocalStorage](#localstorage9) | **LocalStorage** instance.| 1451 1452**Example** 1453```ts 1454let storage: LocalStorage = LocalStorage.GetShared(); 1455``` 1456 1457## AbstractProperty 1458 1459**Atomic service API**: This API can be used in atomic services since API version 12. 1460 1461**System capability**: SystemCapability.ArkUI.ArkUI.Full 1462 1463### get<sup>12+</sup> 1464 1465get(): T 1466 1467Reads data of the referenced property from [AppStorage](../../../ui/state-management/arkts-appstorage.md) or [LocalStorage](../../../ui/state-management/arkts-localstorage.md). 1468 1469**Atomic service API**: This API can be used in atomic services since API version 12. 1470 1471**System capability**: SystemCapability.ArkUI.ArkUI.Full 1472 1473**Return value** 1474 1475| Type| Description | 1476| ---- | ------------------------------------------- | 1477| T | Data of the referenced property in AppStorage or LocalStorage.| 1478 1479**Example** 1480 1481```ts 1482AppStorage.setOrCreate('PropA', 47); 1483let ref1: AbstractProperty<number> | undefined = AppStorage.ref('PropA'); 1484ref1?.get(); // ref1.get()=47 1485``` 1486 1487 1488### set<sup>12+</sup> 1489 1490set(newValue: T): void 1491 1492Updates the data of the referenced property in [AppStorage](../../../ui/state-management/arkts-appstorage.md) or [LocalStorage](../../../ui/state-management/arkts-localstorage.md). The value of **newValue** must be of the **T** type and can be **null** or **undefined**. 1493 1494> **NOTE** 1495> 1496> Since API version 12, AppStorage and LocalStorage support the Map, Set, Date types, as well as **null**, **undefined**, and union types. 1497 1498**Atomic service API**: This API can be used in atomic services since API version 12. 1499 1500**System capability**: SystemCapability.ArkUI.ArkUI.Full 1501 1502 1503**Parameters** 1504 1505 1506| Name | Type| Mandatory| Description | 1507| -------- | ---- | ---- | ------------------------------------- | 1508| newValue | T | Yes | New data to update. The value can be **null** or **undefined**.| 1509 1510 1511**Example** 1512 1513```ts 1514AppStorage.setOrCreate('PropA', 47); 1515let ref1: AbstractProperty<number> | undefined = AppStorage.ref('PropA'); 1516ref1?.set(1); // ref1.get()=1 1517let a: Map<string, number> = new Map([['1', 0]]); 1518let ref2 = AppStorage.setAndRef('MapA', a); 1519ref2.set(a); 1520let b: Set<string> = new Set('1'); 1521let ref3 = AppStorage.setAndRef('SetB', b); 1522ref3.set(b); 1523let c: Date = new Date('2024'); 1524let ref4 = AppStorage.setAndRef('DateC', c); 1525ref4.set(c); 1526ref2.set(null); 1527ref3.set(undefined); 1528``` 1529 1530### info<sup>12+</sup> 1531 1532info(): string 1533 1534Reads the property name of the referenced property from [AppStorage](../../../ui/state-management/arkts-appstorage.md) or [LocalStorage](../../../ui/state-management/arkts-localstorage.md). 1535 1536**Atomic service API**: This API can be used in atomic services since API version 12. 1537 1538**System capability**: SystemCapability.ArkUI.ArkUI.Full 1539 1540**Return value** 1541 1542| Type | Description | 1543| ------ | --------------------------------------------- | 1544| string | Property name of the referenced property in AppStorage or LocalStorage.| 1545 1546**Example** 1547 1548```ts 1549AppStorage.setOrCreate('PropA', 47); 1550let ref1: AbstractProperty<number> | undefined = AppStorage.ref('PropA'); 1551ref1?.info(); // ref1.info()='PropA' 1552``` 1553 1554## SubscribedAbstractProperty 1555 1556**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1557 1558**Atomic service API**: This API can be used in atomic services since API version 11. 1559 1560**System capability**: SystemCapability.ArkUI.ArkUI.Full 1561 1562### get<sup>9+</sup> 1563 1564abstract get(): T 1565 1566Reads the data of the synchronized property from [AppStorage](../../../ui/state-management/arkts-appstorage.md) or [LocalStorage](../../../ui/state-management/arkts-localstorage.md). 1567 1568**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1569 1570**Atomic service API**: This API can be used in atomic services since API version 11. 1571 1572**System capability**: SystemCapability.ArkUI.ArkUI.Full 1573 1574**Return value** 1575 1576| Type | Description | 1577| ---- | ------------------------------- | 1578| T | Data of the synchronized property in AppStorage or LocalStorage.| 1579 1580**Example** 1581```ts 1582AppStorage.setOrCreate('PropA', 47); 1583let prop1: SubscribedAbstractProperty<number> = AppStorage.prop('PropA'); 1584prop1.get(); // prop1.get()=47 1585``` 1586 1587 1588### set<sup>9+</sup> 1589 1590abstract set(newValue: T): void 1591 1592Sets the data of the synchronized property in [AppStorage](../../../ui/state-management/arkts-appstorage.md) or [LocalStorage](../../../ui/state-management/arkts-localstorage.md). The value of **newValue** must be of the **T** type. Since API version 12, it can be **null** or **undefined**. 1593 1594> **NOTE** 1595> 1596>Since API version 12, AppStorage and LocalStorage support the Map, Set, Date types, as well as **null**, **undefined**, and union types. 1597 1598**Widget capability**: This API can be used in ArkTS widgets since API version 9. 1599 1600**Atomic service API**: This API can be used in atomic services since API version 11. 1601 1602**System capability**: SystemCapability.ArkUI.ArkUI.Full 1603 1604 1605**Parameters** 1606 1607 1608| Name | Type| Mandatory| Description | 1609| -------- | ---- | ---- | --------------------------------------------------------- | 1610| newValue | T | Yes | Data to set. Since API version 12, the value can be **null** or **undefined**.| 1611 1612 1613**Example** 1614```ts 1615AppStorage.setOrCreate('PropA', 47); 1616let prop1: SubscribedAbstractProperty<number> = AppStorage.prop('PropA'); 1617prop1.set(1); // prop1.get()=1 1618// Since API version 12, the Map, Set, Date types, as well as null, undefined, and union types are supported. 1619let a: Map<string, number> = new Map([['1', 0]]); 1620let prop2 = AppStorage.setAndProp('MapA', a); 1621prop2.set(a); 1622let b: Set<string> = new Set('1'); 1623let prop3 = AppStorage.setAndProp('SetB', b); 1624prop3.set(b); 1625let c: Date = new Date('2024'); 1626let prop4 = AppStorage.setAndProp('DateC', c); 1627prop4.set(c); 1628prop2.set(null); 1629prop3.set(undefined); 1630``` 1631 1632### aboutToBeDeleted<sup>10+</sup> 1633 1634abstract aboutToBeDeleted(): void 1635 1636Cancels the synchronization relationship between the [SubscribedAbstractProperty](#subscribedabstractproperty) instance and [AppStorage](../../../ui/state-management/arkts-appstorage.md) or [LocalStorage](../../../ui/state-management/arkts-localstorage.md), whether it is a one-way or two-way binding. After **aboutToBeDeleted** is called, the **SubscribedAbstractProperty** instance is invalidated, meaning it can no longer be used to call the [set](#set9-1) or [get](#get9-1) API. 1637 1638**Atomic service API**: This API can be used in atomic services since API version 11. 1639 1640**System capability**: SystemCapability.ArkUI.ArkUI.Full 1641 1642**Example** 1643```ts 1644AppStorage.setOrCreate('PropA', 47); 1645let link = AppStorage.setAndLink('PropB', 49); // PropA -> 47, PropB -> 49 1646link.aboutToBeDeleted(); 1647``` 1648 1649### info 1650 1651info(): string; 1652 1653Property name. 1654 1655**Atomic service API**: This API can be used in atomic services since API version 11. 1656 1657**System capability**: SystemCapability.ArkUI.ArkUI.Full 1658 1659**Return value** 1660 1661|Type |Description | 1662|---------|-------------| 1663|string |Property name. | 1664 1665## PersistPropsOptions<sup>10+</sup> 1666 1667Defines a key-value pair object used to specify persistent properties and their default values, passed as a parameter to [persistProps](#persistprops10). 1668 1669**Atomic service API**: This API can be used in atomic services since API version 11. 1670 1671**System capability**: SystemCapability.ArkUI.ArkUI.Full 1672 1673| Name | Type | Mandatory| Description | 1674| ------------ | ------------------------------------- | ---- | ------------------------------------------------------------ | 1675| key | string | Yes | Property name. | 1676| defaultValue | number \| string \| boolean \| Object | Yes | Default value used to initialize the property when it does not exist in PersistentStorage and AppStorage. Since API version 12, **defaultValue** can be **null** or **undefined**.| 1677 1678## PersistentStorage 1679 1680**Atomic service API**: This API can be used in atomic services since API version 11. 1681 1682**System capability**: SystemCapability.ArkUI.ArkUI.Full 1683 1684For details about how to use PersistentStorage on the UI, see [PersistentStorage: Persisting Application State](../../../ui/state-management/arkts-persiststorage.md). 1685 1686> **NOTE** 1687> 1688> Since API version 12, PersistentStorage supports **null** and **undefined**. 1689 1690### persistProp<sup>10+</sup> 1691 1692static persistProp<T>(key: string, defaultValue: T): void 1693 1694Persists the property corresponding to **key** from [AppStorage](../../../ui/state-management/arkts-appstorage.md) to a file. This API is usually called before access to AppStorage. 1695 1696The order for determining the type and value of a property is as follows: 1697 16981. If the property with the specified key is found in the [PersistentStorage](../../../ui/state-management/arkts-persiststorage.md) file, the corresponding property is created in AppStorage and initialized with the value found in PersistentStorage. 1699 17002. If the property with the specified key is not found in the PersistentStorage file, AppStorage is searched for the property. If the property is found, it is persisted. 1701 17023. If no matching property is found in AppStorage, it is created in AppStorage, initialized with the value of **defaultValue**, and persisted. 1703 1704According to the preceding initialization process, if the property exists in AppStorage, its value will be used, overriding the value in the PersistentStorage file. Because AppStorage stores data in the memory, the property value becomes nonpersistent. 1705 1706**Atomic service API**: This API can be used in atomic services since API version 11. 1707 1708**System capability**: SystemCapability.ArkUI.ArkUI.Full 1709 1710**Parameters** 1711 1712| Name | Type | Mandatory| Description | 1713| ------------ | ------ | ---- | ------------------------------------------------------------ | 1714| key | string | Yes | Property name. | 1715| defaultValue | T | Yes | Default value used for initialization if the specified **key** is not found in PersistentStorage and AppStorage. Since API version 12, the value can be **null** or **undefined**.| 1716 1717 1718**Example** 1719 1720 1721For details about how to use persistProp, see [Accessing a PersistentStorage-Initialized Property from AppStorage](../../../ui/state-management/arkts-persiststorage.md#accessing-a-persistentstorage-initialized-property-from-appstorage). 1722 1723 1724### deleteProp<sup>10+</sup> 1725 1726static deleteProp(key: string): void 1727 1728Performs the reverse operation of [persistProp](#persistprop10). Specifically, this API deletes the property corresponding to the specified **key** from [PersistentStorage](../../../ui/state-management/arkts-persiststorage.md). Subsequent operations on [AppStorage](../../../ui/state-management/arkts-appstorage.md) do not affect data in PersistentStorage. This operation removes the corresponding key from the persistence file. To persist the property again, you can call the [persistProp](#persistprop10) API. 1729 1730**Atomic service API**: This API can be used in atomic services since API version 11. 1731 1732**System capability**: SystemCapability.ArkUI.ArkUI.Full 1733 1734**Parameters** 1735 1736| Name | Type | Mandatory | Description | 1737| ---- | ------ | ---- | ----------------------- | 1738| key | string | Yes | Property name in PersistentStorage.| 1739 1740**Example** 1741```ts 1742PersistentStorage.deleteProp('highScore'); 1743``` 1744 1745 1746### persistProps<sup>10+</sup> 1747 1748static persistProps(props: PersistPropsOptions[]): void 1749 1750Persists multiple properties. This API is similar to [persistProp](#persistprop10), but allows multiple properties to be persisted at once, making it suitable for initializing during application startup. 1751 1752**Atomic service API**: This API can be used in atomic services since API version 11. 1753 1754**System capability**: SystemCapability.ArkUI.ArkUI.Full 1755 1756**Parameters** 1757 1758| Name | Type | Mandatory | Description | 1759| ---------- | ---------------------------------------- | ---- | ---------------------------------------- | 1760| props | [PersistPropsOptions](#persistpropsoptions10)[] | Yes| Array of properties to persist.| 1761 1762**Example** 1763```ts 1764PersistentStorage.persistProps([{ key: 'highScore', defaultValue: '0' }, { key: 'wightScore', defaultValue: '1' }]); 1765``` 1766 1767 1768### keys<sup>10+</sup> 1769 1770static keys(): Array<string> 1771 1772Returns an array of all persisted property names. 1773 1774**Atomic service API**: This API can be used in atomic services since API version 11. 1775 1776**System capability**: SystemCapability.ArkUI.ArkUI.Full 1777 1778**Return value** 1779 1780| Type | Description | 1781| ------------------- | ---------------------------------- | 1782| Array<string> | Array of all persisted property names.| 1783 1784**Example** 1785```ts 1786let keys: Array<string> = PersistentStorage.keys(); 1787``` 1788 1789 1790### PersistProp<sup>(deprecated)</sup> 1791 1792static PersistProp<T>(key: string, defaultValue: T): void 1793 1794Persists the property corresponding to **key** from [AppStorage](../../../ui/state-management/arkts-appstorage.md) to a file. This API is usually called before access to AppStorage. 1795 1796The order for determining the type and value of a property is as follows: 1797 17981. If the property with the specified key is found in the [PersistentStorage](../../../ui/state-management/arkts-persiststorage.md) file, the corresponding property is created in AppStorage and initialized with the value found in PersistentStorage. 1799 18002. If the property with the specified key is not found in the PersistentStorage file, AppStorage is searched for the property. If the property is found, it is persisted. 1801 18023. If no matching property is found in AppStorage, it is created in AppStorage, initialized with the value of **defaultValue**, and persisted. 1803 1804According to the preceding initialization process, if the property exists in AppStorage, its value will be used, overriding the value in the PersistentStorage file. Because AppStorage stores data in the memory, the property value becomes nonpersistent. 1805 1806 1807> **NOTE** 1808> 1809> This API is deprecated since API version 10. You are advised to use [persistProp10+](#persistprop10) instead. 1810 1811**System capability**: SystemCapability.ArkUI.ArkUI.Full 1812 1813**Parameters** 1814 1815| Name | Type | Mandatory| Description | 1816| ------------ | ------ | ---- | ------------------------------------------------------------ | 1817| key | string | Yes | Property name. | 1818| defaultValue | T | Yes | Default value used for initialization if the specified **key** is not found in PersistentStorage and AppStorage. The value cannot be **null** or **undefined**.| 1819 1820 1821**Example** 1822 1823 1824```ts 1825PersistentStorage.PersistProp('highScore', '0'); 1826``` 1827 1828 1829### DeleteProp<sup>(deprecated)</sup> 1830 1831static DeleteProp(key: string): void 1832 1833Performs the reverse operation of [PersistProp](#persistpropdeprecated). Specifically, this API deletes the property corresponding to the specified key from [PersistentStorage](../../../ui/state-management/arkts-persiststorage.md). Subsequent operations on [AppStorage](../../../ui/state-management/arkts-appstorage.md) do not affect data in PersistentStorage. 1834 1835 1836> **NOTE** 1837> 1838> This API is deprecated since API version 10. You are advised to use [deleteProp10+](#deleteprop10) instead. 1839 1840**System capability**: SystemCapability.ArkUI.ArkUI.Full 1841 1842**Parameters** 1843 1844| Name | Type | Mandatory | Description | 1845| ---- | ------ | ---- | ----------------------- | 1846| key | string | Yes | Property name in PersistentStorage.| 1847 1848**Example** 1849```ts 1850PersistentStorage.DeleteProp('highScore'); 1851``` 1852 1853 1854### PersistProps<sup>(deprecated)</sup> 1855 1856static PersistProps(properties: {key: string, defaultValue: any;}[]): void 1857 1858Persists multiple properties. This API is similar to [PersistProp](#persistpropdeprecated), but allows multiple properties to be persisted at once, making it suitable for initializing during application startup. 1859 1860> **NOTE** 1861> 1862> This API is deprecated since API version 10. You are advised to use [persistProps10+](#persistprops10) instead. 1863 1864**System capability**: SystemCapability.ArkUI.ArkUI.Full 1865 1866**Parameters** 1867 1868| Name | Type | Mandatory| Description | 1869| ---------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1870| properties | {key: string, defaultValue: any}[] | Yes | Array of properties to persist.<br>**key**: property name.<br>**defaultValue**: default value. The rules are the same as those of **PersistProp**.| 1871 1872**Example** 1873 1874```ts 1875PersistentStorage.PersistProps([{ key: 'highScore', defaultValue: '0' }, { key: 'wightScore', defaultValue: '1' }]); 1876``` 1877 1878### Keys<sup>(deprecated)</sup> 1879 1880static Keys(): Array<string> 1881 1882Returns an array of all persisted property names. 1883 1884> **NOTE** 1885> 1886> This API is deprecated since API version 10. You are advised to use [keys10+](#keys10-1) instead. 1887 1888**System capability**: SystemCapability.ArkUI.ArkUI.Full 1889 1890**Return value** 1891 1892| Type | Description | 1893| ------------------- | ---------------------------------- | 1894| Array<string> | Array of all persisted property names.| 1895 1896**Example** 1897```ts 1898let keys: Array<string> = PersistentStorage.Keys(); 1899``` 1900## EnvPropsOptions<sup>10+</sup> 1901 1902Defines a key-value pair object used to specify environment variable names and their default values, passed as a parameter to [envProps](#envprops10). 1903 1904**Atomic service API**: This API can be used in atomic services since API version 11. 1905 1906**System capability**: SystemCapability.ArkUI.ArkUI.Full 1907 1908| Name | Type | Mandatory| Description | 1909| ------------ | --------------------------- | ---- | ------------------------------------------------------------ | 1910| key | string | Yes | Environment variable name. For details about the value range, see [Built-in Environment Variables](#built-in-environment-variables).| 1911| defaultValue | number \| string \| boolean | Yes | Default value used if the value of the specified environment variable key is not found in AppStorage.| 1912 1913## Environment 1914 1915For details about how to use environment parameters, see [Environment: Querying the Device Environment](../../../ui/state-management/arkts-environment.md). 1916 1917**Atomic service API**: This API can be used in atomic services since API version 11. 1918 1919**System capability**: SystemCapability.ArkUI.ArkUI.Full 1920 1921### envProp<sup>10+</sup> 1922 1923static envProp<S>(key: string, value: S): boolean 1924 1925Stores the built-in environment variable key from [Environment](../../../ui/state-management/arkts-environment.md) into [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the value of the environment variable key is not found in AppStorage, the default value is used and stored in AppStorage. If the value is successfully stored, **true** is returned. If the value of the environment variable key already exists in AppStorage, **false** is returned. 1926 1927You are advised to call this API when the application is started. 1928 1929It is incorrect to use AppStorage to read environment variables without calling **envProp** first. 1930 1931**Atomic service API**: This API can be used in atomic services since API version 11. 1932 1933**System capability**: SystemCapability.ArkUI.ArkUI.Full 1934 1935**Parameters** 1936 1937| Name| Type | Mandatory| Description | 1938| ------ | ------ | ---- | ------------------------------------------------------------ | 1939| key | string | Yes | Environment variable name. For details about the value range, see [Built-in Environment Variables](#built-in-environment-variables).| 1940| value | S | Yes | Default value used if the value of the environment variable key is not found in AppStorage.| 1941 1942**Return value** 1943 1944| Type | Description | 1945| ------- | ------------------------------------------------------------ | 1946| boolean | Returns **false** if the property corresponding to the key exists in AppStorage; creates a property with the key and the default value and returns **true** otherwise.| 1947 1948**Example** 1949 1950For details about how to use **envProp**, see [Accessing Environment Parameters from the UI](../../../ui/state-management/arkts-environment.md#accessing-environment-parameters-from-the-ui). 1951 1952 1953### envProps<sup>10+</sup> 1954 1955static envProps(props: EnvPropsOptions[]): void 1956 1957Works in a way similar to the [envProp](#envprop10) API, with the difference that it allows for initialization of multiple properties in batches. It is recommended that this API be called during application startup to store system environment variables to [AppStorage](../../../ui/state-management/arkts-appstorage.md) in batches. 1958 1959**Atomic service API**: This API can be used in atomic services since API version 11. 1960 1961**System capability**: SystemCapability.ArkUI.ArkUI.Full 1962 1963**Parameters** 1964 1965| Name| Type | Mandatory| Description | 1966| ------ | --------------------------------------------- | ---- | ------------------------------------ | 1967| props | [EnvPropsOptions](#envpropsoptions10)[] | Yes | Array of key-value pairs consisting of system environment variables and default values.| 1968 1969**Example** 1970```ts 1971Environment.envProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, { 1972 key: 'languageCode', 1973 defaultValue: 'en' 1974}, { key: 'prop', defaultValue: 'hhhh' }]); 1975``` 1976 1977 1978### keys<sup>10+</sup> 1979 1980static keys(): Array<string> 1981 1982Returns an array of keys of environment variables. 1983 1984**Atomic service API**: This API can be used in atomic services since API version 11. 1985 1986**System capability**: SystemCapability.ArkUI.ArkUI.Full 1987 1988**Return value** 1989 1990| Type | Description | 1991| ------------------- | ----------- | 1992| Array<string> | Array of associated system properties.| 1993 1994**Example** 1995```ts 1996Environment.envProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, { 1997 key: 'languageCode', 1998 defaultValue: 'en' 1999}, { key: 'prop', defaultValue: 'hhhh' }]); 2000 2001let keys: Array<string> = Environment.keys(); // keys contains accessibilityEnabled, languageCode, and prop. 2002``` 2003 2004 2005### EnvProp<sup>(deprecated)</sup> 2006 2007static EnvProp<S>(key: string, value: S): boolean 2008 2009Stores the built-in environment variable key from [Environment](../../../ui/state-management/arkts-environment.md) into [AppStorage](../../../ui/state-management/arkts-appstorage.md). If the value of the environment variable key is not found in AppStorage, the default value is used and stored in AppStorage. If the value is successfully stored, **true** is returned. If the value of the environment variable key already exists in AppStorage, **false** is returned. 2010 2011You are advised to call this API when the application is started. 2012 2013It is incorrect to use AppStorage to read environment variables without invoking **EnvProp** first. 2014 2015> **NOTE** 2016> 2017> This API is deprecated since API version 10. You are advised to use [envProp10+](#envprop10) instead. 2018 2019**System capability**: SystemCapability.ArkUI.ArkUI.Full 2020 2021**Parameters** 2022 2023| Name| Type | Mandatory| Description | 2024| ------ | ------ | ---- | ------------------------------------------------------------ | 2025| key | string | Yes | Environment variable name. For details about the value range, see [Built-in Environment Variables](#built-in-environment-variables).| 2026| value | S | Yes | Default value used if the value of the environment variable key is not found in AppStorage.| 2027 2028**Return value** 2029 2030| Type | Description | 2031| ------- | ------------------------------------------------------------ | 2032| boolean | Returns **false** if the property corresponding to the key exists in AppStorage; creates a property with the key and the default value and returns **true** otherwise.| 2033 2034**Example** 2035 2036 2037```ts 2038Environment.EnvProp('accessibilityEnabled', 'default'); 2039``` 2040 2041 2042### EnvProps<sup>(deprecated)</sup> 2043 2044static EnvProps(props: {key: string; defaultValue: any;}[]): void 2045 2046Works in a way similar to the [EnvProp](#envpropdeprecated) API, with the difference that it allows for initialization of multiple properties in batches. It is recommended that this API be called during application startup to store system environment variables to [AppStorage](../../../ui/state-management/arkts-appstorage.md) in batches. 2047 2048> **NOTE** 2049> 2050> This API is deprecated since API version 10. You are advised to use [envProps10+](#envprops10) instead. 2051 2052**System capability**: SystemCapability.ArkUI.ArkUI.Full 2053 2054**Parameters** 2055 2056| Name| Type | Mandatory| Description | 2057| ------ | ------------------------------------------------- | ---- | ------------------------------------ | 2058| props | {key: string, defaultValue: any}[] | Yes | Array of key-value pairs consisting of system environment variables and default values.| 2059 2060**Example** 2061```ts 2062Environment.EnvProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, { 2063 key: 'languageCode', 2064 defaultValue: 'en' 2065}, { key: 'prop', defaultValue: 'hhhh' }]); 2066``` 2067 2068 2069### Keys<sup>(deprecated)</sup> 2070 2071static Keys(): Array<string> 2072 2073Array of keys of environment variables. 2074 2075> **NOTE** 2076> 2077> This API is deprecated since API version 10. You are advised to use [keys10+](#keys10-2) instead. 2078 2079**System capability**: SystemCapability.ArkUI.ArkUI.Full 2080 2081**Return value** 2082 2083| Type | Description | 2084| ------------------- | ----------- | 2085| Array<string> | Array of associated system properties.| 2086 2087**Example** 2088 2089```ts 2090Environment.EnvProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, { 2091 key: 'languageCode', 2092 defaultValue: 'en' 2093}, { key: 'prop', defaultValue: 'hhhh' }]); 2094 2095let keys: Array<string> = Environment.Keys(); // keys contains accessibilityEnabled, languageCode, and prop. 2096``` 2097 2098 2099## Built-in Environment Variables 2100 2101| key | Type | Description | 2102| -------------------- | --------------- | ------------------------------------------------------------ | 2103| accessibilityEnabled | string | Whether to enable accessibility. If there is no value of **accessibilityEnabled** in the environment variables, the default value passed through APIs such as **envProp** and **envProps** is added to AppStorage.| 2104| colorMode | ColorMode | Color mode. The options are as follows:<br>- **ColorMode.LIGHT**: light mode.<br>- **ColorMode.DARK**: dark mode.| 2105| fontScale | number | Font scale. | 2106| fontWeightScale | number | Font weight scale. | 2107| layoutDirection | LayoutDirection | Layout direction. The options are as follows:<br>- **LayoutDirection.LTR**: from left to right.<br>- **LayoutDirection.RTL**: from right to left.<br>- **Auto**: follows the system settings.| 2108| languageCode | string | Current system language. The value is in lowercase, for example, **zh**. | 2109