1 Star 0 Fork 5.2K

OpenHarmony-build / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ts-application-states-apis-persistentstorage.md 5.99 KB
一键复制 编辑 原始数据 按行查看 历史
zengyawen 提交于 2021-11-10 20:02 . add arkui

持久化数据管理

PersistentStorage用于管理应用持久化数据。此对象可以将特定标记的持久化数据链接到AppStorage中,并由AppStorage接口访问对应持久化数据,或者通过@StorageLink修饰器来访问对应key的变量。

PersistentStorage接口

方法

参数说明

返回值

定义

PersistProp

key : string

defaultValue: T

void

关联命名的属性在AppStorage变为持久化数据。赋值覆盖顺序

首先 如果此属性在AppStorage中存在,并且将Persistent中的数据复写为AppStorage中的属性值。

其次Persistent中有此命名的属性,使用Persistent中的属性值。

最后 以上条件不满足使用defaultValue,不支持null和undefined。

DeleteProp

key: string

void

取消双向数据绑定,该属性值将从持久存储中删除。

PersistProps

keys: {

key: string,

defaultValue: any

}[]

void

关联多个命名的属性绑定。

Keys

void

Array<string>

返回所有持久化属性的标记。

说明:

  • PersistProp接口使用时,需要保证输入对应的key应当在Appstorage存在。
  • DeleteProp接口使用时,只能对本次启动已经link过的数据生效。
PersistentStorage.PersistProp("highScore", "0");

@Entry
@Component
struct PersistentComponent {
    @StorageLink('highScore') highScore: string = '0'
    @State currentScore: number = 0
    build() {
        Column() {
            if (this.currentScore === Number(this.highScore)) {
                Text(`new highScore : ${this.highScore}`)
            }
            Button() {
                Text(`goal!, currentScore : ${this.currentScore}`)
                    .fontSize(10)
            }.onClick(() => {
                this.currentScore++
                if (this.currentScore > Number(this.highScore)) {
                    this.highScore = this.currentScore.toString()
                }
            })
        }
    }
}
1
https://gitee.com/openharmony-build/docs.git
git@gitee.com:openharmony-build/docs.git
openharmony-build
docs
docs
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891