Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
d17df5b3
D
Docs
项目概览
OpenHarmony
/
Docs
11 个月 前同步成功
通知
158
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d17df5b3
编写于
9月 15, 2022
作者:
O
openharmony_ci
提交者:
Gitee
9月 15, 2022
浏览文件
操作
浏览文件
下载
差异文件
!9458 Preferences:开发指南和API参考,补充关于context在FA模型和Stage模型下不同的获取使用方式:需同步翻译
Merge pull request !9458 from 葛亚芳/master
上级
19861e3c
a450dad5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
197 addition
and
18 deletion
+197
-18
zh-cn/application-dev/database/database-preference-guidelines.md
...pplication-dev/database/database-preference-guidelines.md
+28
-6
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+169
-12
未找到文件。
zh-cn/application-dev/database/database-preference-guidelines.md
浏览文件 @
d17df5b3
...
...
@@ -55,15 +55,15 @@
| ----------- | ----------------------- | ------------------------------------------- |
| Preferences | flush(): Promise
\<
void> | 将Preferences实例通过异步线程回写入文件中。 |
### 订阅数据变
化
### 订阅数据变
更
订阅数据变更
者类
,订阅的Key的值发生变更后,在执行flush方法后,会触发callback回调。
订阅数据变更,订阅的Key的值发生变更后,在执行flush方法后,会触发callback回调。
**表5**
首选项变化订阅接口
| 类名 | 接口名 | 描述 |
| ----------- | ------------------------------------------------------------ | -------------- |
| Preferences | on(type: 'change', callback: Callback
<
{
key
:
string
}
>
): void | 订阅数据变
化
。 |
| Preferences | on(type: 'change', callback: Callback
<
{
key
:
string
}
>
): void | 订阅数据变
更
。 |
| Preferences | off(type: 'change', callback: Callback
<
{
key
:
string
}
>
): void | 注销订阅。 |
### 删除数据文件
...
...
@@ -88,8 +88,30 @@
2.
获取Preferences实例。
读取指定文件,将数据加载到Preferences实例,用于数据操作。
FA模型示例:
```
js
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
);
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
var
context
=
featureAbility
.
getContext
()
let
promise
=
data_preferences
.
getPreferences
(
context
,
'
mystore
'
);
```
Stage模型示例:
```
ts
// 获取context
import
Ability
from
'
@ohos.application.Ability
'
var
context
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
promise
=
data_preferences
.
getPreferences
(
context
,
'
mystore
'
);
```
3.
存入数据。
...
...
@@ -134,9 +156,9 @@
preferences
.
flush
();
```
6.
订阅数据变
化
。
6.
订阅数据变
更
。
应用订阅数据变
化
需要指定observer作为回调方法。订阅的Key的值发生变更后,当执行flush方法时,observer被触发回调。
应用订阅数据变
更
需要指定observer作为回调方法。订阅的Key的值发生变更后,当执行flush方法时,observer被触发回调。
```
js
var
observer
=
function
(
key
)
{
...
...
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
d17df5b3
...
...
@@ -38,15 +38,21 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
| context |
[
Context
](
js-apis-ability-context.md
)
| 是 | 应用上下文
。 |
| context |
Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-Context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-ability-context.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
| callback | AsyncCallback
<
[Preferences](#preferences)
>
| 是 | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误码。 |
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
var
context
=
featureAbility
.
getContext
()
var
preferences
=
null
;
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
object
)
{
data_preferences
.
getPreferences
(
context
,
'
mystore
'
,
function
(
err
,
object
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. Cause:
"
+
err
);
return
;
...
...
@@ -56,6 +62,28 @@ data_preferences.getPreferences(this.context, 'mystore', function (err, object)
})
```
Stage模型示例:
```
ts
// 获取context
import
Ability
from
'
@ohos.application.Ability
'
var
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
var
preferences
=
null
;
data_preferences
.
getPreferences
(
context
,
'
mystore
'
,
function
(
err
,
object
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. Cause:
"
+
err
);
return
;
}
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
```
## data_preferences.getPreferences
...
...
@@ -69,7 +97,7 @@ getPreferences(context: Context, name: string): Promise<Preferences>
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------- | ---- | ----------------------- |
| context |
[
Context
](
js-apis-ability-context.md
)
| 是 | 应用上下文
。 |
| context |
Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-Context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-ability-context.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
**返回值:**
...
...
@@ -80,9 +108,15 @@ getPreferences(context: Context, name: string): Promise<Preferences>
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
var
context
=
featureAbility
.
getContext
()
var
preferences
=
null
;
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
let
promise
=
data_preferences
.
getPreferences
(
context
,
'
mystore
'
)
promise
.
then
((
object
)
=>
{
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
...
...
@@ -91,6 +125,27 @@ promise.then((object) => {
})
```
Stage模型示例:
```
ts
// 获取context
import
Ability
from
'
@ohos.application.Ability
'
var
context
;
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
var
preferences
=
null
;
let
promise
=
data_preferences
.
getPreferences
(
context
,
'
mystore
'
)
promise
.
then
((
object
)
=>
{
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to get preferences. Cause:
"
+
err
);
})
```
## data_preferences.deletePreferences
...
...
@@ -108,14 +163,20 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context |
[
Context
](
js-apis-ability-context.md
)
| 是 | 应用上下文
。 |
| context |
Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-Context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-ability-context.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误码。 |
**示例:**
FA模型示例:
```
js
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
var
context
=
featureAbility
.
getContext
()
data_preferences
.
deletePreferences
(
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to delete preferences. Cause:
"
+
err
);
return
...
...
@@ -124,6 +185,26 @@ data_preferences.deletePreferences(this.context, 'mystore', function (err) {
})
```
Stage模型示例:
```
ts
// 获取context
import
Ability
from
'
@ohos.application.Ability
'
var
context
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
data_preferences
.
deletePreferences
(
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to delete preferences. Cause:
"
+
err
);
return
}
console
.
info
(
"
Succeeded in deleting preferences.
"
);
})
```
## data_preferences.deletePreferences
...
...
@@ -141,7 +222,7 @@ deletePreferences(context: Context, name: string): Promise<void>
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------- | ---- | ----------------------- |
| context |
[
Context
](
js-apis-ability-context.md
)
| 是 | 应用上下文
。 |
| context |
Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-Context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-ability-context.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
**返回值:**
...
...
@@ -152,8 +233,14 @@ deletePreferences(context: Context, name: string): Promise<void>
**示例:**
FA模型示例:
```
js
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
)
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
var
context
=
featureAbility
.
getContext
()
let
promise
=
data_preferences
.
deletePreferences
(
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting preferences.
"
);
}).
catch
((
err
)
=>
{
...
...
@@ -161,6 +248,25 @@ promise.then(() => {
})
```
Stage模型示例:
```
ts
// 获取context
import
Ability
from
'
@ohos.application.Ability
'
var
context
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
promise
=
data_preferences
.
deletePreferences
(
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to delete preferences. Cause:
"
+
err
);
})
```
## data_preferences.removePreferencesFromCache
...
...
@@ -176,14 +282,20 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
| context |
[
Context
](
js-apis-ability-context.md
)
| 是 | 应用上下文
。 |
| context |
Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-Context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-ability-context.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。当移除成功,err为undefined,否则为错误码。 |
**示例:**
FA模型示例:
```
js
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
var
context
=
featureAbility
.
getContext
()
data_preferences
.
removePreferencesFromCache
(
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to remove preferences. Cause:
"
+
err
);
return
;
...
...
@@ -192,6 +304,26 @@ data_preferences.removePreferencesFromCache(this.context, 'mystore', function (e
})
```
Stage模型示例:
```
ts
// 获取context
import
Ability
from
'
@ohos.application.Ability
'
var
context
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
data_preferences
.
removePreferencesFromCache
(
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to remove preferences. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in removing preferences.
"
);
})
```
## data_preferences.removePreferencesFromCache
...
...
@@ -207,7 +339,7 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
| 参数名 | 类型 | 必填 | 说明 |
| ------- | ------------------------------------- | ---- | ----------------------- |
| context |
[
Context
](
js-apis-ability-context.md
)
| 是 | 应用上下文
。 |
| context |
Context | 是 | 应用上下文。
<br>
FA模型的应用Context定义见
[
Context
](
js-apis-Context.md
)
。
<br>
Stage模型的应用Context定义见
[
Context
](
js-apis-ability-context.md
)
。 |
| name | string | 是 | Preferences实例的名称。 |
**返回值:**
...
...
@@ -218,8 +350,14 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
**示例:**
FA模型示例:
```
js
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
)
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
var
context
=
featureAbility
.
getContext
()
let
promise
=
data_preferences
.
removePreferencesFromCache
(
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
}).
catch
((
err
)
=>
{
...
...
@@ -227,6 +365,25 @@ promise.then(() => {
})
```
Stage模型示例:
```
ts
// 获取context
import
Ability
from
'
@ohos.application.Ability
'
var
context
class
MainAbility
extends
Ability
{
onWindowStageCreate
(
windowStage
){
context
=
this
.
context
}
}
let
promise
=
data_preferences
.
removePreferencesFromCache
(
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to remove preferences. Cause:
"
+
err
);
})
```
## Preferences
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录