Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
e3d45239
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看板
未验证
提交
e3d45239
编写于
5月 05, 2022
作者:
O
openharmony_ci
提交者:
Gitee
5月 05, 2022
浏览文件
操作
浏览文件
下载
差异文件
!3682 Update preferences API9 sample code
Merge pull request !3682 from PaDaBoo/master
上级
c586b9bf
08f7f467
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
329 addition
and
151 deletion
+329
-151
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+329
-151
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
e3d45239
...
@@ -44,27 +44,29 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
...
@@ -44,27 +44,29 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
```
ts
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
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
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed with err:
"
+
err
)
console
.
info
(
"
Get the preferences failed
"
)
return
return
;
}
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed with
err:
"
+
err
)
console
.
info
(
"
Put the value of startup failed,
err:
"
+
err
)
return
return
}
}
console
.
info
(
"
Flushed to file successfully.
"
)
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.
"
)
})
})
})
})
})
}
)
}
```
```
...
@@ -91,26 +93,28 @@ getPreferences(context: Context, name: string): Promise<Preferences>
...
@@ -91,26 +93,28 @@ getPreferences(context: Context, name: string): Promise<Preferences>
```
ts
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
promise
.
then
((
preferences
)
=>
{
promise
.
then
((
preferences
)
=>
{
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed with err:
"
+
err
)
return
}
console
.
info
(
"
Put the value of startup successfully.
"
)
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed with
err:
"
+
err
)
console
.
info
(
"
Put the value of startup failed,
err:
"
+
err
)
return
return
}
}
console
.
info
(
"
Flushed to file successfully.
"
)
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
"
)
})
})
}).
catch
((
err
)
=>
{
}
console
.
info
(
"
Get the preferences failed
"
)
})
```
```
...
@@ -134,14 +138,16 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
...
@@ -134,14 +138,16 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
```
ts
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Deleted failed with err:
"
+
err
)
console
.
info
(
"
Deleted failed, err:
"
+
err
)
return
return
}
}
console
.
info
(
"
Deleted successfully.
"
)
console
.
info
(
"
Deleted successfully.
"
)
})
})
}
```
```
...
@@ -169,13 +175,15 @@ deletePreferences(context: Context, name: string): Promise<void>
...
@@ -169,13 +175,15 @@ deletePreferences(context: Context, name: string): Promise<void>
```
ts
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
proDelete
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
)
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
)
proDelete
.
then
(()
=>
{
promise
.
then
(()
=>
{
console
.
info
(
"
Deleted successfully.
"
)
console
.
info
(
"
Deleted successfully.
"
)
}).
catch
((
err
)
=>
{
}).
catch
((
err
)
=>
{
console
.
info
(
"
Deleted failed with err:
"
+
err
)
console
.
info
(
"
Deleted failed, err:
"
+
err
)
})
})
}
```
```
...
@@ -198,14 +206,16 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
...
@@ -198,14 +206,16 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
```
ts
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
,
function
(
err
)
{
if
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Removed preferences from cache failed with err:
"
+
err
)
console
.
info
(
"
Removed preferences from cache failed, err:
"
+
err
)
return
return
}
}
console
.
info
(
"
Removed preferences from cache successfully.
"
)
console
.
info
(
"
Removed preferences from cache successfully.
"
)
})
})
}
```
```
...
@@ -232,13 +242,15 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
...
@@ -232,13 +242,15 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
```
ts
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
)
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
'
mystore
'
)
promise
.
then
(()
=>
{
promise
.
then
(()
=>
{
console
.
info
(
"
Removed preferences from cache successfully.
"
)
console
.
info
(
"
Removed preferences from cache successfully.
"
)
}).
catch
((
err
)
=>
{
}).
catch
((
err
)
=>
{
console
.
info
(
"
Removed preferences from cache failed with err:
"
+
err
)
console
.
info
(
"
Removed preferences from cache failed, err:
"
+
err
)
})
})
}
```
```
...
@@ -264,13 +276,24 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>):
...
@@ -264,13 +276,24 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>):
-
示例:
-
示例:
```
ts
```
ts
preferences
.
get
(
'
startup
'
,
'
default
'
,
function
(
err
,
value
)
{
import
Ability
from
'
@ohos.application.Ability
'
if
(
err
)
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Get the value of startup failed with err:
"
+
err
)
export
default
class
MainAbility
extends
Ability
{
return
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
console
.
info
(
"
The value of startup is
"
+
value
)
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
)
})
})
}
```
```
...
@@ -295,12 +318,22 @@ get(key: string, defValue: ValueType): Promise<ValueType>
...
@@ -295,12 +318,22 @@ get(key: string, defValue: ValueType): Promise<ValueType>
-
示例:
-
示例:
```
ts
```
ts
let
promiseget
=
preferences
.
get
(
'
startup
'
,
'
default
'
)
import
Ability
from
'
@ohos.application.Ability
'
promiseget
.
then
((
value
)
=>
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
The value of startup is
"
+
value
)
export
default
class
MainAbility
extends
Ability
{
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the value of startup failed with err:
"
+
err
)
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
)
})
}
```
```
...
@@ -321,13 +354,24 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void
...
@@ -321,13 +354,24 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void
-
示例:
-
示例:
```
ts
```
ts
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
import
Ability
from
'
@ohos.application.Ability
'
if
(
err
)
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Put the value of startup failed with err:
"
+
err
)
export
default
class
MainAbility
extends
Ability
{
return
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
console
.
info
(
"
Put the value of startup successfully.
"
)
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.
"
)
})
})
}
```
```
...
@@ -352,12 +396,22 @@ put(key: string, value: ValueType): Promise<void>
...
@@ -352,12 +396,22 @@ put(key: string, value: ValueType): Promise<void>
-
示例:
-
示例:
```
ts
```
ts
let
promiseput
=
preferences
.
put
(
'
startup
'
,
'
auto
'
)
import
Ability
from
'
@ohos.application.Ability
'
promiseput
.
then
(()
=>
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Put the value of startup successfully.
"
)
export
default
class
MainAbility
extends
Ability
{
}).
catch
((
err
)
=>
{
console
.
info
(
"
Put the value of startup failed with err:
"
+
err
)
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
)
})
}
```
```
...
@@ -382,15 +436,28 @@ has(key: string, callback: AsyncCallback<boolean>): boolean
...
@@ -382,15 +436,28 @@ has(key: string, callback: AsyncCallback<boolean>): boolean
-
示例:
-
示例:
```
ts
```
ts
preferences
.
has
(
'
startup
'
,
function
(
err
,
isExist
)
{
import
Ability
from
'
@ohos.application.Ability
'
if
(
err
)
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Check the key of startup failed with err:
"
+
err
)
export
default
class
MainAbility
extends
Ability
{
return
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
isExist
)
{
if
(
err
)
{
console
.
info
(
"
The key of startup is contained.
"
)
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.
"
)
}
})
})
}
```
```
...
@@ -414,14 +481,26 @@ has(key: string): Promise<boolean>
...
@@ -414,14 +481,26 @@ has(key: string): Promise<boolean>
-
示例:
-
示例:
```
ts
```
ts
let
promisehas
=
preferences
.
has
(
'
startup
'
)
import
Ability
from
'
@ohos.application.Ability
'
promisehas
.
then
((
isExist
)
=>
{
import
data_preferences
from
'
@ohos.data.preferences
'
if
(
isExist
)
{
export
default
class
MainAbility
extends
Ability
{
console
.
info
(
"
The key of startup is contained.
"
)
}
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
)
}).
catch
((
err
)
=>
{
promise
.
then
((
preferences
)
=>
{
console
.
info
(
"
Check the key of startup failed with err:
"
+
err
)
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
)
})
}
```
```
...
@@ -441,13 +520,24 @@ delete(key: string, callback: AsyncCallback<void>): void
...
@@ -441,13 +520,24 @@ delete(key: string, callback: AsyncCallback<void>): void
-
示例:
-
示例:
```
ts
```
ts
preferences
.
delete
(
'
startup
'
,
function
(
err
)
{
import
Ability
from
'
@ohos.application.Ability
'
if
(
err
)
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Delete startup key failed with err:
"
+
err
)
export
default
class
MainAbility
extends
Ability
{
return
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
console
.
info
(
"
Deleted startup key successfully.
"
)
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.
"
)
})
})
}
```
```
...
@@ -471,12 +561,22 @@ delete(key: string): Promise<void>
...
@@ -471,12 +561,22 @@ delete(key: string): Promise<void>
-
示例:
-
示例:
```
ts
```
ts
let
promisedel
=
preferences
.
delete
(
'
startup
'
)
import
Ability
from
'
@ohos.application.Ability
'
promisedel
.
then
(()
=>
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Deleted startup key successfully.
"
)
export
default
class
MainAbility
extends
Ability
{
}).
catch
((
err
)
=>
{
console
.
info
(
"
Delete startup key failed with err:
"
+
err
)
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
)
})
}
```
```
...
@@ -495,13 +595,24 @@ flush(callback: AsyncCallback<void>): void
...
@@ -495,13 +595,24 @@ flush(callback: AsyncCallback<void>): void
-
示例:
-
示例:
```
ts
```
ts
preferences
.
flush
(
function
(
err
)
{
import
Ability
from
'
@ohos.application.Ability
'
if
(
err
)
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Flush to file failed with err:
"
+
err
)
export
default
class
MainAbility
extends
Ability
{
return
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
console
.
info
(
"
Flushed to file successfully.
"
)
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.
"
)
})
})
}
```
```
...
@@ -520,12 +631,22 @@ flush(): Promise<void>
...
@@ -520,12 +631,22 @@ flush(): Promise<void>
-
示例:
-
示例:
```
ts
```
ts
let
promiseflush
=
preferences
.
flush
()
import
Ability
from
'
@ohos.application.Ability
'
promiseflush
.
then
(()
=>
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Flushed to file successfully.
"
)
export
default
class
MainAbility
extends
Ability
{
}).
catch
((
err
)
=>
{
console
.
info
(
"
Flush to file failed with err:
"
+
err
)
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
)
})
}
```
```
...
@@ -544,13 +665,24 @@ clear(callback: AsyncCallback<void>): void
...
@@ -544,13 +665,24 @@ clear(callback: AsyncCallback<void>): void
-
示例:
-
示例:
```
ts
```
ts
preferences
.
clear
(
function
(
err
)
{
import
Ability
from
'
@ohos.application.Ability
'
if
(
err
)
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Clear to file failed with err:
"
+
err
)
export
default
class
MainAbility
extends
Ability
{
return
}
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
console
.
info
(
"
Cleared to file successfully.
"
)
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.
"
)
})
})
}
```
```
...
@@ -569,12 +701,22 @@ clear(): Promise<void>
...
@@ -569,12 +701,22 @@ clear(): Promise<void>
-
示例:
-
示例:
```
ts
```
ts
let
promiseclear
=
preferences
.
clear
()
import
Ability
from
'
@ohos.application.Ability
'
promiseclear
.
then
(()
=>
{
import
data_preferences
from
'
@ohos.data.preferences
'
console
.
info
(
"
Cleared to file successfully.
"
)
export
default
class
MainAbility
extends
Ability
{
}).
catch
((
err
)
=>
{
console
.
info
(
"
Clear to file failed with err:
"
+
err
)
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
)
})
}
```
```
...
@@ -594,24 +736,35 @@ on(type: 'change', callback: Callback<{ key : string }>): void
...
@@ -594,24 +736,35 @@ on(type: 'change', callback: Callback<{ key : string }>): void
-
示例:
-
示例:
```
ts
```
ts
var
observer
=
function
(
key
)
{
import
Ability
from
'
@ohos.application.Ability
'
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
import
data_preferences
from
'
@ohos.data.preferences
'
}
export
default
class
MainAbility
extends
Ability
{
preferences
.
on
(
'
change
'
,
observer
)
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
var
observer
=
function
(
key
)
{
if
(
err
)
{
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
console
.
info
(
"
Put the value of startup failed with err:
"
+
err
)
return
}
}
console
.
info
(
"
Put the value of startup successfully.
"
)
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Flush to file failed with
err:
"
+
err
)
console
.
info
(
"
Get the preferences failed,
err:
"
+
err
)
return
return
}
}
console
.
info
(
"
Flushed to file successfully.
"
)
// observer will be called.
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.
})
})
})
})
}
)
}
```
```
...
@@ -631,8 +784,33 @@ off(type: 'change', callback: Callback<{ key : string }>): void
...
@@ -631,8 +784,33 @@ off(type: 'change', callback: Callback<{ key : string }>): void
-
示例:
-
示例:
```
ts
```
ts
var
observer
=
function
(
key
)
{
import
Ability
from
'
@ohos.application.Ability
'
console
.
info
(
"
The key of
"
+
key
+
"
changed.
"
)
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
)
})
})
})
}
}
preferences
.
off
(
'
change
'
,
observer
)
```
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录