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

!10508 [翻译完成】#I5R4ZF

Merge pull request !10508 from Annie_wang/PR9458
...@@ -94,9 +94,16 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -94,9 +94,16 @@ Use the following APIs to delete a **Preferences** instance or data file.
```js ```js
// Obtain the context. // Obtain the context.
import featureAbility from '@ohos.ability.featureAbility' import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext() let context = featureAbility.getContext();
let preferences = null;
let promise = data_preferences.getPreferences(context, 'mystore'); let promise = data_preferences.getPreferences(context, 'mystore');
promise.then((pref) => {
preferences = pref;
}).catch((err) => {
console.info("Failed to get the preferences.");
})
``` ```
Stage model: Stage model:
...@@ -104,14 +111,21 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -104,14 +111,21 @@ Use the following APIs to delete a **Preferences** instance or data file.
```ts ```ts
// Obtain the context. // Obtain the context.
import Ability from '@ohos.application.Ability' import Ability from '@ohos.application.Ability'
var context let context = null;
class MainAbility extends Ability{ let preferences = null;
export default class MainAbility extends Ability {
onWindowStageCreate(windowStage){ onWindowStageCreate(windowStage){
context = this.context context = this.context;
} }
} }
let promise = data_preferences.getPreferences(context, 'mystore'); let promise = data_preferences.getPreferences(context, 'mystore');
promise.then((pref) => {
preferences = pref;
}).catch((err) => {
console.info("Failed to get the preferences.");
})
``` ```
3. Write data. 3. Write data.
...@@ -119,16 +133,12 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -119,16 +133,12 @@ Use the following APIs to delete a **Preferences** instance or data file.
Use the **preferences.put()** method to write data to the **Preferences** instance. Use the **preferences.put()** method to write data to the **Preferences** instance.
```js ```js
promise.then((preferences) => {
let putPromise = preferences.put('startup', 'auto'); let putPromise = preferences.put('startup', 'auto');
putPromise.then(() => { putPromise.then(() => {
console.info("Put the value of 'startup' successfully."); console.info("Put the value of 'startup' successfully.");
}).catch((err) => { }).catch((err) => {
console.info("Failed to put the value of 'startup'. Cause: " + err); console.info("Failed to put the value of 'startup'. Cause: " + err);
}) })
}).catch((err) => {
console.info("Failed to get the preferences.");
})
``` ```
4. Read data. 4. Read data.
...@@ -136,16 +146,12 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -136,16 +146,12 @@ Use the following APIs to delete a **Preferences** instance or data file.
Use the **preferences.get()** method to read data. Use the **preferences.get()** method to read data.
```js ```js
promise.then((preferences) => {
let getPromise = preferences.get('startup', 'default'); let getPromise = preferences.get('startup', 'default');
getPromise.then((value) => { getPromise.then((value) => {
console.info("The value of 'startup' is " + value); console.info("The value of 'startup' is " + value);
}).catch((err) => { }).catch((err) => {
console.info("Failed to get the value of 'startup'. Cause: " + err); console.info("Failed to get the value of 'startup'. Cause: " + err);
}) })
}).catch((err) => {
console.info("Failed to get the preferences.")
});
``` ```
5. Store data persistently. 5. Store data persistently.
...@@ -161,11 +167,12 @@ Use the following APIs to delete a **Preferences** instance or data file. ...@@ -161,11 +167,12 @@ Use the following APIs to delete a **Preferences** instance or data file.
Specify an observer as the callback to subscribe to data changes for an application. When the value of the subscribed key is changed and saved by **flush()**, the observer callback will be invoked to return the new data. Specify an observer as the callback to subscribe to data changes for an application. When the value of the subscribed key is changed and saved by **flush()**, the observer callback will be invoked to return the new data.
```js ```js
var observer = function (key) { let observer = function (key) {
console.info("The key" + key + " changed."); console.info("The key" + key + " changed.");
} }
preferences.on('change', observer); preferences.on('change', observer);
preferences.put('startup', 'auto', function (err) { // The data is changed from 'auto' to 'manual'.
preferences.put('startup', 'manual', function (err) {
if (err) { if (err) {
console.info("Failed to put the value of 'startup'. Cause: " + err); console.info("Failed to put the value of 'startup'. Cause: " + err);
return; return;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册