Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
3957a7fb
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
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看板
提交
3957a7fb
编写于
5月 13, 2022
作者:
P
PaDaBoo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update data-Preferences file
Signed-off-by:
N
PaDaBoo
<
xuejianwu@huawei.com
>
上级
b4e18ea1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
216 addition
and
411 deletion
+216
-411
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+216
-411
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
3957a7fb
...
...
@@ -35,38 +35,20 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| context |
[
Context
](
js-apis-
C
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| context |
[
Context
](
js-apis-
ability-c
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| name | string | 是 | 应用程序内部数据存储名称。 |
| callback | AsyncCallback
<
[Preferences](#preferences)
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed
"
)
return
;
}
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
})
})
})
}
```
```
ts
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get preferences failed.
"
)
return
;
}
console
.
info
(
"
Get preferences successfully.
"
)
})
```
## data_preferences.getPreferences
...
...
@@ -80,7 +62,7 @@ getPreferences(context: Context, name: string): Promise<Preferences>
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| context |
[
Context
](
js-apis-
C
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| context |
[
Context
](
js-apis-
ability-c
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| name | string | 是 | 应用程序内部数据存储名称。 |
**返回值:**
...
...
@@ -89,32 +71,14 @@ getPreferences(context: Context, name: string): Promise<Preferences>
| Promise
<
[Preferences](#preferences)
>
| Promise实例,用于异步获取结果。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
})
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed
"
)
})
}
```
```
ts
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
console
.
info
(
"
Get preferences successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get preferences failed.
"
)
})
```
## data_preferences.deletePreferences
...
...
@@ -129,25 +93,20 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| context |
[
Context
](
js-apis-
C
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| context |
[
Context
](
js-apis-
ability-c
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| name | string | 是 | 应用程序内部数据存储名称。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Deleted failed, err:
"
+
err
)
return
}
console
.
info
(
"
Deleted successfully.
"
)
})
}
```
```
ts
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Deleted preferences failed, err:
"
+
err
)
return
}
console
.
info
(
"
Deleted preferences successfully.
"
)
})
```
## data_preferences.deletePreferences
...
...
@@ -162,7 +121,7 @@ deletePreferences(context: Context, name: string): Promise<void>
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| context |
[
Context
](
js-apis-
C
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| context |
[
Context
](
js-apis-
ability-c
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| name | string | 是 | 应用程序内部数据存储名称。 |
**返回值:**
...
...
@@ -171,19 +130,14 @@ deletePreferences(context: Context, name: string): Promise<void>
| Promise
<
void
>
| Promise实例,用于异步获取结果。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Deleted successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Deleted failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Deleted preferences successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Deleted preferences failed, err:
"
+
err
)
})
```
## data_preferences.removePreferencesFromCache
...
...
@@ -199,25 +153,20 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| context |
[
Context
](
js-apis-
C
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| context |
[
Context
](
js-apis-
ability-c
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| name | string | 是 | 应用程序内部数据存储名称。 |
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Removed preferences from cache failed, err:
"
+
err
)
return
}
console
.
info
(
"
Removed preferences from cache successfully.
"
)
})
}
```
```
ts
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Removed preferences from cache failed, err:
"
+
err
)
return
}
console
.
info
(
"
Removed preferences from cache successfully.
"
)
})
```
## data_preferences.removePreferencesFromCache
...
...
@@ -233,7 +182,7 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| context |
[
Context
](
js-apis-
C
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| context |
[
Context
](
js-apis-
ability-c
ontext.md
)
| 是 | 应用程序或功能的上下文。 |
| name | string | 是 | 应用程序内部数据存储名称。 |
**返回值:**
...
...
@@ -242,19 +191,14 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
| Promise
<
void
>
| Promise实例,用于异步获取结果。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Removed preferences from cache successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Removed preferences from cache failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Removed preferences from cache successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Removed preferences from cache failed, err:
"
+
err
)
})
```
## Preferences
...
...
@@ -278,26 +222,15 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>):
| callback | AsyncCallback
<
ValueType
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
get
(
'
startup
'
,
'
default
'
,
function
(
err
,
value
)
{
if
(
err
)
{
console
.
info
(
"
Get the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
The value of startup is
"
+
value
)
})
})
}
```
```
ts
preferences
.
get
(
'
startup
'
,
'
default
'
,
function
(
err
,
value
)
{
if
(
err
)
{
console
.
info
(
"
Get value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Get value of startup is
"
+
value
)
})
```
### get
...
...
@@ -320,24 +253,14 @@ get(key: string, defValue: ValueType): Promise<ValueType>
| Promise
<
ValueType
>
| Promise实例,用于异步获取结果。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
let
promiseGet
=
preferences
.
get
(
'
startup
'
,
'
default
'
)
promiseGet
.
then
((
value
)
=>
{
console
.
info
(
"
The value of startup is
"
+
value
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the value of startup failed, err:
"
+
err
)
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
preferences
.
get
(
'
startup
'
,
'
default
'
)
promise
.
then
((
value
)
=>
{
console
.
info
(
"
Get value of startup is
"
+
value
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get value of startup failed, err:
"
+
err
)
})
```
### put
...
...
@@ -356,26 +279,15 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
})
})
}
```
```
ts
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put value of startup successfully.
"
)
})
```
### put
...
...
@@ -398,24 +310,14 @@ put(key: string, value: ValueType): Promise<void>
| Promise
<
void
>
| Promise实例,用于异步处理。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
let
promisePut
=
preferences
.
put
(
'
startup
'
,
'
auto
'
)
promisePut
.
then
(()
=>
{
console
.
info
(
"
Put the value of startup successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
preferences
.
put
(
'
startup
'
,
'
auto
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Put value of startup successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Put value of startup failed, err:
"
+
err
)
})
```
### has
...
...
@@ -438,30 +340,19 @@ has(key: string, callback: AsyncCallback<boolean>): boolean
| boolean | true表示存在,false表示不存在。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
has
(
'
startup
'
,
function
(
err
,
isExist
)
{
if
(
err
)
{
console
.
info
(
"
Check the key of startup failed, err:
"
+
err
)
return
}
if
(
isExist
)
{
console
.
info
(
"
The key of startup is contained.
"
)
}
else
{
console
.
info
(
"
The key of startup dose not contain.
"
)
}
})
})
}
```
```
ts
preferences
.
has
(
'
startup
'
,
function
(
err
,
isExist
)
{
if
(
err
)
{
console
.
info
(
"
Check the key of startup failed, err:
"
+
err
)
return
}
if
(
isExist
)
{
console
.
info
(
"
The key of startup is contained.
"
)
}
else
{
console
.
info
(
"
The key of startup dose not contain.
"
)
}
})
```
### has
...
...
@@ -483,28 +374,18 @@ has(key: string): Promise<boolean>
| Promise
<
boolean
>
| Promise实例,用于异步处理。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
let
promiseHas
=
preferences
.
has
(
'
startup
'
)
promiseHas
.
then
((
isExist
)
=>
{
if
(
isExist
)
{
console
.
info
(
"
The key of startup is contained.
"
)
}
else
{
console
.
info
(
"
The key of startup dose not contain.
"
)
}
}).
catch
((
err
)
=>
{
console
.
info
(
"
Check the key of startup failed, err:
"
+
err
)
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
preferences
.
has
(
'
startup
'
)
promise
.
then
((
isExist
)
=>
{
if
(
isExist
)
{
console
.
info
(
"
The key of startup is contained.
"
)
}
else
{
console
.
info
(
"
The key of startup dose not contain.
"
)
}
}).
catch
((
err
)
=>
{
console
.
info
(
"
Check the key of startup failed, err:
"
+
err
)
})
```
### delete
...
...
@@ -522,26 +403,15 @@ delete(key: string, callback: AsyncCallback<void>): void
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
delete
(
'
startup
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Delete startup key failed, err:
"
+
err
)
return
}
console
.
info
(
"
Deleted startup key successfully.
"
)
})
})
}
```
```
ts
preferences
.
delete
(
'
startup
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Delete startup key failed, err:
"
+
err
)
return
}
console
.
info
(
"
Deleted startup key successfully.
"
)
})
```
### delete
...
...
@@ -563,24 +433,14 @@ delete(key: string): Promise<void>
| Promise
<
void
>
| Promise实例,用于异步处理。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
let
promiseDelete
=
preferences
.
delete
(
'
startup
'
)
promiseDelete
.
then
(()
=>
{
console
.
info
(
"
Deleted startup key successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Delete startup key failed, err:
"
+
err
)
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
preferences
.
delete
(
'
startup
'
)
promise
.
then
(()
=>
{
console
.
info
(
"
Deleted startup key successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Delete startup key failed, err:
"
+
err
)
})
```
### flush
...
...
@@ -597,26 +457,15 @@ flush(callback: AsyncCallback<void>): void
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
})
})
}
```
```
ts
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
})
```
### flush
...
...
@@ -633,24 +482,14 @@ flush(): Promise<void>
| Promise
<
void
>
| Promise实例,用于异步处理。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
let
promiseFlush
=
preferences
.
flush
()
promiseFlush
.
then
(()
=>
{
console
.
info
(
"
Flushed to file successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
preferences
.
flush
()
promise
.
then
(()
=>
{
console
.
info
(
"
Flushed to file successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
})
```
### clear
...
...
@@ -667,26 +506,15 @@ clear(callback: AsyncCallback<void>): void
| callback | AsyncCallback
<
void
>
| 是 | 回调函数。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
clear
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Clear to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Cleared to file successfully.
"
)
})
})
}
```
```
ts
preferences
.
clear
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Clear to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Cleared to file successfully.
"
)
})
```
### clear
...
...
@@ -703,24 +531,14 @@ clear(): Promise<void>
| Promise
<
void
>
| Promise实例,用于异步处理。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
let
promiseClear
=
preferences
.
clear
()
promiseClear
.
then
(()
=>
{
console
.
info
(
"
Cleared to file successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Clear to file failed, err:
"
+
err
)
})
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
})
}
```
```
ts
let
promise
=
preferences
.
clear
()
promise
.
then
(()
=>
{
console
.
info
(
"
Cleared to file successfully.
"
)
}).
catch
((
err
)
=>
{
console
.
info
(
"
Clear to file failed, err:
"
+
err
)
})
```
### on('change')
...
...
@@ -738,37 +556,30 @@ on(type: 'change', callback: Callback<{ key : string }>): void
| callback | Callback
<
{ key : string }
>
| 回调对象实例。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
var
observer
=
function
(
key
)
{
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
on
(
'
change
'
,
observer
)
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
// observer will be called.
})
})
})
}
```
```
ts
var
observer
=
function
(
key
)
{
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
}
...
preferences
.
on
(
'
change
'
,
observer
)
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
// observer will be called.
})
})
```
### off('change')
...
...
@@ -786,37 +597,31 @@ off(type: 'change', callback: Callback<{ key : string }>): void
| callback | Callback
<
{ key : string }
>
| 需要取消的回调对象实例。 |
**示例:**
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
var
observer
=
function
(
key
)
{
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Get the preferences failed, err:
"
+
err
)
return
}
preferences
.
on
(
'
change
'
,
observer
)
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
// observer will be called.
preferences
.
off
(
'
change
'
,
observer
)
})
})
})
}
```
```
ts
var
observer
=
function
(
key
)
{
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
}
...
preferences
.
on
(
'
change
'
,
observer
)
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed, err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed, err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
// observer will be called.
preferences
.
off
(
'
change
'
,
observer
)
})
})
```
## ValueType
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录