鸿蒙语言基础类库:【@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);            
      },        
    });    
  }
}
相关推荐
前端御书房1 小时前
HarmonyOS 组件
pytorch·深度学习·harmonyos
亦世凡华、2 小时前
HarmonyOS NEXT:实现电影列表功能展示界面
经验分享·华为·harmonyos·harmonyos next
jmoych3 小时前
华为LTC流程架构分享
华为·架构
2101_824775823 小时前
华为设备所有查看命令以及其对应作用
网络·网络协议·网络安全·华为
蒙娜丽宁3 小时前
华为仓颉语言入门(7):深入理解 do-while 循环及其应用
java·数据库·华为·仓颉
Android技术栈5 小时前
鸿蒙开发(NEXT/API 12)【穿戴设备信息查询】手机侧应用开发
嵌入式硬件·硬件架构·移动开发·harmonyos·鸿蒙·鸿蒙系统
helloxmg12 小时前
鸿蒙harmonyos next flutter混合开发之ohos工程引用 har 文件
flutter·华为·harmonyos
看山还是山,看水还是。16 小时前
华为常见命令手册
运维·服务器·网络·笔记·华为
zhongcx19 小时前
鸿蒙应用示例:系统退出应用的最佳实践
harmonyos
zhongcx19 小时前
鸿蒙应用示例:利用attributeModifier解决样式抽离与组件状态管理
harmonyos