未验证 提交 c980bf7e 编写于 作者: O openharmony_ci 提交者: Gitee

!22332 翻译完成 21754+20856:wallpaper+状态管理文档修改

Merge pull request !22332 from ester.zhou/TR-21754
......@@ -625,7 +625,7 @@ Subscribes to the wallpaper color change event.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event to subscribe to. The value **'colorChange'** indicates subscribing to the wallpaper color change event.|
| callback | function | Yes| Callback triggered when the wallpaper color changes. The wallpaper type and main colors are returned.<br>- colors<br> Main color information of the wallpaper. For details, see [RgbaColor](#rgbacolordeprecated).<br>- wallpaperType<br> Wallpaper type.|
| callback | function | Yes| Callback triggered when the wallpaper color changes. The wallpaper type and main colors are returned.<br>- **colors**: main color information of the wallpaper. For details, see [RgbaColor](#rgbacolordeprecated).<br>- **wallpaperType**: wallpaper type. |
**Example**
......@@ -657,7 +657,7 @@ Unsubscribes from the wallpaper color change event.
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event to unsubscribe from. The value **'colorChange'** indicates unsubscribing from the wallpaper color change event.|
| callback | function | No| Callback for the wallpaper color change event. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.<br>- colors<br> Main color information of the wallpaper. For details, see [RgbaColor](#rgbacolordeprecated).<br>- wallpaperType<br> Wallpaper type.|
| callback | function | No| Callback used for unsubscription. If this parameter is not set, this API unsubscribes from all callbacks of the specified event type.<br>- **colors**: main color information of the wallpaper. For details, see [RgbaColor](#rgbacolordeprecated).<br>- **wallpaperType**: wallpaper type. |
**Example**
......
# State Management with Application-level Variables
The state management module provides APIs for data storage, persistent data management, **Ability** data storage, and environment status required by applications.
> **NOTE**
The state management module provides data storage, persistent data management, UIAbility data storage, and environment state required by applications.
>**NOTE**
>
> 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.
>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.
The meanings of T and S in this topic are as follows:
| Type | Description |
| ---- | -------------------------------------- |
| T | Class, number, boolean, string, and arras of these types.|
| S | number, boolean, string. |
## AppStorage
### Link
Link(propName: string): any
For details about how to use AppStorage on the UI, see [AppStorage: Application-wide UI State Storage](../../quick-start/arkts-appstorage.md).
### link<sup>10+</sup>
static link\<T>(propName: string): SubscribedAbstractProperty\<T>
Establishes two-way data binding with the given attribute (specified by **propName**) in AppStorage. If the given attribute exists in AppStorage, the two-way bound data of the attribute in AppStorage is returned.
Any update of the data is synchronized back to AppStorage, which then synchronizes the update to all data and custom components bound to the attribute.
If the given attribute does not exist in AppStorage, **undefined** is returned.
Establishes two-way data binding between an attribute and this **LocalStorage** instance.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------- |
| propName | string | Yes | Name of the target attribute.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ----- | ---------------------------------------- |
| @Link | Returns two-way binding to this attribute if there is data with a given key. This means that attribute changes made by a variable or component will be synchronized to the **AppStorage**, and attribute changes made through the **AppStorage** will be synchronized to the variable or component.|
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;T&gt; | Returns two-way bound data if specified attribute exists in AppStorage; returns **undefined** otherwise.|
```ts
let simple = AppStorage.Link('simpleProp')
AppStorage.setOrCreate('PropA', 47);
let linkToPropA1 = AppStorage.link('PropA');
let linkToPropA2 = AppStorage.link('PropA'); // linkToPropA2.get() == 47
linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48
```
### SetAndLink
SetAndLink\<T>(propName: string, defaultValue: T): SubscribedAbstractProperty\<T>
### setAndLink<sup>10+</sup>
Works in a way similar to the **Link** API. If the current key is stored in the **AppStorage**, the value corresponding to the key is returned. If the key has not been created, a **Link** instance corresponding to the default value is created and returned.
static setAndLink&lt;T&gt;(propName: string, defaultValue: T): SubscribedAbstractProperty&lt;T&gt;
Works in a way similar to the **Link** API. If the given attribute exists in AppStorage, the two-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist, it is created and initialized with <b class="+ topic/ph hi-d/b " id="b537113298389">defaultValue</b> in AppStorage, and two-way bound data is returned.
**Parameters**
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ----------- |
| propName | string | Yes | Target key.|
| defaultValue | T | Yes | Default value to set. |
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| defaultValue | T | Yes | Default value used to initialize the attribute with the specified attribute name in AppStorage.|
**Return value**
| Type | Description |
| ----- | ---------------------------------------- |
| @Link | Returns the value corresponding to the key if the current key is stored in the **AppStorage**; creates and returns a **Link** instance corresponding to the default value if the key has not been created.|
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;T&gt; | Instance of **SubscribedAbstractProperty&lt;T&gt;** and two-way bound data of the given attribute in AppStorage|
```ts
let simple = AppStorage.SetAndLink('simpleProp', 121)
AppStorage.setOrCreate('PropA', 47);
let link1: SubscribedAbstractProperty<number> = AppStorage.setAndLink('PropB', 49); // Create PropB 49
let link2: SubscribedAbstractProperty<number> = AppStorage.setAndLink('PropA', 50); // PropA exists, remains 47
```
### Prop
Prop(propName: string): any
### prop<sup>10+</sup>
Establishes one-way data binding with an attribute to update its status.
static prop&lt;T&gt;(propName: string): SubscribedAbstractProperty&lt;T&gt;
Establishes one-way data binding with the given attribute (specified by **propName**) in AppStorage. If the given attribute exists in AppStorage, the one-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist in AppStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to AppStorage.
>**NOTE**
>
>Prop supports only simple types.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------- |
| propName | string | Yes | Target key.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;T&gt; | Returns one-way bound data if specified attribute exists in AppStorage; returns **undefined** otherwise.|
```ts
AppStorage.setOrCreate('PropA', 47);
let prop1: SubscribedAbstractProperty<number> = AppStorage.prop('PropA');
let prop2: SubscribedAbstractProperty<number> = AppStorage.prop('PropA');
prop1.set(1); // one-way sync: prop1.get()=1; but prop2.get() == 47
```
### setAndProp<sup>10+</sup>
static setAndProp&lt;T&gt;(propName: string, defaultValue: T): SubscribedAbstractProperty&lt;T&gt;
Works in a way similar to the **Prop** API. If the given attribute exists in AppStorage, the one-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in AppStorage, and one-way bound data is returned.
**Parameters**
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| defaultValue | T | Yes | Default value used to initialize the attribute with the specified attribute name in AppStorage.|
**Return value**
| Type | Description |
| ----------------------------------- | --------------------------------------- |
| SubscribedAbstractProperty&lt;T&gt; | Instance of **SubscribedAbstractProperty&lt;T&gt;**.|
```ts
AppStorage.setOrCreate('PropA', 47);
let prop: SubscribedAbstractProperty<number> = AppStorage.setAndProp('PropB', 49); // PropA -> 47, PropB -> 49
```
### has<sup>10+</sup>
static has(propName: string): boolean
Checks whether the attribute with the specified attribute name exists in AppStorage.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the attribute with the specified attribute name exists in AppStorage; returns **false** otherwise.|
```ts
AppStorage.has('simpleProp');
```
### get<sup>10+</sup>
static get&lt;T&gt;(propName: string): T | undefined
Obtains the attribute with the specified attribute name in AppStorage. If the attribute does not exist, **undefined** is returned.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------------------------ | ---------------------------------------- |
| T \| undefined | Returns the attribute with the specified attribute name in AppStorage; returns **undefined** if the attribute does not exist.|
```ts
AppStorage.setOrCreate('PropA', 47);
let value: number = AppStorage.get('PropA'); // 47
```
### set<sup>10+</sup>
static set&lt;T&gt;(propName: string, newValue: T): boolean
Sets the value for the attribute with the specified attribute name in AppStorage.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| newValue | T | Yes | Attribute value, which cannot be **undefined** or **null**.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; return **false** if the attribute with the specified attribute name does not exist in AppStorage, or the value to set is **undefined** or **null**. |
```ts
AppStorage.setOrCreate('PropA', 48);
let res: boolean = AppStorage.set('PropA', 47) // true
let res1: boolean = AppStorage.set('PropB', 47) // false
```
### setOrCreate<sup>10+</sup>
static setOrCreate&lt;T&gt;(propName: string, newValue: T): void
Sets a new value for the attribute with the specified attribute name in AppStorage or, if the attribute does not exist, creates one with the specified attribute name and default value.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| newValue | T | Yes | Attribute value, which cannot be **undefined** or **null**.|
```ts
AppStorage.setOrCreate('simpleProp', 121);
```
### delete<sup>10+</sup>
static delete(propName: string): boolean
Deletes the attribute with the specified attribute name from AppStorage
under the prerequisite that the attribute does not have a subscriber. If there is a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
The subscribers of the attribute are attributes with the same name bound to APIs such as **Link** and **Prop**, **\@StorageLink('propName')**, and **\@StorageProp('propName')**. This means that if **\@StorageLink('propName')** and **\@StorageProp('propName')** are used in a custom component or if there is still a **SubscribedAbstractProperty** instance in sync with the attribute, the attribute cannot be deleted from AppStorage.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
```ts
AppStorage.setOrCreate('PropA', 47);
AppStorage.link('PropA');
let res: boolean = AppStorage.delete('PropA'); // false, PropA still has a subscriber
AppStorage.setOrCreate('PropB', 48);
let res1: boolean = AppStorage.delete('PropB'); // true, PropB is deleted from AppStorage successfully
```
### keys<sup>10+</sup>
static keys(): IterableIterator&lt;string&gt;
Obtains all attribute names in AppStorage.
**Return value**
| Type | Description |
| ------------------------------ | ------------------ |
| IterableIterator&lt;string&gt; | All attribute names in AppStorage.|
```ts
AppStorage.setOrCreate('PropB', 48);
let keys: IterableIterator<string> = AppStorage.keys();
```
### clear<sup>10+</sup>
static clear(): boolean
Deletes all attributes from AppStorage under the prerequisite that none of the attributes has a subscriber. If any of the attributes has a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
For details about the subscriber, see [Delete](#delete10).
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
```ts
AppStorage.setOrCreate('PropA', 47);
let res: boolean = AppStorage.clear(); // true, there are no subscribers
```
### size<sup>10+</sup>
static size(): number
Obtains the number of attributes in AppStorage.
**Return value**
| Type | Description |
| ----- | ---------------------------------------- |
| @Prop | Returns one-way binding to an attribute with a given key if the attribute exists; returns **undefined** otherwise. One-way binding means that attribute changes made through the **AppStorage** will be synchronized to the variable or component, but attribute changes made by the variable or component will not be synchronized to the **AppStorage**. This API returns immutable variables and is applicable to mutable and immutable state variables alike.|
| Type | Description |
| ------ | ------------------- |
| number | Number of attributes in AppStorage.|
```ts
let simple = AppStorage.Prop('simpleProp')
AppStorage.setOrCreate('PropB', 48);
let res: number = AppStorage.size(); // 1
```
### SetAndProp
SetAndProp\<S>(propName: string, defaultValue: S): SubscribedAbstractProperty\<S>
### Link<sup>(deprecated)</sup>
static Link(propName: string): any
Establishes two-way data binding with the given attribute (specified by **propName**) in AppStorage. If the given attribute exists in AppStorage, the two-way bound data of the attribute in AppStorage is returned.
Any update of the data is synchronized back to AppStorage, which then synchronizes the update to all data and custom components bound to the attribute.
If the given attribute does not exist in AppStorage, **undefined** is returned.
This API is deprecated since API version 10. You are advised to use [link10+](#link10) instead.
Works in a way similar to the **Prop** API. If the current key is stored in the **AppStorage**, the value corresponding to the key is returned. If the key has not been created, a **Prop** instance corresponding to the default value is created and returned.
**Parameters**
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | --------------- |
| propName | string | Yes | Key of the target key-value pair.|
| defaultValue | S | Yes | Default value to set. |
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ----- | ---------------------------------------- |
| @Prop | Returns the value corresponding to the key if the current key is stored in the **AppStorage**; creates and returns a **Prop** instance corresponding to the default value otherwise.|
| Type | Description |
| ---- | ---------------------------------------- |
| any | Returns two-way bound data if specified attribute exists in AppStorage; returns **undefined** otherwise.|
```ts
let simple = AppStorage.SetAndProp('simpleProp', 121)
AppStorage.SetOrCreate('PropA', 47);
let linkToPropA1 = AppStorage.Link('PropA');
let linkToPropA2 = AppStorage.Link('PropA'); // linkToPropA2.get() == 47
linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48
```
### Has
Has(propName: string): boolean
### SetAndLink<sup>(deprecated)</sup>
static SetAndLink&lt;T&gt;(propName: string, defaultValue: T): SubscribedAbstractProperty&lt;T&gt;
Works in a way similar to the **Link** API. If the given attribute exists in AppStorage, the two-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist, it is created and initialized with <b class="+ topic/ph hi-d/b " id="b537113298389">defaultValue</b> in AppStorage, and two-way bound data is returned.
Checks whether the attribute corresponding to the specified key exists.
This API is deprecated since API version 10. You are advised to use [setAndLink10+](#setandlink10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------- |
| propName | string | Yes | Key of the attribute.|
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| defaultValue | T | Yes | Default value used to initialize the attribute with the specified attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------- | ------------- |
| boolean | Returns whether the attribute exists.|
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;T&gt; | Instance of **SubscribedAbstractProperty&lt;T&gt;** and two-way bound data of the given attribute in AppStorage|
```ts
let simple = AppStorage.Has('simpleProp')
AppStorage.SetOrCreate('PropA', 47);
let link1: SubscribedAbstractProperty<number> = AppStorage.SetAndLink('PropB', 49); // Create PropB 49
let link2: SubscribedAbstractProperty<number> = AppStorage.SetAndLink('PropA', 50); // PropA exists, remains 47
```
### Get
### Prop<sup>(deprecated)</sup>
static Prop(propName: string): any
Get\<T>(propName: string): T | undefined
Establishes one-way data binding with the given attribute (specified by **propName**) in AppStorage. If the given attribute exists in AppStorage, the one-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist in AppStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to AppStorage.
Obtains the value of the specified key.
>**NOTE**
>
>Prop supports only simple types.
>
>This API is deprecated since API version 10. You are advised to use [prop10+](#prop10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------- |
| propName | string | Yes | Key of the value to obtain.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ----------------- | ------------- |
| T or undefined| Returns the attribute value if the attribute exists; returns **undefined** otherwise.|
| Type | Description |
| ---- | ---------------------------------------- |
| any | Returns one-way bound data if specified attribute exists in AppStorage; returns **undefined** otherwise.|
```ts
let simple = AppStorage.Get('simpleProp')
AppStorage.SetOrCreate('PropA', 47);
let prop1 = AppStorage.Prop('PropA');
let prop2 = AppStorage.Prop('PropA');
prop1.set(1); // one-way sync: prop1.get()=1; but prop2.get() == 47
```
### Set
Set\<T>(propName: string, newValue: T): boolean
### SetAndProp<sup>(deprecated)</sup>
static SetAndProp&lt;S&gt;(propName: string, defaultValue: S): SubscribedAbstractProperty&lt;S&gt;
Replaces the value of a saved key.
Works in a way similar to the **Prop** API. If the given attribute exists in AppStorage, the one-way bound data of the attribute in AppStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in AppStorage, and one-way bound data is returned.
This API is deprecated since API version 10. You are advised to use [setAndProp10+](#setandprop10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------- |
| propName | string | Yes | Key to set. |
| newValue | T | Yes | Value to set.|
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| defaultValue | S | Yes | Default value used to initialize the attribute with the specified attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------- | ----------------------------------- |
| boolean | Returns **true** and the value if the key exists; returns **false** otherwise.|
| Type | Description |
| ----------------------------------- | --------------------------------------- |
| SubscribedAbstractProperty&lt;S&gt; | Instance of **SubscribedAbstractProperty&lt;S&gt;**.|
```ts
let simple = AppStorage.Set('simpleProp', 121)
AppStorage.SetOrCreate('PropA', 47);
let prop: SubscribedAbstractProperty<number> = AppStorage.SetAndProp('PropB', 49); // PropA -> 47, PropB -> 49
```
### SetOrCreate
SetOrCreate\<T>(propName: string, newValue: T): void
### Has<sup>(deprecated)</sup>
static Has(propName: string): boolean
Checks whether the attribute with the specified attribute name exists in AppStorage.
Creates or updates the value of the specified key.
This API is deprecated since API version 10. You are advised to use [has10+](#has10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | --------------- |
| propName | string | Yes | Key to set. |
| newValue | T | Yes | Value to be updated or created.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Updates the value of the attribute and returns **true** if an attribute that has the same name as the specified key exists; creates an attribute with the specified value as its default value and returns **false** otherwise. **undefined** and **null** are not allowed to return **true**.|
| boolean | Returns **true** if the attribute with the specified attribute name exists in AppStorage; returns **false** otherwise.|
```ts
let simple = AppStorage.SetOrCreate('simpleProp', 121)
AppStorage.Has('simpleProp');
```
### Delete
Delete(propName: string): boolean
### Get<sup>(deprecated)</sup>
static Get&lt;T&gt;(propName: string): T | undefined
Deletes the key-value pair that matches the specified key.
Obtains the attribute with the specified attribute name in AppStorage. If the attribute does not exist, **undefined** is returned.
This API is deprecated since API version 10. You are advised to use [get10+](#get10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------ |
| propName | string | Yes | Key of the target key-value pair.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------------------------ | ---------------------------------------- |
| T \| undefined | Returns the attribute with the specified attribute name in AppStorage; returns **undefined** if the attribute does not exist.|
```ts
AppStorage.SetOrCreate('PropA', 47);
let value: number = AppStorage.Get('PropA'); // 47
```
### Set<sup>(deprecated)</sup>
static Set&lt;T&gt;(propName: string, newValue: T): boolean
Sets the value for the attribute with the specified attribute name in AppStorage.
This API is deprecated since API version 10. You are advised to use [set10+](#set10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| newValue | T | Yes | Attribute value, which cannot be **undefined** or **null**.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the key-value pair exists and is successfully deleted; returns **false** otherwise.|
| boolean | Returns **true** if the operation is successful; return **false** if the attribute with the specified attribute name does not exist in AppStorage, or the value to set is **undefined** or **null**. |
```ts
let simple = AppStorage.Delete('simpleProp')
AppStorage.SetOrCreate('PropA', 48);
let res: boolean = AppStorage.Set('PropA', 47) // true
let res1: boolean = AppStorage.Set('PropB', 47) // false
```
### keys
keys(): IterableIterator\<string>
### SetOrCreate<sup>(deprecated)</sup>
Searches for all keys.
static SetOrCreate&lt;T&gt;(propName: string, newValue: T): void
Sets a new value for the attribute with the specified attribute name in AppStorage or, if the attribute does not exist, creates one with the specified attribute name and default value.
This API is deprecated since API version 10. You are advised to use [setOrCreate10+](#setorcreate10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------------- |
| propName | string | Yes | Attribute name in AppStorage. |
| newValue | T | Yes | Attribute value, which cannot be **undefined** or **null**.|
```ts
AppStorage.SetOrCreate('simpleProp', 121);
```
### Delete<sup>(deprecated)</sup>
static Delete(propName: string): boolean
Deletes the attribute with the specified attribute name from AppStorage
under the prerequisite that the attribute does not have a subscriber. If there is a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
The subscribers of the attribute are attributes with the same name bound to APIs such as **Link** and **Prop**, **\@StorageLink('propName')**, and **\@StorageProp('propName')**. This means that if **\@StorageLink('propName')** and **\@StorageProp('propName')** are used in a custom component or if there is still a **SubscribedAbstractProperty** instance in sync with the attribute, the attribute cannot be deleted from AppStorage.
This API is deprecated since API version 10. You are advised to use [delete10+](#delete10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| -------------- | -------------- |
| array\<string> | Returns an array of strings containing all keys.|
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
```ts
let simple = AppStorage.Keys()
AppStorage.SetOrCreate('PropA', 47);
AppStorage.Link('PropA');
let res: boolean = AppStorage.Delete('PropA'); // false, PropA still has a subscriber
AppStorage.SetOrCreate('PropB', 48);
let res1: boolean = AppStorage.Delete('PropB'); // true, PropB is deleted from AppStorage successfully
```
### staticClear
staticClear(): boolean
### Keys<sup>(deprecated)</sup>
Deletes all attributes.
static Keys(): IterableIterator&lt;string&gt;
This API is deprecated since API version 9. You are advised to use [Clear](#clear) instead.
Obtains all attribute names in AppStorage.
This API is deprecated since API version 10. You are advised to use [keys10+](#keys10) instead.
**Return value**
| Type | Description |
| ------- | --------------------------------- |
| boolean | Returns **true** if all attributes are deleted; returns **false** if any of the attributes is being referenced by a state variable.|
| Type | Description |
| ------------------------------ | ------------------ |
| IterableIterator&lt;string&gt; | All attribute names in AppStorage.|
```ts
let simple = AppStorage.staticClear()
AppStorage.SetOrCreate('PropB', 48);
let keys: IterableIterator<string> = AppStorage.Keys();
```
### Clear<sup>9+</sup>
Clear(): boolean
### staticClear<sup>(deprecated)</sup>
static staticClear(): boolean
Deletes all attributes.
This API is deprecated since API version 9. You are advised to use [Clear9+](#clear9) instead.
**Return value**
| Type | Description |
| ------- | --------------------------------- |
| boolean | Returns **true** if all attributes are deleted; returns **false** if any of the attributes is being referenced by a state variable.|
```ts
let simple = AppStorage.Clear()
let simple = AppStorage.staticClear();
```
### Clear<sup>(deprecated)</sup>
static Clear(): boolean
Deletes all attributes from AppStorage under the prerequisite that none of the attributes has a subscriber. If any of the attributes has a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
For details about the subscriber, see [Delete](#deletedeprecated).
This API is deprecated since API version 10. You are advised to use [clear10+](#clear10) instead.
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
```typescript
AppStorage.SetOrCreate('PropA', 47);
let res: boolean = AppStorage.Clear(); // true, there are no subscribers
```
### IsMutable
IsMutable(propName: string): boolean
### IsMutable<sup>(deprecated)</sup>
static IsMutable(propName: string): boolean
Checks whether an attribute exists and can be changed.
Checks whether the given attribute in AppStorage name is mutable.
This API is deprecated since API version 10.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | :--- | ------------ |
| propName | string | Yes | Key of the target attribute.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ---------------- |
| propName | string | Yes | Attribute name in AppStorage.|
**Return value**
| Type | Description |
| ------- | ------------------ |
| boolean | Returns whether the attribute exists and can be changed.|
| Type | Description |
| ------- | -------------------------------- |
| boolean | Whether the given attribute in AppStorage name is mutable.|
```ts
let simple = AppStorage.IsMutable('simpleProp')
AppStorage.SetOrCreate('PropA', 47);
let res: boolean = AppStorage.IsMutable('simpleProp');
```
### Size
Size(): number
### Size<sup>(deprecated)</sup>
static Size(): number
Obtains the number of attributes in AppStorage.
Obtains the number of existing key-value pairs.
This API is deprecated since API version 10. You are advised to use [size10+](#size10) instead.
**Return value**
| Type | Description |
| ------ | --------- |
| number | Returns the number of key-value pairs.|
| Type | Description |
| ------ | ------------------- |
| number | Number of attributes in AppStorage.|
```ts
let simple = AppStorage.Size()
AppStorage.SetOrCreate('PropB', 48);
let res: number = AppStorage.Size(); // 1
```
## LocalStorage<sup>9+</sup>
For details about how to use LocalStorage on the UI, see [LocalStorage: UI State Storage](../../quick-start/arkts-localstorage.md).
### constructor<sup>9+</sup>
constructor(initializingProperties?: Object)
Creates and initializes a **LocalStorage** object.
Creates a **LocalStorage** instance and initializes it using the attributes and values returned by **Object.keys(initializingProperties)**.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| ---------------------- | ------ | ---- | ---------------------------------------- |
| initializingProperties | Object | No | All object attributes and their values returned by **object.keys(obj)**.|
| initializingProperties | Object | No | Attributes and values used to initialize the **LocalStorage** instance. The value cannot be **undefined**.|
```ts
let storage = new LocalStorage()
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
```
### GetShared<sup>9+</sup>
static GetShared(): LocalStorage
### getShared<sup>10+</sup>
static getShared(): LocalStorage
Obtains the **LocalStorage** object being shared.
Obtains the **LocalStorage** instance shared by the current stage.
This API can be used only in the stage model.
Since API version 9, this API is supported in ArkTS widgets.
**Model restriction**: This API can be used only in the stage model.
**Return value**
| Type | Description |
| ----------------------------- | ----------------- |
| [LocalStorage](#localstorage) | **LocalStorage** object.|
| Type | Description |
| ------------------------------ | ----------------- |
| [LocalStorage](#localstorage9) | **LocalStorage** instance.|
For details about how to use **getShared**, see [Sharing a LocalStorage Instance from UIAbility to One or More Pages](../../quick-start/arkts-localstorage.md#sharing-a-localstorage-instance-from-uiability-to-one-or-more-pages).
```ts
let storage = LocalStorage.GetShared()
```
### has<sup>9+</sup>
has(propName: string): boolean
Checks whether the **LocalStorage** contains the specified attribute.
Checks whether the attribute with the specified attribute name exists in LocalStorage.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------- |
| propName | string | Yes | Key of the attribute.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------ |
| propName | string | Yes | Attribute name in LocalStorage.|
**Return value**
| Type | Description |
| ------- | ------------- |
| boolean | Returns whether the attribute exists.|
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the attribute with the specified attribute name exists in AppStorage; returns **false** otherwise.|
```ts
let storage = new LocalStorage()
storage.has('storageSimpleProp')
```
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
storage.has('PropA'); // true
```
### get<sup>9+</sup>
get\<T>(propName: string): T
get&lt;T&gt;(propName: string): T | undefined
Obtains the value of the specified key.
Obtains the attribute with the specified attribute name in LocalStorage.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------- |
| propName | string | Yes | Key of the value to obtain.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------ |
| propName | string | Yes | Attribute name in LocalStorage.|
**Return value**
| Type | Description |
| -------------- | ---------------------------------------- |
| T \| undefined | Returns the value of the specified key if it exists; returns **undefined** otherwise.|
| Type | Description |
| ------------------------ | ---------------------------------------- |
| T \| undefined | Returns the attribute with the specified attribute name in LocalStorage; returns **undefined** if the attribute does not exist.|
```ts
let storage = new LocalStorage()
let simpleValue = storage.get('storageSimpleProp')
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let value: number = storage.get('PropA'); // 47
```
### set<sup>9+</sup>
set\<T>(propName: string, newValue: T): boolean
set&lt;T&gt;(propName: string, newValue: T): boolean
Sets a value for the attribute with the specified attribute name in LocalStorage.
Sets a new value for the specified key.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------- |
| propName | string | Yes | Key to set. |
| newValue | T | Yes | Value to set.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------- |
| propName | string | Yes | Attribute name in LocalStorage. |
| newValue | T | Yes | Attribute value, which cannot be **undefined** or **null**.|
**Return value**
| Type | Description |
| ------- | ----------------------------------- |
| boolean | Returns **true** and the value if the key exists; returns **false** otherwise.|
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; return **false** if the attribute with the specified attribute name does not exist in LocalStorage, or the value to set is **undefined** or **null**. |
```ts
let storage = new LocalStorage()
storage.set('storageSimpleProp', 121)
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let res: boolean = storage.set('PropA', 47); // true
let res1: boolean = storage.set('PropB', 47); // false
```
### setOrCreate<sup>9+</sup>
setOrCreate\<T>(propName: string, newValue: T): boolean
setOrCreate&lt;T&gt;(propName: string, newValue: T): boolean
Creates or updates the value of the specified key.
Sets a new value for the attribute with the specified attribute name in LocalStorage or, if the attribute does not exist, creates one with the specified attribute name and default value.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | :--- | -------------- |
| propName | string | Yes | Key of the value to create or update. |
| newValue | T | Yes | Value to be updated or created.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------------------- |
| propName | string | Yes | Attribute name in LocalStorage. |
| newValue | T | Yes | Attribute value, which cannot be **undefined** or **null**.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Updates the value of the attribute and returns **true** if an attribute that has the same name as the specified key exists; creates an attribute with the specified value as its default value and returns false otherwise. **undefined** and **null** are not allowed.|
| boolean | Returns **false** if **newValue** is set to **undefined** or **null**.<br>Updates the target attribute with the new value and returns **true** if the attribute exists in LocalStorage.<br>Creates an attribute with the specified attribute name and default value if the attribute does not exist in LocalStorage.|
```ts
let storage = new LocalStorage()
storage.setOrCreate('storageSimpleProp', 121)
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let res: boolean =storage.setOrCreate('PropA', 121); // true
let res1: boolean =storage.setOrCreate('PropB', 111); // true
let res2: boolean =storage.setOrCreate('PropB', undefined); // false
```
### link<sup>9+</sup>
link\<T>(propName: string): T
link&lt;T&gt;(propName: string): SubscribedAbstractProperty&lt;T&gt;
Establishes two-way data binding with the given attribute in this **LocalStorage** instance. If the given attribute exists, the two-way bound data of the attribute in LocalStorage is returned.
Establishes two-way data binding between an attribute and this **LocalStorage** instance.
Any update of the data is synchronized back to LocalStorage, which then synchronizes the update to all data and custom components bound to the attribute.
If the given attribute does not exist in LocalStorage, **undefined** is returned.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ----------- |
| propName | string | Yes | Name of the target attribute.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------ |
| propName | string | Yes | Attribute name in LocalStorage.|
**Return value**
| Type | Description |
| ---- | ---------------------------------------- |
| T | Returns two-way binding to this attribute if there is data with a given key. This means that attribute changes made by a variable or component will be synchronized to the **LocalStorage**, and attribute changes made through the **LocalStorage** will be synchronized to the variable or component. returns **undefined** if the attribute with the given key does not exist.|
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;T&gt; | Returns the **SubscribedAbstractProperty<T>** instance if the given attribute exists in AppStorage; returns **undefined** otherwise.|
```ts
let storage = new LocalStorage()
let localStorage = storage.link('storageSimpleProp')
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let linkToPropA1: SubscribedAbstractProperty<number> = storage.link('PropA');
let linkToPropA2: SubscribedAbstractProperty<number> = storage.link('PropA'); // linkToPropA2.get() == 47
linkToPropA1.set(48); // Two-way synchronization: linkToPropA1.get() == linkToPropA2.get() == 48
```
### setAndLink<sup>9+</sup>
setAndLink\<T>(propName: string, defaultValue: T): T
setAndLink&lt;T&gt;(propName: string, defaultValue: T): SubscribedAbstractProperty&lt;T&gt;
Works in a way similar to the **Link** API.
Works in a way similar to the **Link** API. If the given attribute exists in LocalStorage, the two-way bound data of the attribute in LocalStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in LocalStorage, and two-way bound data is returned.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ----------- |
| propName | string | Yes | Target key.|
| defaultValue | T | Yes | Default value to set. |
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| propName | string | Yes | Attribute name in LocalStorage. |
| defaultValue | T | Yes | Default value used to initialize the attribute with the specified attribute name in LocalStorage.|
**Return value**
| Type | Description |
| ----- | ---------------------------------------- |
| @Link | Returns the value corresponding to the key if the current key is stored in the **LocalStorage**; creates and returns a **Link** instance corresponding to the default value if the key has not been created.|
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;T&gt; | Returns the **SubscribedAbstractProperty<T>** instance if the given attribute exists in AppStorage; returns **undefined** otherwise.|
```ts
let storage = new LocalStorage()
let localStorage = storage.setAndLink('storageSimpleProp', 121)
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let link1: SubscribedAbstractProperty<number> = storage.setAndLink('PropB', 49); // Create PropB 49
var link2: SubscribedAbstractProperty<number> = storage.setAndLink('PropA', 50); // PropA exists, remains 47
```
### prop<sup>9+</sup>
prop\<T>(propName: string): T
prop&lt;S&gt;(propName: string): SubscribedAbstractProperty&lt;S&gt;
Establishes one-way data binding with the given attribute in this **LocalStorage** instance. If the given attribute exists, the one-way bound data of the attribute in LocalStorage is returned. If the given attribute does not exist in LocalStorage, **undefined** is returned. Updates of the one-way bound data are not synchronized back to LocalStorage.
Establishes one-way data binding with an attribute to update its status.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------- |
| propName | string | Yes | Key of the attribute.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------ |
| propName | string | Yes | Attribute name in LocalStorage.|
**Return value**
| Type | Description |
| ----- | ---------------------------------------- |
| @Prop | Returns one-way binding to an attribute with a given key if the attribute exists; returns **undefined** otherwise. One-way binding means that attribute changes made through the **LocalStorage** will be synchronized to the variable or component, but attribute changes made by the variable or component will not be synchronized to the **LocalStorage**. This API returns immutable variables and is applicable to mutable and immutable state variables alike. |
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;S&gt; | Returns the **SubscribedAbstractProperty&lt;S&gt;** instance if the given attribute exists in LocalStorage; returns **undefined** otherwise.|
```ts
let storage = new LocalStorage()
let localStorage = storage.prop('storageSimpleProp')
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let prop1: SubscribedAbstractProperty<number> = storage.prop('PropA');
let prop2: SubscribedAbstractProperty<number> = storage.prop('PropA');
prop1.set(1); // one-way sync: prop1.get()=1; but prop2.get() == 47
```
### setAndProp<sup>9+</sup>
setAndProp\<T>(propName: string, defaultValue: T): T
setAndProp&lt;S&gt;(propName: string, defaultValue: S): SubscribedAbstractProperty&lt;S&gt;
Works in a way similar to the **Prop** API.
Establishes one-way data binding with the given attribute in this **LocalStorage** instance. If the given attribute exists, the one-way bound data of the attribute in LocalStorage is returned. If the given attribute does not exist, it is created and initialized with **defaultValue** in LocalStorage, and one-way bound data is returned.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | -------------- |
| propName | string | Yes | Key of the target key-value pair.|
| defaultValue | T | Yes | Default value to set. |
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| propName | string | Yes | Attribute name in LocalStorage. |
| defaultValue | S | Yes | Default value used to initialize the attribute with the specified attribute name in LocalStorage.|
**Return value**
| Type | Description |
| ----- | ---------------------------------------- |
| @Prop | Returns the value corresponding to the given key if the key is stored in the **LocalStorage**; creates and returns a **Prop** instance corresponding to the default value if the key has not been created.|
| Type | Description |
| ----------------------------------- | ---------------------------------------- |
| SubscribedAbstractProperty&lt;S&gt; | Instance of **SubscribedAbstractProperty&lt;T&gt;** and one-way bound data of the given attribute in LocalStorage.|
```ts
let storage = new LocalStorage()
let localStorage = storage.setAndProp('storageSimpleProp', 121)
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let prop: SubscribedAbstractProperty<number> = storage.setAndProp('PropB', 49); // PropA -> 47, PropB -> 49
```
### delete<sup>9+</sup>
delete(propName: string): boolean
Deletes the key-value pair that matches the specified key.
Deletes the attribute with the specified attribute name from LocalStorage under the prerequisite that the attribute does not have a subscriber. If the deletion is successful, **true** is returned.
The subscribers of the attribute are attributes with the same name bound to the **Link** and **Prop** APIs, **\@LocalStorageLink('propName')**, and **\@LocalStorageProp('propName')**. This means that if **\@LocalStorageLink('propName')** and **\@LocalStorageProp('propName')** are used in a custom component or if there is still a **SubscribedAbstractProperty** instance (return type of the **link** and **prop** APIs) in sync with the attribute, the attribute cannot be deleted from LocalStorage.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ------ | :--- | ------------ |
| propName | string | Yes | Key of the target key-value pair.|
| Name | Type | Mandatory | Description |
| -------- | ------ | ---- | ------------------ |
| propName | string | Yes | Attribute name in LocalStorage.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the key-value pair exists and is successfully deleted; returns **false** otherwise.|
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
```ts
let storage = new LocalStorage()
storage.delete('storageSimpleProp')
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
storage.link('PropA');
let res: boolean = storage.delete('PropA'); // false, PropA still has a subscriber
let res1: boolean = storage.delete('PropB'); // false, PropB is not in storage
storage.setOrCreate('PropB', 48);
let res2: boolean = storage.delete('PropB'); // true, PropB is deleted from storage successfully
```
### keys<sup>9+</sup>
keys(): IterableIterator\<string>
keys(): IterableIterator&lt;string&gt;
Obtains all attribute names in LocalStorage.
Searches for all keys.
Since API version 9, this API is supported in ArkTS widgets.
**Return value**
| Type | Description |
| -------------- | ------------------- |
| array\<string> | Returns an array of strings containing all keys that are not serializable.|
| Type | Description |
| ------------------------------ | -------------------- |
| IterableIterator&lt;string&gt; | All attribute names in LocalStorage.|
```ts
let storage = new LocalStorage()
let simple = storage.keys()
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let keys: IterableIterator<string> = storage.keys();
```
### size<sup>9+</sup>
size(): number
Obtains the number of existing key-value pairs.
Obtains the number of attributes in LocalStorage.
Since API version 9, this API is supported in ArkTS widgets.
**Return value**
| Type | Description |
| ------ | --------- |
| number | Returns the number of key-value pairs.|
| number | Number of attributes in LocalStorage.|
```ts
let storage = new LocalStorage()
let simple = storage.size()
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let res: number = storage.size(); // 1
```
### Clear<sup>9+</sup>
### clear<sup>9+</sup>
clear(): boolean
Deletes all attributes.
Deletes all attributes from LocalStorage under the prerequisite that none of the attributes has a subscriber. If any of the attributes has a subscriber, **false** is returned. If the deletion is successful, **true** is returned.
Since API version 9, this API is supported in ArkTS widgets.
**Return value**
| Type | Description |
| ------- | --------------------------------- |
| boolean | Returns **true** if all attributes are deleted; returns **false** if any of the attributes is being referenced by a state variable.|
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **true** if the operation is successful; returns **false** otherwise.|
```ts
let storage = new LocalStorage()
let simple = storage.clear()
let storage: LocalStorage = new LocalStorage({ 'PropA': 47 });
let res: boolean = storage.clear(); // true, there are no subscribers
```
### GetShared<sup>(deprecated)</sup>
static GetShared(): LocalStorage
Obtains the **LocalStorage** instance shared by the current stage.
Since API version 9, this API is supported in ArkTS widgets.
This API is deprecated since API version 10. You are advised to use [getShared10+](#getshared10) instead.
**Model restriction**: This API can be used only in the stage model.
**Return value**
| Type | Description |
| ------------------------------ | ----------------- |
| [LocalStorage](#localstorage9) | **LocalStorage** instance.|
```ts
let storage: LocalStorage = LocalStorage.GetShared();
```
## SubscribedAbstractProperty
### get<sup>9+</sup>
abstract get(): T
Obtains attribute data synchronized from AppStorage or LocalStorage.
Since API version 9, this API is supported in ArkTS widgets.
**Return value**
| Type | Description |
| ---- | ------------------------------- |
| T | Attribute data synchronized from AppStorage or LocalStorage.|
```ts
AppStorage.SetOrCreate('PropA', 47);
let prop1 = AppStorage.Prop('PropA');
prop1.get(); // prop1.get()=47
```
### set<sup>9+</sup>
abstract set(newValue: T): void
Sets the attribute data synchronized from AppStorage or LocalStorage.
Since API version 9, this API is supported in ArkTS widgets.
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ---- | ---- | ------- |
| newValue | T | Yes | Data to set.|
```
AppStorage.SetOrCreate('PropA', 47);
let prop1 = AppStorage.Prop('PropA');
prop1.set(1); // prop1.get()=1
```
### aboutToBeDeleted<sup>10+</sup>
abstract aboutToBeDeleted(): void
Cancels one-way or two-way synchronization between the **SubscribedAbstractProperty** instance and AppStorage or LocalStorage.
```ts
AppStorage.SetOrCreate('PropA', 47);
let link = AppStorage.SetAndLink('PropB', 49); // PropA -> 47, PropB -> 49
link.aboutToBeDeleted();
link.set(50); // PropB -> 49, link.get() --> undefined
```
## PersistentStorage
### constructor
constructor(appStorage: AppStorage, storage: Storage)
For details about how to use PersistentStorage on the UI, see [PersistentStorage: Application State Persistence](../../quick-start/arkts-persiststorage.md).
### PersistPropsOptions
| Name | Type | Mandatory| Description |
| ------------ | ----------------------- | ---- | ------------------------------------------------------------ |
| key | string | Yes | Attribute name. |
| defaultValue | number\|string\|boolean | Yes | Default value used to initialize the created attribute. The value cannot be **undefined** or **null**.|
### persistProp<sup>10+</sup>
static persistProp&lt;T&gt;(key: string, defaultValue: T): void
Persists the attribute with the specified key in AppStorage to a file. This API is usually called before access to AppStorage.
The sequence of determining the type and value of an attribute is as follows:
1. If the PersistentStorage file contains the attribute with the specified key, an attribute with the key as the name is created in AppStorage and initialized with the attribute of the key found in PersistentStorage.
2. If the attribute with the specified key is not found in the PersistentStorage file, AppStorage is searched for the attribute corresponding to the key. If the matching attribute is found, it is persisted.
3. If no matching attribute is found in AppStorage, it is created in AppStorage, initialized with the value of **defaultValue**, and persisted.
Creates a **persistentstorage** object.
According to the preceding initialization process, if AppStorage contains the matching attribute, the value of this attribute is used to overwrite the value in the PersistentStorage file. Because AppStorage stores data in the memory, the attribute value becomes impersistent.
**Parameters**
| Name | Type | Mandatory | Description |
| ---------- | ---------- | ---- | ---------------- |
| appStorage | AppStorage | Yes | Singleton object that saves all attributes and attribute values.|
| storage | Storage | Yes | **Storage** object. |
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| key | string | Yes | Attribute name. |
| defaultValue | T | Yes | Default value used to initialize the created attribute. The value cannot be **undefined** or **null**.|
**Example:**
For details about how to use persistProp, see [Accessing PersistentStorage Initialized Attribute from AppStorage](../../quick-start/arkts-persiststorage.md#accessing-persistentstorage-initialized-attribute-from-appstorage).
### deleteProp<sup>10+</sup>
static deleteProp(key: string): void
Performs the reverse operation of **PersistProp**. Specifically, this API deletes the attribute corresponding to the key from PersistentStorage. Subsequent AppStorage operations do not affect data in PersistentStorage.
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------------------- |
| key | string | Yes | Attribute name in PersistentStorage.|
```ts
let persistentstorage = new PersistentStorage(AppStorage,Storage)
PersistentStorage.deleteProp('highScore');
```
### PersistProp
PersistProp(key:string,defaultValue:T): void
### persistProps<sup>10+</sup>
Changes the attribute that matches the specified key to persistent data in the **AppStorage**.
static persistProps(props: PersistPropsOptions[]): void
Works in a way similar to the **PersistProp** API, with the difference that it allows for persistence in batches and is therefore ideal for initialization during application startup.
**Parameters**
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | -------------- |
| key | string | Yes | Key of the target attribute. |
| defaultValue | T | Yes | Value of the target attribute.|
| Name | Type | Mandatory | Description |
| ---------- | ---------------------------------------- | ---- | ---------------------------------------- |
| props | [PersistPropsOptions](#persistpropsoptions)[] | Yes| Array of persistent attributes.|
```ts
PersistentStorage.persistProps([{ key: 'highScore', defaultValue: '0' }, { key: 'wightScore', defaultValue: '1' }]);
```
### keys<sup>10+</sup>
static keys(): Array&lt;string&gt;
Obtains an array of keys for all persistent attributes.
**Return value**
| Type | Description |
| ------------------- | ----------------- |
| Array&lt;string&gt; | Array of keys of all persistent attributes.|
```ts
PersistentStorage.PersistProp('highScore', '0')
let keys: Array<string> = PersistentStorage.keys();
```
### DeleteProp
DeleteProp(key: string): void
### PersistProp<sup>(deprecated)</sup>
static PersistProp&lt;T&gt;(key: string, defaultValue: T): void
Persists the attribute with the specified key in AppStorage to a file. This API is usually called before access to AppStorage.
The sequence of determining the type and value of an attribute is as follows:
1. If the PersistentStorage file contains the attribute with the specified key, an attribute with the key as the name is created in AppStorage and initialized with the attribute of the key found in PersistentStorage.
2. If the attribute with the specified key is not found in the PersistentStorage file, AppStorage is searched for the attribute corresponding to the key. If the matching attribute is found, it is persisted.
3. If no matching attribute is found in AppStorage, it is created in AppStorage, initialized with the value of **defaultValue**, and persisted.
According to the preceding initialization process, if AppStorage contains the matching attribute, the value of this attribute is used to overwrite the value in the PersistentStorage file. Because AppStorage stores data in the memory, the attribute value becomes impersistent.
Cancels two-way binding. The value of this attribute will be deleted from the persistent storage.
This API is deprecated since API version 10. You are advised to use [persistProp10+](#persistprop10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ------------ |
| key | string | Yes | Key of the target attribute.|
| Name | Type | Mandatory | Description |
| ------------ | ------ | ---- | ---------------------------------------- |
| key | string | Yes | Attribute name. |
| defaultValue | T | Yes | Default value used to initialize the created attribute. The value cannot be **undefined** or **null**.|
**Example:**
```ts
PersistentStorage.DeleteProp('highScore')
PersistentStorage.PersistProp('highScore', '0');
```
### PersistProps
PersistProps(properties: {key: string, defaultValue: any}[]): void
### DeleteProp<sup>(deprecated)</sup>
static DeleteProp(key: string): void
Changes the attributes that match the specified keys to persistent data in the **AppStorage**.
Performs the reverse operation of **PersistProp**. Specifically, this API deletes the attribute corresponding to the key from PersistentStorage. Subsequent AppStorage operations do not affect data in PersistentStorage.
This API is deprecated since API version 10. You are advised to use [deleteProp10+](#deleteprop10) instead.
**Parameters**
| Name | Type | Mandatory | Description |
| ---- | ---------------------------------- | ---- | --------- |
| key | {key: string, defaultValue: any}[] | Yes | Keys of the target attributes.|
| Name | Type | Mandatory | Description |
| ---- | ------ | ---- | ----------------------- |
| key | string | Yes | Attribute name in PersistentStorage.|
```ts
PersistentStorage.PersistProps([{key: 'highScore', defaultValue: '0'},{key: 'wightScore',defaultValue: '1'}])
PersistentStorage.DeleteProp('highScore');
```
### Keys
Keys(): Array\<string>
### PersistProps<sup>(deprecated)</sup>
static PersistProps(properties: {key: string, defaultValue: any;}[]): void
Works in a way similar to the **PersistProp** API, with the difference that it allows for persistence in batches and is therefore ideal for initialization during application startup.
This API is deprecated since API version 10. You are advised to use [persistProps10+](#persistprops10) instead.
**Parameters**
| Name | Type | Mandatory| Description |
| ---------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ |
| properties | {key: string, defaultValue: any}[] | Yes | Array of attributes to persist.<br>**key**: attribute name.<br>**defaultValue**: default value. The rules are the same as those of **PersistProp**.|
```ts
PersistentStorage.PersistProps([{ key: 'highScore', defaultValue: '0' }, { key: 'wightScore', defaultValue: '1' }]);
```
Returns the flags of all persistent attributes.
### Keys<sup>(deprecated)</sup>
static Keys(): Array&lt;string&gt;
Obtains an array of keys for all persistent attributes.
This API is deprecated since API version 10. You are advised to use [keys10+](#keys10-1) instead.
**Return value**
| Type | Description |
| -------------- | ------------- |
| Array\<string> | Returns the flags of all persistent attributes.|
| Type | Description |
| ------------------- | ----------------- |
| Array&lt;string&gt; | Array of keys of all persistent attributes.|
```ts
let simple = PersistentStorage.Keys()
let keys: Array<string> = PersistentStorage.Keys();
```
> **NOTE**
>
> - When using **PersistProp**, ensure that the input key exists in the **AppStorage**.
>
> - **DeleteProp** takes effect only for the data that has been linked during the current startup.
## Environment
### constructor
Creates an **Environment** object.
For details about how to use Environment, see [Environment: Device Environment Query](../../quick-start/arkts-environment.md).
### EnvPropsOptions
| Name | Type | Mandatory| Description |
| ------------ | ----------------------- | ---- | ------------------------------------------------------------ |
| key | string | Yes | Environment variable name. For details about the value range, see [Built-in Environment Variables](#built-in-environment-variables).|
| defaultValue | number\|string\|boolean | Yes | Default value used if the value of the environment variable key is not found in AppStorage.|
### envProp<sup>10+</sup>
static envProp&lt;S&gt;(key: string, value: S): boolean
Saves the built-in environment variable key in environment to AppStorage. If the value of the environment variable key is not found in AppStorage, the default value is used. If the value is successfully saved, **true** is returned. If the value of the environment variable key is found in AppStorage, **false** is returned.
You are advised to call this API when the application is started.
It is incorrect to use AppStorage to read environment variables without invoking **EnvProp**.
**Parameters**
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | --------------------------------------- |
| key | string | Yes | Environment variable name. For details about the value range, see [Built-in Environment Variables](#built-in-environment-variables). |
| value | S | Yes | Default value used if the value of the environment variable key is not found in AppStorage.|
**Return value**
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **false** if the attribute corresponding to the key exists in AppStorage; returns **false** otherwise.|
**Example:**
For details about how to use **envProp**, see [Accessing Environment Parameters from UI](../../quick-start/arkts-environment.md#accessing-environment-parameters-from-ui).
### envProps<sup>10+</sup>
static envProps(props: EnvPropsOptions[]): void
Works in a way similar to the **EnvProp** API, with the difference that it allows for initialization of multiple attributes in batches. You are advised to call this API during application startup to save system environment variables to AppStorage in batches.
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | --------------------------------------------- | ---- | ------------------------------------ |
| props | [EnvPropsOptions](#envpropsoptions)[] | Yes | Array of key-value pairs consisting of system environment variables and default values.|
```ts
Environment.envProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, {
key: 'languageCode',
defaultValue: 'en'
}, { key: 'prop', defaultValue: 'hhhh' }]);
```
### keys<sup>10+</sup>
static keys(): Array&lt;string&gt;
Array of keys of environment variables.
**Return value**
| Type | Description |
| ------------------- | ----------- |
| Array&lt;string&gt; | Returns an array of associated system attributes.|
```ts
let simple = new Environment()
Environment.envProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, {
key: 'languageCode',
defaultValue: 'en'
}, { key: 'prop', defaultValue: 'hhhh' }]);
let keys: Array<string> = Environment.keys(); // accessibilityEnabled, languageCode, prop
```
### EnvProp
EnvProp\<S>(key: string, value: S): boolean
### EnvProp<sup>(deprecated)</sup>
static EnvProp&lt;S&gt;(key: string, value: S): boolean
Saves the built-in environment variable key in environment to AppStorage. If the value of the environment variable key is not found in AppStorage, the default value is used. If the value is successfully saved, **true** is returned. If the value of the environment variable key is found in AppStorage, **false** is returned.
You are advised to call this API when the application is started.
Binds this system attribute to the **AppStorage**. You are advised to use this API during application startup. If the attribute already exists in the **AppStorage**, **false** is returned. Do not use the variables in the **AppStorage**. Instead, call this API to bind environment variables.
It is incorrect to use AppStorage to read environment variables without invoking **EnvProp**.
This API is deprecated since API version 10. You are advised to use [envProp10+](#envprop10) instead.
**Parameters**
| Name | Type | Mandatory | Description | Description |
| ----- | ------ | ---- | ---------- | ------------------------- |
| key | string | Yes | Key of the target attribute. | For details, see **Built-in environment variables**.|
| value | S | Yes | Value of the target attribute.| Value of the target attribute. |
| Name | Type | Mandatory | Description |
| ----- | ------ | ---- | --------------------------------------- |
| key | string | Yes | Environment variable name. For details about the value range, see [Built-in Environment Variables](#built-in-environment-variables). |
| value | S | Yes | Default value used if the value of the environment variable key is not found in AppStorage.|
**Return value**
| Type | Description |
| ------- | ---------------------- |
| boolean | Returns whether the attribute exists in the **AppStorage**.|
| Type | Description |
| ------- | ---------------------------------------- |
| boolean | Returns **false** if the attribute corresponding to the key exists in AppStorage; returns **false** otherwise.|
**Built-in environment variables**
**Example:**
| key | Type | Description |
| -------------------- | --------------- | ---------------------------------------- |
| accessibilityEnabled | string | Whether to enable accessibility. |
| colorMode | ColorMode | Color mode. The options are as follows:<br>- **ColorMode.LIGHT**: light mode.<br>- **ColorMode.DARK**: dark mode.|
| fontScale | number | Font scale. |
| fontWeightScale | number | Font weight scale. |
| layoutDirection | LayoutDirection | Layout direction. The options are as follows:<br>- **LayoutDirection.LTR**: The direction is from left to right.<br>- **LayoutDirection.RTL**: The direction is from right to left.|
| languageCode | string | Current system language. The value is in lowercase, for example, **zh**. |
```ts
Environment.EnvProp('accessibilityEnabled', 'default')
Environment.EnvProp('accessibilityEnabled', 'default');
```
### EnvProps
EnvProps(props: {key: string, defaultValue: any}[]): void
### EnvProps<sup>(deprecated)</sup>
static EnvProps(props: {key: string; defaultValue: any;}[]): void
Works in a way similar to the **EnvProp** API, with the difference that it allows for initialization of multiple attributes in batches. You are advised to call this API during application startup to save system environment variables to AppStorage in batches.
Associates this system item array with the **AppStorage**.
This API is deprecated since API version 10. You are advised to use [envProps10+](#envprops10) instead.
**Parameters**
| Name | Type | Mandatory | Description | Description |
| ---- | ---------------------------------- | ---- | --------- | --------- |
| key | {key: string, defaultValue: any}[] | Yes | Keys of the target attributes.| Keys of the target attributes.|
| Name | Type | Mandatory | Description |
| ----- | ---------------------------------------- | ---- | ------------------ |
| props | {key: string, defaultValue: any}[] | Yes | Array of key-value pairs consisting of system environment variables and default values.|
```ts
Environment.EnvProps([{key: 'accessibilityEnabled', defaultValue: 'default'},{key: 'accessibilityUnEnabled', defaultValue: 'undefault'}])
Environment.EnvProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, {
key: 'languageCode',
defaultValue: 'en'
}, { key: 'prop', defaultValue: 'hhhh' }]);
```
### Keys
Keys(): Array\<string>
### Keys<sup>(deprecated)</sup>
static Keys(): Array&lt;string&gt;
Returns an array of associated system attributes.
Array of keys of environment variables.
This API is deprecated since API version 10. You are advised to use [keys10+](#keys10-2) instead.
**Return value**
| Type | Description |
| -------------- | ----------- |
| Array\<string> | Returns an array of associated system attributes.|
| Type | Description |
| ------------------- | ----------- |
| Array&lt;string&gt; | Returns an array of associated system attributes.|
```ts
let simple = Environment.Keys()
Environment.EnvProps([{ key: 'accessibilityEnabled', defaultValue: 'default' }, {
key: 'languageCode',
defaultValue: 'en'
}, { key: 'prop', defaultValue: 'hhhh' }]);
let keys: Array<string> = Environment.Keys(); // accessibilityEnabled, languageCode, prop
```
## Built-in Environment Variables
| key | Type | Description |
| -------------------- | --------------- | ---------------------------------------- |
| accessibilityEnabled | string | Whether to enable accessibility. |
| colorMode | ColorMode | Color mode. The options are as follows:<br>- **ColorMode.LIGHT**: light mode.<br>- **ColorMode.DARK**: dark mode.|
| fontScale | number | Font scale. |
| fontWeightScale | number | Font weight scale. |
| layoutDirection | LayoutDirection | Layout direction. The options are as follows:<br>- **LayoutDirection.LTR**: from left to right.<br>- **LayoutDirection.RTL**: from right to left.|
| languageCode | string | Current system language. The value is in lowercase, for example, **zh**. |
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册