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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
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,6 +44,7 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
```
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
)
{
...
...
@@ -52,19 +53,20 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
}
preferences
.
put
(
'
startup
'
,
'
auto
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Put the value of startup failed with
err:
"
+
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 with
err:
"
+
err
)
console
.
info
(
"
Flush to file failed,
err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
})
})
})
}
```
...
...
@@ -91,18 +93,19 @@ getPreferences(context: Context, name: string): Promise<Preferences>
```
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 with
err:
"
+
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 with
err:
"
+
err
)
console
.
info
(
"
Flush to file failed,
err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
...
...
@@ -111,6 +114,7 @@ getPreferences(context: Context, name: string): Promise<Preferences>
}).
catch
((
err
)
=>
{
console
.
info
(
"
Get the preferences failed
"
)
})
}
```
...
...
@@ -134,14 +138,16 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
```
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 with
err:
"
+
err
)
console
.
info
(
"
Deleted failed,
err:
"
+
err
)
return
}
console
.
info
(
"
Deleted successfully.
"
)
})
}
```
...
...
@@ -169,13 +175,15 @@ deletePreferences(context: Context, name: string): Promise<void>
```
ts
import
Ability
from
'
@ohos.application.Ability
'
import
data_preferences
from
'
@ohos.data.preferences
'
export
default
class
MainAbility
extends
Ability
{
let
proDelet
e
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
)
proDelet
e
.
then
(()
=>
{
let
promis
e
=
data_preferences
.
deletePreferences
(
this
.
context
,
'
mystore
'
)
promis
e
.
then
(()
=>
{
console
.
info
(
"
Deleted successfully.
"
)
}).
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
```
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 with
err:
"
+
err
)
console
.
info
(
"
Removed preferences from cache failed,
err:
"
+
err
)
return
}
console
.
info
(
"
Removed preferences from cache successfully.
"
)
})
}
```
...
...
@@ -232,13 +242,15 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
```
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 with
err:
"
+
err
)
console
.
info
(
"
Removed preferences from cache failed,
err:
"
+
err
)
})
}
```
...
...
@@ -264,13 +276,24 @@ get(key: string, defValue: 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 with
err:
"
+
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>
-
示例:
```
ts
let
promiseget
=
preferences
.
get
(
'
startup
'
,
'
default
'
)
promiseget
.
then
((
value
)
=>
{
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 with err:
"
+
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
-
示例:
```
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 with
err:
"
+
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>
-
示例:
```
ts
let
promiseput
=
preferences
.
put
(
'
startup
'
,
'
auto
'
)
promiseput
.
then
(()
=>
{
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 with err:
"
+
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
-
示例:
```
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 with
err:
"
+
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>
-
示例:
```
ts
let
promisehas
=
preferences
.
has
(
'
startup
'
)
promisehas
.
then
((
isExist
)
=>
{
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 with
err:
"
+
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
-
示例:
```
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 with
err:
"
+
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>
-
示例:
```
ts
let
promisedel
=
preferences
.
delete
(
'
startup
'
)
promisedel
.
then
(()
=>
{
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 with
err:
"
+
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
-
示例:
```
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 with
err:
"
+
err
)
console
.
info
(
"
Flush to file failed,
err:
"
+
err
)
return
}
console
.
info
(
"
Flushed to file successfully.
"
)
})
})
}
```
...
...
@@ -520,12 +631,22 @@ flush(): Promise<void>
-
示例:
```
ts
let
promiseflush
=
preferences
.
flush
()
promiseflush
.
then
(()
=>
{
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 with err:
"
+
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
-
示例:
```
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 with
err:
"
+
err
)
console
.
info
(
"
Clear to file failed,
err:
"
+
err
)
return
}
console
.
info
(
"
Cleared to file successfully.
"
)
})
})
}
```
...
...
@@ -569,12 +701,22 @@ clear(): Promise<void>
-
示例:
```
ts
let
promiseclear
=
preferences
.
clear
()
promiseclear
.
then
(()
=>
{
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 with err:
"
+
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
-
示例:
```
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 with
err:
"
+
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 with
err:
"
+
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
-
示例:
```
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
)
})
})
})
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录