# AppStorage **AppStorage** is a singleton object in an application and is created by the UI framework when the application is started. It is designed to provide central storage for variable application state attributes. **AppStorage** contains all the state attributes that need to be accessed throughout the application. The **AppStorage** retains all attributes and their values as long as the application remains running, and the attribute values can be accessed through unique key values. The UI component can synchronize the application state data with the **AppStorage** through the decorators. The implementation of the application service logic can also access the **AppStorage** through APIs. The selection state property of the **AppStorage** can be synchronized with different data sources or data sinks. These data sources and data sinks can be local or remote on the device and have different functions, such as data persistence. Such data sources and data sinks can be implemented independently of the UI in service logics. By default, the attributes in the **AppStorage** are changeable, and **AppStorage** can also use immutable \(read-only\) attributes. ## AppStorage APIs

Name

Type

Return Value

Definition

Link

key: string

@Link

Returns a bidirectional data binding to this attribute if there is data with a given key, meaning that changes made to the data by a variable or component will be synchronized to the AppStorage, and changes made to the data by the AppStorage will be synchronized to the variable or component. If the attribute with this key does not exist or is read-only, undefined is returned.

SetAndLink

key : String

defaultValue: T

@Link

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.

Prop

key: string

@Prop

Returns a one-way data binding to an attribute with a given key if the attribute exists. This one-way binding means that changes to the attribute can only be synchronized to variables or components through AppStorage. The variable returned by this method is an immutable variable, which is applicable to the variable and immutable state attributes. If the attribute with this key does not exist, undefined is returned.

NOTE:

The attribute value type corresponding to the prop method is of a simple type.

SetAndProp

key : string

defaultValue: S

@Prop

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.

Has

key: string

boolean

Checks whether the attribute corresponding to the key value exists.

Keys

void

array<string>

Returns an array of strings containing all keys.

Get

string

T or undefined

Obtains the value of the key.

Set

string, newValue : T

void

Replaces the value of a saved key.

SetOrCreate

string, newValue : T

boolean

If an attribute with the same name exists: returns true if the attribute can be modified, and false otherwise.

If the attribute with the same name does not exist: Create the first attribute whose value is the defaultValue. The null and undefined values are not supported.

Delete

key : string

boolean

Deletes an attribute. If the attribute exists, true is returned. Otherwise, false is returned.

Clear

none

boolean

Deletes all attributes. If a state variable still references any of the attributes, false is returned.

IsMutable

key: string

  

Specifies whether the attribute exists and can be changed.

>![](../../public_sys-resources/icon-note.gif) **NOTE:** >Currently, the API can process only basic data and cannot modify a value in an object. ## Example ``` let link1 = AppStorage.Link('PropA') let link2 = AppStorage.Link('PropA') let prop = AppStorage.Prop('PropA') link1 = 47 // causes link1 == link2 == prop == 47 link2 = link1 + prop // causes link1 == link2 == prop == 94 prop = 1 // error, prop is immutable ```