js-apis-system-storage.md 7.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# 数据存储

G
ge-yafang 已提交
3
>  **说明:**
Z
zengyawen 已提交
4
>
5
>  - 从API Version 6开始,该模块不再维护,可以使用模块[`@ohos.data.storage`](js-apis-data-storage.md)。
Z
zengyawen 已提交
6
>
7
>  - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
Z
zengyawen 已提交
8 9 10

## 导入模块

L
li_juntao 已提交
11
```js
Z
zengyawen 已提交
12 13 14 15 16
import storage from '@system.storage';
```

## storage.get

17
get(options: GetStorageOptions): void
Z
zengyawen 已提交
18

L
li_juntao 已提交
19
通过索引读取缓存中存储的值。
Z
zengyawen 已提交
20

G
ge-yafang 已提交
21
**系统能力:**  SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
22 23 24

**参数:**

25 26 27
| 参数名  | 类型                    | 必填 | 说明       |
| ------- | -------------------- | ---- | ---------- |
| options | [GetStorageOptions](#getstorageoptions) | 是   | 接口配置信息。 |
Z
zengyawen 已提交
28 29 30

**示例:**

L
li_juntao 已提交
31
```js
Z
zengyawen 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
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

52
set(options: SetStorageOptions): void
Z
zengyawen 已提交
53

L
li_juntao 已提交
54
修改缓存中索引对应的值。
Z
zengyawen 已提交
55

G
ge-yafang 已提交
56
**系统能力:**  SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
57 58 59

**参数:**

60 61 62
| 参数名  | 类型                   | 必填 | 说明       |
| ------- | ------------------- | ---- | ---------- |
| options | [SetStorageOptions](#setstorageoptions) | 是   | 接口配置信息。 |
Z
zengyawen 已提交
63 64 65

**示例:**

L
li_juntao 已提交
66
```js
Z
zengyawen 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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

85
clear(options?: ClearStorageOptions): void
Z
zengyawen 已提交
86

L
li_juntao 已提交
87
清空缓存中存储的键值对。
Z
zengyawen 已提交
88

G
ge-yafang 已提交
89
**系统能力:**  SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
90 91 92

**参数:**

93 94 95
| 参数名  | 类型                                        | 必填 | 说明           |
| ------- | ------------------------------------------- | ---- | -------------- |
| options | [ClearStorageOptions](#clearstorageoptions) | 否   | 接口配置信息。 |
Z
zengyawen 已提交
96 97 98

**示例:**

L
li_juntao 已提交
99
```js
Z
zengyawen 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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

116
delete(options: DeleteStorageOptions): void
Z
zengyawen 已提交
117

L
li_juntao 已提交
118
删除缓存中索引对应的键值对。
Z
zengyawen 已提交
119

G
ge-yafang 已提交
120
**系统能力:**  SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
121 122 123

**参数:**

124 125 126
| 参数名  | 类型                                          | 必填 | 说明           |
| ------- | --------------------------------------------- | ---- | -------------- |
| options | [DeleteStorageOptions](#deletestorageoptions) | 是   | 接口配置信息。 |
Z
zengyawen 已提交
127 128 129

**示例:**

L
li_juntao 已提交
130
```js
Z
zengyawen 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143
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);            
      },        
    });    
  }
}
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
```

## GetStorageOptions

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

| 名称     | 类型          | 必填 | 说明                     |
| -------- | ---------------- | ---- | ------------------- |
| key      | string                               | 是   | 内容索引。                                             |
| default  | string                               | 否   | key不存在则返回的默认值。                              |
| success  | (data: any) => void                  | 否   | 接口调用成功的回调函数,data为返回key对应的value。     |
| fail     | (data: string, code: number) => void | 否   | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
| complete | () => void                           | 否   | 接口调用结束的回调函数。                               |


## SetStorageOptions

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

| 名称     | 类型                | 必填 | 说明                   |
| -------- | ------------------- | ---- | -------------------- |
| key      | string                               | 是   | 要修改的存储值的索引。                                 |
| value    | string                               | 是   | 新值。长度需小于128字节。                              |
| success  | () => void                           | 否   | 接口调用成功的回调函数。                               |
| fail     | (data: string, code: number) => void | 否   | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
| complete | () => void                           | 否   | 接口调用结束的回调函数。                               |


## ClearStorageOptions

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

| 名称     | 类型             | 必填 | 说明                         |
| -------- | --------------------- | ---- | -------------------- |
| success  | () => void                           | 否   | 接口调用成功的回调函数。                               |
| fail     | (data: string, code: number) => void | 否   | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
| complete | () => void                           | 否   | 接口调用结束的回调函数。                               |


## DeleteStorageOptions

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

| 名称     | 类型                 | 必填 | 说明                  |
| -------- | -------------------- | ---- | ------------------ |
| key      | string                               | 是   | 内容索引。                                             |
| success  | () => void                           | 否   | 接口调用成功的回调函数。                               |
| fail     | (data: string, code: number) => void | 否   | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
| complete | () => void                           | 否   | 接口调用结束的回调函数。                               |