未验证 提交 4d484dd5 编写于 作者: 葛亚芳 提交者: Gitee

update zh-cn/application-dev/reference/apis/js-apis-data-storage.md.

Signed-off-by: N@ge-yafang <geyafang@huawei.com>
Signed-off-by: N葛亚芳 <geyafang@huawei.com>
上级 2db79965
...@@ -18,10 +18,10 @@ import data_storage from '@ohos.data.storage'; ...@@ -18,10 +18,10 @@ import data_storage from '@ohos.data.storage';
**系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core
| 名称 | 参数类型 | 可读 | 可写 | 说明 | | 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- | | ---------------- | -------- | ---- | ---- | ------------------------------------- |
| MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,需小于80字节。 | | MAX_KEY_LENGTH | string | 是 | 否 | key的最大长度限制,需小于80字节。 |
| MAX_VALUE_LENGTH | string | 是 | 否 | value的最大长度限制,需小于8192字节。 | | MAX_VALUE_LENGTH | string | 是 | 否 | value的最大长度限制,需小于8192字节。 |
## data_storage.getStorageSync ## data_storage.getStorageSync
...@@ -33,25 +33,33 @@ getStorageSync(path: string): Storage ...@@ -33,25 +33,33 @@ getStorageSync(path: string): Storage
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| [Storage](#storage) | 获取到要操作的Storage实例,用于进行数据存储操作。 | | ------------------- | ------------------------------------------------- |
| [Storage](#storage) | 获取到要操作的Storage实例,用于进行数据存储操作。 |
**示例:** **示例:**
```js
import data_storage from '@ohos.data.storage' ```js
import featureAbility from '@ohos.ability.featureAbility';
let path = '/data/storage/el2/database'
let storage = data_storage.getStorageSync(path + '/mystore') var path;
storage.putSync('startup', 'auto') var context = featureAbility.getContext();
storage.flushSync() context.getFilesDir().then((filePath) => {
path = filePath;
``` console.info("======================>getFilesDirPromsie====================>");
});
let storage = data_storage.getStorageSync(path + '/mystore');
storage.putSync('startup', 'auto');
storage.flushSync();
```
## data_storage.getStorage ## data_storage.getStorage
...@@ -63,25 +71,33 @@ getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void ...@@ -63,25 +71,33 @@ getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | -------- | ---------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback&lt;[Storage](#storage)&gt; | 是 | 回调函数。 | | path | string | 是 | 应用程序内部数据存储路径。 |
| callback | AsyncCallback&lt;[Storage](#storage)&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
import data_storage from '@ohos.data.storage' ```js
import featureAbility from '@ohos.ability.featureAbility';
let path = '/data/storage/el2/database'
data_storage.getStorage(path + '/mystore', function (err, storage) { var path;
if (err) { var context = featureAbility.getContext();
console.info("Get the storage failed, path: " + path + '/mystore') context.getFilesDir().then((filePath) => {
return; path = filePath;
} console.info("======================>getFilesDirPromsie====================>");
storage.putSync('startup', 'auto') });
storage.flushSync()
}) data_storage.getStorage(path + '/mystore', function (err, storage) {
``` if (err) {
console.info("Failed to get the storage. path: " + path + '/mystore');
return;
}
storage.putSync('startup', 'auto');
storage.flushSync();
})
```
## data_storage.getStorage ## data_storage.getStorage
...@@ -93,29 +109,37 @@ getStorage(path: string): Promise&lt;Storage&gt; ...@@ -93,29 +109,37 @@ getStorage(path: string): Promise&lt;Storage&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;[Storage](#storage)&gt; | Promise实例,用于异步获取结果。 | | ---------------------------------- | ------------------------------- |
| Promise&lt;[Storage](#storage)&gt; | Promise实例,用于异步获取结果。 |
**示例:** **示例:**
```js
import data_storage from '@ohos.data.storage' ```js
import featureAbility from '@ohos.ability.featureAbility';
let path = '/data/storage/el2/database'
var path;
let getPromise = data_storage.getStorage(path + '/mystore') var context = featureAbility.getContext();
getPromise.then((storage) => { context.getFilesDir().then((filePath) => {
storage.putSync('startup', 'auto') path = filePath;
storage.flushSync() console.info("======================>getFilesDirPromsie====================>");
}).catch((err) => { });
console.info("Get the storage failed, path: " + path + '/mystore')
}) let getPromise = data_storage.getStorage(path + '/mystore');
``` getPromise.then((storage) => {
storage.putSync('startup', 'auto');
storage.flushSync();
}).catch((err) => {
console.info("Failed to get the storage. path: " + path + '/mystore');
})
```
## data_storage.deleteStorageSync ## data_storage.deleteStorageSync
...@@ -127,15 +151,25 @@ deleteStorageSync(path: string): void ...@@ -127,15 +151,25 @@ deleteStorageSync(path: string): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
data_storage.deleteStorageSync(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
```
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
data_storage.deleteStorageSync(path + '/mystore');
```
## data_storage.deleteStorage ## data_storage.deleteStorage
...@@ -147,22 +181,32 @@ deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -147,22 +181,32 @@ deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | -------- | ------------------------- | ---- | -------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | path | string | 是 | 应用程序内部数据存储路径。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
data_storage.deleteStorage(path + '/mystore', function (err) { import featureAbility from '@ohos.ability.featureAbility';
if (err) {
console.info("Deleted failed with err: " + err) var path;
return var context = featureAbility.getContext();
} context.getFilesDir().then((filePath) => {
console.info("Deleted successfully.") path = filePath;
}) console.info("======================>getFilesDirPromsie====================>");
``` });
data_storage.deleteStorage(path + '/mystore', function (err) {
if (err) {
console.info("Failed to delete the storage with err: " + err);
return;
}
console.info("Succeeded in deleting the storage.");
})
```
## data_storage.deleteStorage ## data_storage.deleteStorage
...@@ -174,25 +218,36 @@ deleteStorage(path: string): Promise&lt;void&gt; ...@@ -174,25 +218,36 @@ deleteStorage(path: string): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 | | ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
let promisedelSt = data_storage.deleteStorage(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
promisedelSt.then(() => {
console.info("Deleted successfully.") var path;
}).catch((err) => { var context = featureAbility.getContext();
console.info("Deleted failed with err: " + err) context.getFilesDir().then((filePath) => {
}) path = filePath;
``` console.info("======================>getFilesDirPromsie====================>");
});
let promisedelSt = data_storage.deleteStorage(path + '/mystore');
promisedelSt.then(() => {
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
console.info("Failed to delete the storage with err: " + err);
})
```
## data_storage.removeStorageFromCacheSync ## data_storage.removeStorageFromCacheSync
...@@ -204,15 +259,24 @@ removeStorageFromCacheSync(path: string): void ...@@ -204,15 +259,24 @@ removeStorageFromCacheSync(path: string): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 | | path | string | 是 | 应用程序内部数据存储路径。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
data_storage.removeStorageFromCacheSync(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
```
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
data_storage.removeStorageFromCacheSync(path + '/mystore');
```
## data_storage.removeStorageFromCache ## data_storage.removeStorageFromCache
...@@ -224,22 +288,32 @@ removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -224,22 +288,32 @@ removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | -------- | ------------------------- | ---- | -------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | path | string | 是 | 应用程序内部数据存储路径。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
data_storage.removeStorageFromCache(path + '/mystore', function (err) { import featureAbility from '@ohos.ability.featureAbility';
if (err) {
console.info("Removed storage from cache failed with err: " + err) var path;
return var context = featureAbility.getContext();
} context.getFilesDir().then((filePath) => {
console.info("Removed storage from cache successfully.") path = filePath;
}) console.info("======================>getFilesDirPromsie====================>");
``` });
data_storage.removeStorageFromCache(path + '/mystore', function (err) {
if (err) {
console.info("Failed to remove storage from cache with err: " + err);
return;
}
console.info("Succeeded in removing storage from cache.");
})
```
## data_storage.removeStorageFromCache ## data_storage.removeStorageFromCache
...@@ -251,25 +325,36 @@ removeStorageFromCache(path: string): Promise&lt;void&gt; ...@@ -251,25 +325,36 @@ removeStorageFromCache(path: string): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| path | string | 是 | 应用程序内部数据存储路径。 | | ------ | ------ | ---- | -------------------------- |
| path | string | 是 | 应用程序内部数据存储路径。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 | | ------------------- | ------------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。 |
**示例:** **示例:**
```js
let path = '/data/storage/el2/database' ```js
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore') import featureAbility from '@ohos.ability.featureAbility';
promiserevSt.then(() => {
console.info("Removed storage from cache successfully.") var path;
}).catch((err) => { var context = featureAbility.getContext();
console.info("Removed storage from cache failed with err: " + err) context.getFilesDir().then((filePath) => {
}) path = filePath;
``` console.info("======================>getFilesDirPromsie====================>");
});
let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => {
console.info("Succeeded in removing storage from cache.");
}).catch((err) => {
console.info("Failed to remove storage from cache with err: " + err);
})
```
## Storage ## Storage
...@@ -286,21 +371,24 @@ getSync(key: string, defValue: ValueType): ValueType ...@@ -286,21 +371,24 @@ getSync(key: string, defValue: ValueType): ValueType
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| defValue | [ValueType](#valuetype) | 是 | 给定key的存储不存在,则要返回的默认值。支持number、string、boolean。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| defValue | [ValueType](#valuetype) | 是 | 给定key的存储不存在,则要返回的默认值。支持number、string、boolean。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| ValueType | 键对应的值,如果值为null或者非默认值类型,返回默认数据。 | | --------- | -------------------------------------------------------- |
| ValueType | 键对应的值,如果值为null或者非默认值类型,返回默认数据。 |
**示例:** **示例:**
```js
let value = storage.getSync('startup', 'default') ```js
console.info("The value of startup is " + value) let value = storage.getSync('startup', 'default');
``` console.info("The value of startup is " + value);
```
### get ### get
...@@ -312,22 +400,24 @@ get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): ...@@ -312,22 +400,24 @@ get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;):
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ------------------------------ | ---- | ----------------------------------------- |
| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| callback | AsyncCallback&lt;ValueType&gt; | 是 | 回调函数。 | | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
| callback | AsyncCallback&lt;ValueType&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.get('startup', 'default', function(err, value) { ```js
if (err) { storage.get('startup', 'default', function(err, value) {
console.info("Get the value of startup failed with err: " + err) if (err) {
return console.info("Failed to get the value of startup with err: " + err);
return;
} }
console.info("The value of startup is " + value) console.info("The value of startup is " + value);
}) })
``` ```
### get ### get
...@@ -340,25 +430,27 @@ get(key: string, defValue: ValueType): Promise&lt;ValueType&gt; ...@@ -340,25 +430,27 @@ get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | ----------------------- | ---- | ----------------------------------------- |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 | | defValue | [ValueType](#valuetype) | 是 | 默认返回值。支持number、string、boolean。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;ValueType&gt; | Promise实例,用于异步获取结果。 | | ------------------------ | ------------------------------- |
| Promise&lt;ValueType&gt; | Promise实例,用于异步获取结果。 |
**示例:** **示例:**
```js
let promiseget = storage.get('startup', 'default') ```js
promiseget.then((value) => { let promiseget = storage.get('startup', 'default');
console.info("The value of startup is " + value) promiseget.then((value) => {
}).catch((err) => { console.info("The value of startup is " + value)
console.info("Get the value of startup failed with err: " + err) }).catch((err) => {
}) console.info("Failed to get the value of startup with err: " + err);
``` })
```
### putSync ### putSync
...@@ -370,15 +462,17 @@ putSync(key: string, value: ValueType): void ...@@ -370,15 +462,17 @@ putSync(key: string, value: ValueType): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要修改的存储的key,不能为空。 | | ------ | ----------------------- | ---- | ----------------------------------------- |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | | key | string | 是 | 要修改的存储的key,不能为空。 |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
**示例:** **示例:**
```js
storage.putSync('startup', 'auto') ```js
``` storage.putSync('startup', 'auto');
```
### put ### put
...@@ -390,22 +484,24 @@ put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void ...@@ -390,22 +484,24 @@ put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要修改的存储的key,不能为空。 | | -------- | ------------------------- | ---- | ----------------------------------------- |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | | key | string | 是 | 要修改的存储的key,不能为空。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.put('startup', 'auto', function (err) { ```js
if (err) { storage.put('startup', 'auto', function (err) {
console.info("Put the value of startup failed with err: " + err) if (err) {
return console.info("Failed to put the value of startup with err: " + err);
} return;
console.info("Put the value of startup successfully.") }
}) console.info("Succeeded in putting the value of startup.");
``` })
```
### put ### put
...@@ -417,25 +513,28 @@ put(key: string, value: ValueType): Promise&lt;void&gt; ...@@ -417,25 +513,28 @@ put(key: string, value: ValueType): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要修改的存储的key,不能为空。 | | ------ | ----------------------- | ---- | ----------------------------------------- |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 | | key | string | 是 | 要修改的存储的key,不能为空。 |
| value | [ValueType](#valuetype) | 是 | 存储的新值。支持number、string、boolean。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```js
let promiseput = storage.put('startup', 'auto') ```js
promiseput.then(() => { let promiseput = storage.put('startup', 'auto');
console.info("Put the value of startup successfully.") promiseput.then(() => {
}).catch((err) => { console.info("Succeeded in putting the value of startup.");
console.info("Put the value of startup failed with err: " + err) }).catch((err) => {
}) console.info("Failed to put the value of startup with err: " + err);
``` })
```
### hasSync ### hasSync
...@@ -447,22 +546,25 @@ hasSync(key: string): boolean ...@@ -447,22 +546,25 @@ hasSync(key: string): boolean
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | ------ | ------ | ---- | ------------------------------- |
| key | string | 是 | 要获取的存储key名称,不能为空。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| boolean | true&nbsp;表示存在,false表示不存在。 | | ------- | ------------------------------------- |
| boolean | true&nbsp;表示存在,false表示不存在。 |
**示例:** **示例:**
```js
let isExist = storage.hasSync('startup') ```js
if (isExist) { let isExist = storage.hasSync('startup');
console.info("The key of startup is contained.") if (isExist) {
} console.info("The key of startup is contained.");
``` }
```
### has ### has
...@@ -474,28 +576,31 @@ has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean ...@@ -474,28 +576,31 @@ has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ---------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| boolean | true表示存在,false表示不存在。 | | ------- | ------------------------------- |
| boolean | true表示存在,false表示不存在。 |
**示例:** **示例:**
```js
storage.has('startup', function (err, isExist) { ```js
if (err) { storage.has('startup', function (err, isExist) {
console.info("Check the key of startup failed with err: " + err) if (err) {
return console.info("Failed to check the key of startup with err: " + err);
} return;
if (isExist) { }
console.info("The key of startup is contained.") if (isExist) {
} console.info("The key of startup is contained.");
}) }
``` })
```
### has ### has
...@@ -507,26 +612,29 @@ has(key: string): Promise&lt;boolean&gt; ...@@ -507,26 +612,29 @@ has(key: string): Promise&lt;boolean&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | ------ | ------ | ---- | ------------------------------- |
| key | string | 是 | 要获取的存储key名称,不能为空。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;boolean&gt; | Promise实例,用于异步处理。 | | ---------------------- | --------------------------- |
| Promise&lt;boolean&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```js
let promisehas = storage.has('startup') ```js
promisehas.then((isExist) => { let promisehas = storage.has('startup')
if (isExist) { promisehas.then((isExist) => {
console.info("The key of startup is contained.") if (isExist) {
} console.info("The key of startup is contained.");
}).catch((err) => { }
console.info("Check the key of startup failed with err: " + err) }).catch((err) => {
}) console.info("Failed to check the key of startup with err: " + err);
``` })
```
### deleteSync ### deleteSync
...@@ -538,14 +646,16 @@ deleteSync(key: string): void ...@@ -538,14 +646,16 @@ deleteSync(key: string): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称。它不能为空。 | | ------ | ------ | ---- | --------------------------------- |
| key | string | 是 | 要获取的存储key名称。它不能为空。 |
**示例:** **示例:**
```js
storage.deleteSync('startup') ```js
``` storage.deleteSync('startup');
```
### delete ### delete
...@@ -557,21 +667,23 @@ delete(key: string, callback: AsyncCallback&lt;void&gt;): void ...@@ -557,21 +667,23 @@ delete(key: string, callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称,不能为空。 | | -------- | ------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | key | string | 是 | 要获取的存储key名称,不能为空。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.delete('startup', function (err) { ```js
if (err) { storage.delete('startup', function (err) {
console.info("Delete startup key failed with err: " + err) if (err) {
return console.info("Failed to delete startup key failed err: " + err);
} return;
console.info("Deleted startup key successfully.") }
}) console.info("Succeeded in deleting startup key.");
``` })
```
### delete ### delete
...@@ -583,24 +695,27 @@ delete(key: string): Promise&lt;void&gt; ...@@ -583,24 +695,27 @@ delete(key: string): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| key | string | 是 | 要获取的存储key名称。 | | ------ | ------ | ---- | --------------------- |
| key | string | 是 | 要获取的存储key名称。 |
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```js
let promisedel = storage.delete('startup') ```js
promisedel.then(() => { let promisedel = storage.delete('startup')
console.info("Deleted startup key successfully.") promisedel.then(() => {
}).catch((err) => { console.info("Succeeded in deleting startup key.");
console.info("Delete startup key failed with err: " + err) }).catch((err) => {
}) console.info("Failed to delete startup key failed err: " + err);
``` })
```
### flushSync ### flushSync
...@@ -612,9 +727,10 @@ flushSync(): void ...@@ -612,9 +727,10 @@ flushSync(): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**示例:** **示例:**
```js
storage.flushSync() ```js
``` storage.flushSync();
```
### flush ### flush
...@@ -626,20 +742,22 @@ flush(callback: AsyncCallback&lt;void&gt;): void ...@@ -626,20 +742,22 @@ flush(callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.flush(function (err) { ```js
if (err) { storage.flush(function (err) {
console.info("Flush to file failed with err: " + err) if (err) {
return console.info("Failed to flush to file with err: " + err);
} return;
console.info("Flushed to file successfully.") }
}) console.info("Succeeded in flushing to file.");
``` })
```
### flush ### flush
...@@ -651,19 +769,21 @@ flush(): Promise&lt;void&gt; ...@@ -651,19 +769,21 @@ flush(): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**返回值:** **返回值:**
| 类型 | 说明 |
| -------- | -------- | | 类型 | 说明 |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```js
let promiseflush = storage.flush() ```js
promiseflush.then(() => { let promiseflush = storage.flush();
console.info("Flushed to file successfully.") promiseflush.then(() => {
}).catch((err) => { console.info("Succeeded in flushing to file.");
console.info("Flush to file failed with err: " + err) }).catch((err) => {
}) console.info("Failed to flush to file with err: " + err);
``` })
```
### clearSync ### clearSync
...@@ -675,9 +795,10 @@ clearSync(): void ...@@ -675,9 +795,10 @@ clearSync(): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**示例:** **示例:**
```js
storage.clearSync() ```js
``` storage.clearSync();
```
### clear ### clear
...@@ -689,20 +810,22 @@ clear(callback: AsyncCallback&lt;void&gt;): void ...@@ -689,20 +810,22 @@ clear(callback: AsyncCallback&lt;void&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | 参数名 | 类型 | 必填 | 说明 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 | | -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。 |
**示例:** **示例:**
```js
storage.clear(function (err) { ```js
if (err) { storage.clear(function (err) {
console.info("Clear to file failed with err: " + err) if (err) {
return console.info("Failed to clear the storage with err: " + err);
} return;
console.info("Cleared to file successfully.") }
}) console.info("Succeeded in clearing the storage.");
``` })
```
### clear ### clear
...@@ -714,19 +837,20 @@ clear(): Promise&lt;void&gt; ...@@ -714,19 +837,20 @@ clear(): Promise&lt;void&gt;
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**返回值:** **返回值:**
| 类型 | 说明 | | 类型 | 说明 |
| -------- | -------- | | ------------------- | --------------------------- |
| Promise&lt;void&gt; | Promise实例,用于异步处理。 | | Promise&lt;void&gt; | Promise实例,用于异步处理。 |
**示例:** **示例:**
```js
let promiseclear = storage.clear() ```js
promiseclear.then(() => { let promiseclear = storage.clear();
console.info("Cleared to file successfully.") promiseclear.then(() => {
}).catch((err) => { console.info("Succeeded in clearing the storage.");
console.info("Clear to file failed with err: " + err) }).catch((err) => {
}) console.info("Failed to clear the storage with err: " + err);
``` })
```
### on('change') ### on('change')
...@@ -738,20 +862,22 @@ on(type: 'change', callback: Callback&lt;StorageObserver&gt;): void ...@@ -738,20 +862,22 @@ on(type: 'change', callback: Callback&lt;StorageObserver&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- | | 参数名 | 类型 | 说明 |
| type | string | 事件类型,固定值'change',表示数据变更。 | | -------- | --------------------------------------------------- | ---------------------------------------- |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 回调对象实例。 | | type | string | 事件类型,固定值'change',表示数据变更。 |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 回调对象实例。 |
**示例:** **示例:**
```js
var observer = function (key) { ```js
console.info("The key of " + key + " changed.") var observer = function (key) {
} console.info("The key of " + key + " changed.");
storage.on('change', observer) }
storage.putSync('startup', 'auto') storage.on('change', observer);
storage.flushSync() // observer will be called. storage.putSync('startup', 'auto');
``` storage.flushSync(); // observer will be called.
```
### off('change') ### off('change')
...@@ -763,27 +889,29 @@ off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void ...@@ -763,27 +889,29 @@ off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
**参数:** **参数:**
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- | | 参数名 | 类型 | 说明 |
| type | string | 事件类型,固定值'change',表示数据变更。 | | -------- | --------------------------------------------------- | ---------------------------------------- |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 需要取消的回调对象实例。 | | type | string | 事件类型,固定值'change',表示数据变更。 |
| callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | 需要取消的回调对象实例。 |
**示例:** **示例:**
```js
var observer = function (key) { ```js
console.info("The key of " + key + " changed.") var observer = function (key) {
} console.info("The key of " + key + " changed.");
storage.off('change', observer) }
``` storage.off('change', observer);
```
## StorageObserver ## StorageObserver
**系统能力:** 以下各项对应的系统能力均为SystemCapability.DistributedDataManager.Preferences.Core **系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
| 名称 | 参数类型 | 必填 | 说明 | | 名称 | 参数类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | ---- | -------- | ---- | ---------------- |
| key | string | 否 | 变更的数据内容。 | | key | string | 否 | 变更的数据内容。 |
## ValueType ## ValueType
...@@ -795,4 +923,4 @@ off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void ...@@ -795,4 +923,4 @@ off(type: 'change', callback: Callback&lt;StorageObserver&gt;): void
| ------- | -------------------- | | ------- | -------------------- |
| number | 表示值类型为数字。 | | number | 表示值类型为数字。 |
| string | 表示值类型为字符。 | | string | 表示值类型为字符。 |
| boolean | 表示值类型为布尔值。 | | boolean | 表示值类型为布尔值。 |
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册