Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
050987a2
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看板
未验证
提交
050987a2
编写于
4月 02, 2022
作者:
O
openharmony_ci
提交者:
Gitee
4月 02, 2022
浏览文件
操作
浏览文件
下载
差异文件
!2999 修改Preferences的示例程序
Merge pull request !2999 from PaDaBoo/master
上级
f9307547
e665505a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
61 addition
and
25 deletion
+61
-25
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
...pplication-dev/reference/apis/js-apis-data-preferences.md
+61
-25
未找到文件。
zh-cn/application-dev/reference/apis/js-apis-data-preferences.md
浏览文件 @
050987a2
...
...
@@ -10,7 +10,7 @@
## 导入模块
```
import data_
P
references from '@ohos.data.preferences'
import data_
p
references from '@ohos.data.preferences'
```
## 属性
...
...
@@ -23,7 +23,7 @@ import data_Preferences from '@ohos.data.preferences'
| MAX_VALUE_LENGTH | string | 是 | 否 | string类型value的最大长度限制,大小为8192字节。 |
## data_
P
references.getPreferences
## data_
p
references.getPreferences
getPreferences(context: Context, name: string, callback: AsyncCallback
<
Preferences
>
): void
...
...
@@ -42,20 +42,32 @@ getPreferences(context: Context, name: string, callback: AsyncCallback<Prefer
-
示例:
```
import Ability from '@ohos.application.Ability'
import data_
P
references from '@ohos.data.preferences'
import data_
p
references from '@ohos.data.preferences'
var path = this.context.getDataBaseDir()
data_
P
references.getPreferences(this.context, 'mystore', function (err, preferences) {
data_
p
references.getPreferences(this.context, 'mystore', function (err, preferences) {
if (err) {
console.info("Get the preferences failed, path: " + path + '/mystore')
return;
}
preferences.putSync('startup', 'auto')
preferences.flushSync()
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) {
console.info("Flush to file failed with err: " + err)
return
}
console.info("Flushed to file successfully.")
})
})
})
```
## data_
P
references.getPreferences
## data_
p
references.getPreferences
getPreferences(context: Context, name: string): Promise
<
Preferences
>
...
...
@@ -77,19 +89,31 @@ getPreferences(context: Context, name: string): Promise<Preferences>
-
示例:
```
import Ability from '@ohos.application.Ability'
import data_
P
references from '@ohos.data.preferences'
import data_
p
references from '@ohos.data.preferences'
var path = this.context.getDataBaseDir()
let promisePre = data_
P
references.getPreferences(this.context, 'mystore')
let promisePre = data_
p
references.getPreferences(this.context, 'mystore')
promisePre.then((preferences) => {
preferences.putSync('startup', 'auto')
preferences.flushSync()
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) {
console.info("Flush to file failed with err: " + err)
return
}
console.info("Flushed to file successfully.")
})
})
}).catch((err) => {
console.info("Get the preferences failed, path: " + path + '/mystore')
})
```
## data_
P
references.deletePreferences
## data_
p
references.deletePreferences
deletePreferences(context: Context, name: string, callback: AsyncCallback
<
void
>
): void
...
...
@@ -107,8 +131,8 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
-
示例:
```
import Ability from '@ohos.application.Ability'
import data_
P
references from '@ohos.data.preferences'
data_
P
references.deletePreferences(this.context, 'mystore', function (err) {
import data_
p
references from '@ohos.data.preferences'
data_
p
references.deletePreferences(this.context, 'mystore', function (err) {
if (err) {
console.info("Deleted failed with err: " + err)
return
...
...
@@ -118,7 +142,7 @@ deletePreferences(context: Context, name: string, callback: AsyncCallback<voi
```
## data_
P
references.deletePreferences
## data_
p
references.deletePreferences
deletePreferences(context: Context, name: string): Promise
<
void
>
...
...
@@ -140,8 +164,8 @@ deletePreferences(context: Context, name: string): Promise<void>
-
示例:
```
import Ability from '@ohos.application.Ability'
import data_
P
references from '@ohos.data.preferences'
let promisedelPre = data_
P
references.deletePreferences(this.context, 'mystore')
import data_
p
references from '@ohos.data.preferences'
let promisedelPre = data_
p
references.deletePreferences(this.context, 'mystore')
promisedelPre.then(() => {
console.info("Deleted successfully.")
}).catch((err) => {
...
...
@@ -150,7 +174,7 @@ deletePreferences(context: Context, name: string): Promise<void>
```
## data_
P
references.removePreferencesFromCache
## data_
p
references.removePreferencesFromCache
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback
<
void
>
): void
...
...
@@ -170,8 +194,8 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
-
示例:
```
import Ability from '@ohos.application.Ability'
import data_
P
references from '@ohos.data.preferences'
data_
P
references.removePreferencesFromCache(this.context, 'mystore', function (err) {
import data_
p
references from '@ohos.data.preferences'
data_
p
references.removePreferencesFromCache(this.context, 'mystore', function (err) {
if (err) {
console.info("Removed preferences from cache failed with err: " + err)
return
...
...
@@ -181,7 +205,7 @@ removePreferencesFromCache(context: Context, name: string, callback: AsyncCallba
```
## data_
P
references.removePreferencesFromCache
## data_
p
references.removePreferencesFromCache
removePreferencesFromCache(context: Context, name: string): Promise
<
void
>
...
...
@@ -205,8 +229,8 @@ removePreferencesFromCache(context: Context, name: string): Promise<void>
-
示例:
```
import Ability from '@ohos.application.Ability'
import data_
P
references from '@ohos.data.preferences'
let promiserevPre = data_
P
references.removePreferencesFromCache(this.context, 'mystore')
import data_
p
references from '@ohos.data.preferences'
let promiserevPre = data_
p
references.removePreferencesFromCache(this.context, 'mystore')
promiserevPre.then(() => {
console.info("Removed preferences from cache successfully.")
}).catch((err) => {
...
...
@@ -595,8 +619,20 @@ on(type: 'change', callback: Callback<{ key : string }>): void
console.info("The key of " + key + " changed.")
}
preferences.on('change', observer)
preferences.put('startup', 'auto')
preferences.flush() // observer will be called.
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) {
console.info("Flush to file failed with err: " + err)
return
}
console.info("Flushed to file successfully.") // observer will be called.
})
})
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录