鸿蒙语言基础类库:【@system.storage (数据存储)】

数据存储

说明:

  • 从API Version 6开始,该模块不再维护,可以使用模块[@ohos.data.storage]。在API Version 9后,推荐使用新模块[@ohos.data.preferences]。
  • 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

复制代码
import storage from '@system.storage';

storage.get

get(Object): void

通过索引读取缓存中存储的值。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 内容索引。
default string key不存在则返回的默认值。
success Function 接口调用成功的回调函数,data为返回key对应的value。
fail Function 接口调用失败的回调函数,data为错误信息,code为错误码。
complete Function 接口调用结束的回调函数。

示例:

复制代码
export default {    
  storageGet() {        
    storage.get({            
      key: 'storage_key',            
      success: function(data) {                
        console.log('call storage.get success: ' + data);            
      },            
      fail: function(data, code) {                
        console.log('call storage.get fail, code: ' + code + ', data: ' + data);            
      },            
      complete: function() {                
        console.log('call complete');            
      },
    });    
  }
}

storage.set

set(Object): void

修改缓存中索引对应的值。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 要修改的存储值的索引。
value string 新值。长度需小于128字节。
success Function 接口调用成功的回调函数。
fail Function 接口调用失败的回调函数,data为错误信息,code为错误码。
complete Function 接口调用结束的回调函数。

示例:

复制代码
export default {    
  storageSet() {        
    storage.set({            
      key: 'storage_key',            
      value: 'storage value',            
      success: function() {                
        console.log('call storage.set success.');            
      },            
      fail: function(data, code) {                
        console.log('call storage.set fail, code: ' + code + ', data: ' + data);            
      },        
    });    
  }
}

storage.clear

clear(Object): void

清空缓存中存储的键值对。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数。
fail Function 接口调用失败的回调函数,data为错误信息,code为错误码。
complete Function 接口调用结束的回调函数。

示例:

复制代码
export default {    
  storageClear() {        
    storage.clear({            
      success: function() {                
        console.log('call storage.clear success.');            
      },            
      fail: function(data, code) {                
        console.log('call storage.clear fail, code: ' + code + ', data: ' + data);            
      },        
    });    
  }
}

storage.delete

delete(Object): void

删除缓存中索引对应的键值对。

系统能力: SystemCapability.DistributedDataManager.Preferences.Core

参数:

参数名 类型 必填 说明
key string 内容索引。
success Function 接口调用成功的回调函数。
fail Function 接口调用失败的回调函数,data为错误信息,code为错误码。
complete Function 接口调用结束的回调函数。

示例:

复制代码
export default {    
  storageDelete() {        
    storage.delete({            
      key: 'Storage1',            
      success: function() {                
        console.log('call storage.delete success.');            
      },            
      fail: function(data, code) {                
        console.log('call storage.delete fail, code: ' + code + ', data: ' + data);            
      },        
    });    
  }
}
相关推荐
23zhgjx-zgx2 小时前
华为ensp:配置Local区域的安全策略及ASPF配置
网络·华为
王码码20354 小时前
Flutter 三方库 sparky 的鸿蒙化适配指南 - 实现极简 2D 游戏引擎功能、支持高效精灵图渲染与跨端游戏逻辑
flutter·harmonyos·鸿蒙·openharmony
人工智能知识库4 小时前
华为人工智能HCIP-AI Solution Architect H13-323题库(26年最新,带解析知识点)
华为·hcip·题库·hcip-ai·h13-323
Industio_触觉智能5 小时前
【转载】2026数字中国信创赛道【开放原子电鸿专项赛】开启报名,触觉智能电鸿硬件
信创·openharmony·rk3568·国产化·开源鸿蒙·电鸿·电力鸿蒙
特立独行的猫a8 小时前
OpenHarmony海思WS63星闪平台:Opus 音频编解码库介绍与海思 WS63 平台移植
驱动开发·移植·openharmony·星闪·opus·ws63
●VON9 小时前
Flutter组件通信详解:父子组件交互的最佳实践
javascript·flutter·华为·交互·harmonyos·von
Alter12309 小时前
华为吴辉:AI正在重构生产系统,“大增量时代”已经到来
人工智能·华为·重构
特立独行的猫a9 小时前
OpenHarmony海思WS63星闪平台:EasyLogger 移植到海思 WS63 平台完整指南
驱动开发·openharmony·ws63·hi3863·easylogger
国医中兴10 小时前
ClickHouse查询优化:从原理到实战
flutter·harmonyos·鸿蒙·openharmony
枫叶丹410 小时前
【HarmonyOS 6.0】OAID服务正式支持TV设备
开发语言·华为·harmonyos