openharmony 鸿蒙 js-apis-system-storage

2025-06-12 浏览 (1)

@system.storage (Data Storage)

NOTE

  • The APIs of this module are no longer maintained since API version 6, and you are advised to use @ohos.data.storage. From API version 9, you are advised to use @ohos.data.preferences.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs of this module can be used only in the FA model.

Modules to Import

import storage from '@system.storage';

storage.get

get(options: GetStorageOptions): void

Reads the value stored in the cache based on the specified key.

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

Parameters

NameTypeMandatoryDescription
optionsGetStorageOptionsYesAPI configuration.

Example

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(options: SetStorageOptions): void

Sets the value in the cache based on the specified key.

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

Parameters

NameTypeMandatoryDescription
optionsSetStorageOptionsYesAPI configuration.

Example

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(options?: ClearStorageOptions): void

Clears the key-value pairs from the cache.

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

Parameters

NameTypeMandatoryDescription
optionsClearStorageOptionsNoAPI configuration.

Example

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(options: DeleteStorageOptions): void

Deletes the key-value pair based on the specified key.

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

Parameters

NameTypeMandatoryDescription
optionsDeleteStorageOptionsYesAPI configuration.

Example

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);            
      },        
    });    
  }
}

GetStorageOptions

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

NameTypeMandatoryDescription
keystringYesKey of the target data.
defaultstringNoDefault value returned when the specified key does not exist.
success(data: any) => voidNoCalled to return the result when storage.get() is called successfully. data is the value indexed by the specified key.
fail(data: string, code: number) => voidNoCalled to return the result when storage.get() fails to be called. data is the error information, and code indicates the error code.
complete() => voidNoCalled when storage.get() is complete.

SetStorageOptions

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

NameTypeMandatoryDescription
keystringYesKey of the data to set.
valuestringYesNew value to set. The length must be less than 128 bytes.
success() => voidNoCalled when storage.set() is called successfully.
fail(data: string, code: number) => voidNoCalled to return the result when storage.set() fails to be called. data is the error information, and code indicates the error code.
complete() => voidNoCalled when storage.set() is complete.

ClearStorageOptions

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

NameTypeMandatoryDescription
success() => voidNoCalled when storage.clear() is called successfully.
fail(data: string, code: number) => voidNoCalled to return the result when storage.clear() fails to be called. data is the error information, and code indicates the error code.
complete() => voidNoCalled when storage.clear() is complete.

DeleteStorageOptions

System capability: SystemCapability.DistributedDataManager.Preferences.Core.Lite

NameTypeMandatoryDescription
keystringYesKey of the data to delete.
success() => voidNoCalled when storage.delete() is called successfully.
fail(data: string, code: number) => voidNoCalled to return the result when storage.delete() fails to be called. data is the error information, and code indicates the error code.
complete() => voidNoCalled when storage.delete() is complete.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkData (ArkData Management)

harmony 鸿蒙Data

harmony 鸿蒙OH_Cursor

harmony 鸿蒙OH_Predicates

harmony 鸿蒙OH_Rdb_Config

harmony 鸿蒙OH_Rdb_Store

harmony 鸿蒙OH_VBucket

harmony 鸿蒙OH_VObject

harmony 鸿蒙Preferences

harmony 鸿蒙_r_d_b

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/isndE5