未验证 提交 dac57a71 编写于 作者: O openharmony_ci 提交者: Gitee

!8197 3.1Release分支:data-storage 和window问题修改:window需同步翻译

Merge pull request !8197 from 葛亚芳/OpenHarmony-3.1-Release
......@@ -76,8 +76,8 @@ Use the following APIs to delete a **Storage** instance or data file.
1. Import **@ohos.data.storage** and related modules to the development environment.
```js
import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility' // Used to obtain the file storage path.
import dataStorage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility'; // Used to obtain the file storage path.
```
2. Create a **Storage** instance.
......@@ -85,9 +85,14 @@ Use the following APIs to delete a **Storage** instance or data file.
Read the specified file and load its data to the **Storage** instance for data operations.
```js
var context = featureAbility.getContext()
var path = await context.getFilesDir()
let promise = dataStorage.getStorage(path + '/mystore')
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
let promise = dataStorage.getStorage(path + '/mystore');
```
......@@ -97,14 +102,14 @@ Use the following APIs to delete a **Storage** instance or data file.
```js
promise.then((storage) => {
let getPromise = storage.put('startup', 'auto') // Save data to the Storage instance.
let getPromise = storage.put('startup', 'auto'); // Save data to the Storage instance.
getPromise.then(() => {
console.info("Put the value of startup successfully.")
console.info("Succeeded in putting the value of startup.");
}).catch((err) => {
console.info("Put the value of startup failed with err: " + err)
console.info("Failed to put the value of startup with err: " + err);
})
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
```
......@@ -115,14 +120,14 @@ Use the following APIs to delete a **Storage** instance or data file.
```js
promise.then((storage) => {
let getPromise = storage.get('startup', 'default')
let getPromise = storage.get('startup', 'default');
getPromise.then((value) => {
console.info("The value of startup is " + value)
console.info("The value of startup is " + value);
}).catch((err) => {
console.info("Get the value of startup failed with err: " + err)
console.info("Failed to get the value of startup with err: " + err);
})
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
```
......@@ -142,15 +147,15 @@ Use the following APIs to delete a **Storage** instance or data file.
```js
promise.then((storage) => {
var observer = function (key) {
console.info("The key of " + key + " changed.")
console.info("The key of " + key + " changed.");
}
storage.on('change', observer)
storage.putSync('startup', 'auto') // Modify data in the Storage instance.
storage.flushSync() // Trigger the StorageObserver callback.
storage.on('change', observer);
storage.putSync('startup', 'auto'); // Modify data in the Storage instance.
storage.flushSync(); // Trigger the StorageObserver callback.
storage.off(...change..., observer) // Unsubscribe from the data changes.
storage.off('change', observer); // Unsubscribe from the data changes.
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
```
......@@ -160,11 +165,11 @@ Use the following APIs to delete a **Storage** instance or data file.
Use the **deleteStorage** method to delete the **Storage** singleton of the specified file from the memory, and delete the specified file, its backup file, and damaged files. After the specified files are deleted, the application cannot use that instance to perform any data operation. Otherwise, data inconsistency will occur. The deleted data and files cannot be restored.
```js
let promise = dataStorage.deleteStorage(path + '/mystore')
let promise = dataStorage.deleteStorage(path + '/mystore');
promise.then(() => {
console.info("Deleted successfully.")
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
console.info("Deleted failed with err: " + err)
console.info("Failed to deleted the storage with err: " + err);
})
```
......@@ -75,20 +75,22 @@
1. 准备工作,导入@ohos.data.storage以及相关的模块到开发环境。
```js
import dataStorage from '@ohos.data.storage'
import featureAbility from '@ohos.ability.featureAbility' // 用于获取文件存储路径
import dataStorage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility'; // 用于获取文件存储路径
```
2. 获取Storage实例。
读取指定文件,将数据加载到Storage实例,用于数据操作。
```js
var context = featureAbility.getContext()
context.getFilesDir().then(() => {
var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
path = filePath;
console.info("======================>getFilesDirPromsie====================>");
});
let promise = dataStorage.getStorage(path + '/mystore')
let promise = dataStorage.getStorage(path + '/mystore');
```
3. 存入数据。
......@@ -97,14 +99,14 @@
```js
promise.then((storage) => {
let getPromise = storage.put('startup', 'auto') // 保存数据到缓存的storage示例中。
let getPromise = storage.put('startup', 'auto'); // 保存数据到缓存的storage示例中。
getPromise.then(() => {
console.info("Put the value of startup successfully.")
console.info("Succeeded in putting the value of startup.");
}).catch((err) => {
console.info("Put the value of startup failed with err: " + err)
console.info("Failed to put the value of startup failed with err: " + err);
})
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
```
......@@ -116,12 +118,14 @@
promise.then((storage) => {
let getPromise = storage.get('startup', 'default')
getPromise.then((value) => {
console.info("The value of startup is " + value)
console.info("The value of startup is " + value);
}).catch((err) => {
console.info("Get the value of startup failed with err: " + err)
console.info("Failed to get the value of startup with err: " + err);
})
}).catch((err) => {
console.info("Get the storage failed")})
console.info("Failed to get the storage.")
})
```
5. 数据持久化。
......@@ -139,15 +143,15 @@
```js
promise.then((storage) => {
var observer = function (key) {
console.info("The key of " + key + " changed.")
console.info("The key of " + key + " changed.");
}
storage.on('change', observer)
storage.putSync('startup', 'auto') // 修改storage存储数据
storage.flushSync() // 触发订阅者回调方法
storage.putSync('startup', 'auto'); // 修改storage存储数据
storage.flushSync(); // 触发订阅者回调方法
storage.off('change', observer) // 注销数据变化订阅
storage.off('change', observer); // 注销数据变化订阅
}).catch((err) => {
console.info("Get the storage failed")
console.info("Failed to get the storage.");
})
```
......@@ -156,11 +160,13 @@
使用deleteStorage方法从内存中移除指定文件对应的Storage单实例,并删除指定文件及其备份文件、损坏文件。删除指定文件时,应用不允许再使用该实例进行数据操作,否则会出现数据一致性问题。删除后,数据及文件将不可恢复。
```js
let promise = dataStorage.deleteStorage(path + '/mystore')
let promise = dataStorage.deleteStorage(path + '/mystore');
promise.then(() => {
console.info("Deleted successfully.")
console.info("Succeeded in deleting the storage.");
}).catch((err) => {
console.info("Deleted failed with err: " + err)})
console.info("Failed to delete the storage with err: " + err);
})
```
## 相关实例
针对轻量级数据存储开发,有以下相关实例可供参考:
......
......@@ -1124,13 +1124,14 @@ setSystemBarEnable(names: Array<'status' | 'navigation'>, callback: AsyncCallbac
**示例:**
```js
var names = ["status", "navigation"];
// 此处以不显示导航栏、状态栏为例
var names = [];
windowClass.setSystemBarEnable(names, (err, data) => {
if (err.code) {
console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
console.error('Failed to set the system bar to be invisible. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the system bar to be visible. Data: ' + JSON.stringify(data));
console.info('Succeeded in setting the system bar to be invisible. Data: ' + JSON.stringify(data));
});
```
......@@ -1157,12 +1158,13 @@ setSystemBarEnable(names: Array<'status' | 'navigation'>): Promise&lt;void&gt;
**示例:**
```js
var names = ["status", "navigation"];
// 此处以不显示导航栏、状态栏为例
var names = [];
let promise = windowClass.setSystemBarEnable(names);
promise.then((data)=> {
console.info('Succeeded in setting the system bar to be visible. Data: ' + JSON.stringify(data));
console.info('Succeeded in setting the system bar to be invisible. Data: ' + JSON.stringify(data));
}).catch((err)=>{
console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
console.error('Failed to set the system bar to be invisible. Cause:' + JSON.stringify(err));
});
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册