【HarmonyOS】API6使用storage实现轻量级数据存储

写在前面

本篇内容基于API6 JS语言进行开发,通过结合轻量级数据存储开发指导的文档,帮助大家完成一个实际的代码案例,通过这个小案例,可以实现简单数据的存储。

参考文档:文档中心

1、页面布局

首先我们编写一个简单的页面布局,页面中只有一个文本和两个按钮,文本用来展示存储的数据结果,按钮用来控制存储和获取数据,代码如下:

javascript 复制代码
<div class="container">
    <text>{{content}}</text>
    <div class="option">
        <button class="button" value="存储" "onclickSave"></button>
        <button class="button" value="获取" "onclickGet"></button>
    </div>
</div>

2、页面样式

然后我们简单处理一下页面的样式,代码如下:

javascript 复制代码
.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: 20px;
}
.option {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    margin-top: 20px;
    margin-right: 20px;
}
.button {
    font-size: 15px;
    text-align: center;
    margin-left: 10px;
    margin-right: 10px;
    width: 200px;
    height: 40px;
}

3、页面逻辑

最后,我们来实现页面中的数据存储以及数据获取的逻辑,相关代码参考本文开头部分的官网文档:

javascript 复制代码
import featureAbility from '@ohos.ability.featureAbility'; // 用于获取文件存储路径
import prompt from '@system.prompt';
import storage from '@ohos.data.storage';

export default {
    data: {
        content: "",
    },
    onclickSave() {
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            storage.getStorage(filePath + '/mystore').then((storage) => {
                // 保存数据到缓存的storage实例中
                let getPromise = storage.put('qwe', true);
                getPromise.then(() => {
                    storage.flush();
                    console.info("jarchie---保存成功");
                }).catch((err) => {
                    console.info("jarchie---保存失败" + err);
                })
            }).catch((err) => {
                console.info("jarchie---获取storage失败" + err);
            })
        });
    },
    onclickGet() {
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            storage.getStorage(filePath + '/mystore').then((storage) => {
                let getPromise = storage.get('qwe', false);
                getPromise.then((value) => {
                    this.content = value;
                    console.info("jarchie---" + this.content);
                    prompt.showToast({
                        message: this.content,
                        duration: 1000
                    })
                }).catch((err) => {
                    console.info("jarchie---" + err);
                })
            }).catch((err) => {
                console.info("jarchie---获取storage失败" + err)
            })
        });
    }
}

4、实现效果

相关推荐
李游Leo12 小时前
HarmonyOS 7 + Spatial Recon Kit-Core File Kit:3DGS 重建产物的原子发布与中断恢复【鸿蒙心迹】
3d·华为·harmonyos
HwJack2013 小时前
【共创稿事节】HarmonyOS 7空间排布原则:视锥、舒适区与可达性
microsoft·华为·harmonyos
李游Leo14 小时前
HarmonyOS 7 Core Vision Kit + Image Kit:超分批处理的 PixelMap 预算、失败降级与结果原子替换【鸿蒙心迹】
harmonyos
李游Leo14 小时前
HarmonyOS 7 AbilityAccessCtrl + ArkUI:权限弹窗重入治理与提审证据链【鸿蒙心迹】
华为·harmonyos
李游Leo15 小时前
HarmonyOS 7 Spatial Recon Kit + Preferences:重建会话中断恢复与脏任务回收【鸿蒙心迹】
华为·harmonyos
李游Leo16 小时前
《HarmonyOS 7 ArkGraphics 3D 空间设计开发实战》03:glTF模型加载、实例复用与资源生命周期【鸿蒙心迹】
3d·harmonyos
李游Leo17 小时前
HarmonyOS 7 + ArkTS-HUKS:精准碰一碰载荷的签名校验与重放窗口【鸿蒙心迹】
华为·harmonyos
花花_117 小时前
HarmonyOS 7 状态手记 01|页面状态别乱放
华为·harmonyos
李游Leo18 小时前
HarmonyOS 7 + Hvigor-Localization Kit:多语言资源占位符签名与回退链路预检【鸿蒙心迹】
harmonyos
李游Leo18 小时前
HarmonyOS 7 AVSession + Float Window:闪控窗播放控制权的会话移交与重复指令去重【鸿蒙心迹】
华为·harmonyos