Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
5f7b6300
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看板
未验证
提交
5f7b6300
编写于
7月 31, 2023
作者:
O
openharmony_ci
提交者:
Gitee
7月 31, 2023
浏览文件
操作
浏览文件
下载
差异文件
!21652 修复preferences示例代码编译问题与代码格式化
Merge pull request !21652 from 常佳兴/master
上级
acefe6ef
2cc0a969
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
87 addition
and
88 deletion
+87
-88
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+87
-88
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
5f7b6300
...
...
@@ -55,12 +55,12 @@ let preferences = null;
try
{
data_preferences
.
getPreferences
(
context
,
'
mystore
'
,
function
(
err
,
val
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
preferences
=
val
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
preferences
=
val
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
...
...
@@ -195,14 +195,14 @@ let context = featureAbility.getContext();
let
preferences
=
null
;
try
{
data_preferences
.
getPreferences
(
context
,
{
name
:
'
mystore
'
},
function
(
err
,
val
)
{
data_preferences
.
getPreferences
(
context
,
{
name
:
'
mystore
'
},
function
(
err
,
val
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
preferences
=
val
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
}
preferences
=
val
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
...
...
@@ -219,7 +219,7 @@ let preferences = null;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
val
)
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
val
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -275,7 +275,7 @@ let context = featureAbility.getContext();
let
preferences
=
null
;
try
{
let
promise
=
data_preferences
.
getPreferences
(
context
,
{
name
:
'
mystore
'
});
let
promise
=
data_preferences
.
getPreferences
(
context
,
{
name
:
'
mystore
'
});
promise
.
then
((
object
)
=>
{
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
...
...
@@ -297,7 +297,7 @@ let preferences = null;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
let
promise
=
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
promise
.
then
((
object
)
=>
{
preferences
=
object
;
console
.
info
(
"
Succeeded in getting preferences.
"
);
...
...
@@ -493,7 +493,7 @@ import featureAbility from '@ohos.ability.featureAbility';
let
context
=
featureAbility
.
getContext
();
try
{
data_preferences
.
deletePreferences
(
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
data_preferences
.
deletePreferences
(
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -513,7 +513,7 @@ import UIAbility from '@ohos.app.ability.UIAbility';
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
deletePreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
)
{
data_preferences
.
deletePreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to delete preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -571,7 +571,7 @@ import featureAbility from '@ohos.ability.featureAbility';
let
context
=
featureAbility
.
getContext
();
try
{
let
promise
=
data_preferences
.
deletePreferences
(
context
,
{
name
:
'
mystore
'
});
let
promise
=
data_preferences
.
deletePreferences
(
context
,
{
name
:
'
mystore
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting preferences.
"
);
}).
catch
((
err
)
=>
{
...
...
@@ -590,7 +590,7 @@ import UIAbility from '@ohos.app.ability.UIAbility';
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
let
promise
=
data_preferences
.
deletePreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting preferences.
"
);
}).
catch
((
err
)
=>
{
...
...
@@ -702,8 +702,8 @@ import featureAbility from '@ohos.ability.featureAbility';
let
context
=
featureAbility
.
getContext
();
try
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
context
,
'
mystore
'
);
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
...
...
@@ -821,7 +821,7 @@ FA模型示例:
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
try
{
data_preferences
.
removePreferencesFromCache
(
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
data_preferences
.
removePreferencesFromCache
(
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -841,7 +841,7 @@ import UIAbility from '@ohos.app.ability.UIAbility';
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
)
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -898,9 +898,9 @@ FA模型示例:
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
try
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
context
,
{
name
:
'
mystore
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
let
promise
=
data_preferences
.
removePreferencesFromCache
(
context
,
{
name
:
'
mystore
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
...
...
@@ -917,7 +917,7 @@ import UIAbility from '@ohos.app.ability.UIAbility';
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
)
{
try
{
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
let
promise
=
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
});
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in removing preferences.
"
);
}).
catch
((
err
)
=>
{
...
...
@@ -1405,7 +1405,7 @@ delete(key: string): Promise<void>
```
js
try
{
let
promise
=
preferences
.
delete
(
'
startup
'
);
promise
.
then
(()
=>
{
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in deleting the key 'startup'.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to delete the key 'startup'. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
...
...
@@ -1520,7 +1520,7 @@ clear(callback: AsyncCallback<void>): void
```
js
try
{
preferences
.
clear
(
function
(
err
)
{
preferences
.
clear
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to clear. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -1552,8 +1552,8 @@ clear(): Promise<void>
```
js
try
{
let
promise
=
preferences
.
clear
();
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in clearing.
"
);
promise
.
then
(()
=>
{
console
.
info
(
"
Succeeded in clearing.
"
);
}).
catch
((
err
)
=>
{
console
.
info
(
"
Failed to clear. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
})
...
...
@@ -1601,33 +1601,33 @@ on(type: 'change', callback: Callback<{ key : string }>): void
```
js
try
{
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences.
"
);
return
;
}
let
observer
=
function
(
key
)
{
console
.
info
(
"
The key
"
+
key
+
"
changed.
"
);
}
preferences
.
on
(
'
change
'
,
observer
);
preferences
.
put
(
'
startup
'
,
'
manual
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to flush. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in flushing.
"
);
})
})
})
data_preferences
.
getPreferences
(
this
.
context
,
'
mystore
'
,
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences.
"
);
return
;
}
let
observer
=
function
(
key
)
{
console
.
info
(
"
The key
"
+
key
+
"
changed.
"
);
}
preferences
.
on
(
'
change
'
,
observer
);
preferences
.
put
(
'
startup
'
,
'
manual
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to flush. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in flushing.
"
);
})
})
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to flush. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
console
.
info
(
"
Failed to flush. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
...
...
@@ -1660,40 +1660,39 @@ on(type: 'multiProcessChange', callback: Callback<{ key : string }>): void
```
js
try
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences.
"
);
return
;
}
let
observer
=
function
(
key
)
{
console
.
info
(
"
The key
"
+
key
+
"
changed.
"
);
}
preferences
.
on
(
'
multiProcessChange
'
,
observer
);
preferences
.
put
(
'
startup
'
,
'
manual
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to flush. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in flushing.
"
);
})
})
})
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences.
"
);
return
;
}
let
observer
=
function
(
key
)
{
console
.
info
(
"
The key
"
+
key
+
"
changed.
"
);
}
preferences
.
on
(
'
multiProcessChange
'
,
observer
);
preferences
.
put
(
'
startup
'
,
'
manual
'
,
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to put the value of 'startup'. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in putting the value of 'startup'.
"
);
preferences
.
flush
(
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to flush. Cause:
"
+
err
);
return
;
}
console
.
info
(
"
Succeeded in flushing.
"
);
})
})
})
}
catch
(
err
)
{
console
.
info
(
"
Failed to flush. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
console
.
info
(
"
Failed to flush. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
}
```
**示例2:**
```
js
let
preferences
=
null
;
try
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
},
function
(
err
,
val
)
{
if
(
err
)
{
...
...
@@ -1704,7 +1703,7 @@ try {
let
observer
=
function
(
key
)
{
console
.
info
(
"
The key
"
+
key
+
"
changed.
"
);
try
{
data_preferences
.
removePreferencesFromCache
(
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
data_preferences
.
removePreferencesFromCache
(
this
.
context
,
{
name
:
'
mystore
'
},
function
(
err
)
{
if
(
err
)
{
console
.
info
(
"
Failed to remove preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -1717,7 +1716,7 @@ try {
}
try
{
data_preferences
.
getPreferences
(
context
,
{
name
:
'
mystore
'
},
function
(
err
,
val
)
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
},
function
(
err
,
val
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences. code =
"
+
err
.
code
+
"
, message =
"
+
err
.
message
);
return
;
...
...
@@ -1820,7 +1819,7 @@ off(type: 'multiProcessChange', callback?: Callback<{ key : string }>): vo
```
js
try
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
preferences
)
{
data_preferences
.
getPreferences
(
this
.
context
,
{
name
:
'
mystore
'
,
dataGroupId
:
'
myId
'
},
function
(
err
,
preferences
)
{
if
(
err
)
{
console
.
info
(
"
Failed to get preferences.
"
);
return
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录