js-apis-data-preferences.md 35.7 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.data.preferences (首选项)
P
PaDoBoo 已提交
2

3 4 5
首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。

数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型以及这3种类型的数组类型。
P
PaDoBoo 已提交
6 7


P
PaDaBoo 已提交
8
> **说明:**
9
>
10
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
P
PaDoBoo 已提交
11 12 13 14


## 导入模块

15
```js
16
import data_preferences from '@ohos.data.preferences';
P
PaDoBoo 已提交
17 18
```

G
ge-yafang 已提交
19
## 常量
P
PaDoBoo 已提交
20

21
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
22

23 24
| 名称             | 参数类型 | 可读 | 可写 | 说明                                    |
| ---------------- | -------- | ---- | ---- | --------------------------------------- |
25 26
| MAX_KEY_LENGTH   | number   | 是   | 否   | Key的最大长度限制,需小于80个字节。     |
| MAX_VALUE_LENGTH | number   | 是   | 否   | Value的最大长度限制,需小于8192个字节。 |
P
PaDoBoo 已提交
27 28


29
## data_preferences.getPreferences
P
PaDoBoo 已提交
30

P
PaDaBoo 已提交
31
getPreferences(context: Context, name: string, callback: AsyncCallback<Preferences>): void
P
PaDoBoo 已提交
32

33
获取Preferences实例,使用callback异步回调。
P
PaDaBoo 已提交
34

P
PaDaBoo 已提交
35
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
36

G
ge-yafang 已提交
37
**参数:**
38 39 40

| 参数名   | 类型                                             | 必填 | 说明                                                         |
| -------- | ------------------------------------------------ | ---- | ------------------------------------------------------------ |
Z
zhongjianfei 已提交
41
| context  | Context            | 是   | 应用上下文。<br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。                                                 |
42 43
| name     | string                                           | 是   | Preferences实例的名称。                                      |
| callback | AsyncCallback&lt;[Preferences](#preferences)&gt; | 是   | 回调函数。当获取Preferences实例成功,err为undefined,返回Preferences实例;否则err为错误码。 |
P
PaDoBoo 已提交
44

G
ge-yafang 已提交
45
**示例:**
46

47 48
FA模型示例:

49
```js
50
// 获取context
51 52 53
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
let preferences = null;
D
duanweiling 已提交
54

D
duanweiling 已提交
55
try {
D
duanweiling 已提交
56 57
    data_preferences.getPreferences(context, 'mystore', function (err, val) {
        if (err) {
D
duanweiling 已提交
58 59 60 61 62
	        console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message);
	        return;
	    }
	    console.info("Succeeded in getting preferences.");
	})
D
duanweiling 已提交
63
} catch (err) {
D
duanweiling 已提交
64
    console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
65
}
P
PaDaBoo 已提交
66
```
P
PaDoBoo 已提交
67

68 69 70 71
Stage模型示例:

```ts
// 获取context
72 73
import Ability from '@ohos.application.Ability';
let context = null;
74 75
class MainAbility extends Ability{
    onWindowStageCreate(windowStage){
76
        context = this.context;
77 78 79
    }
}

80
let preferences = null;
D
duanweiling 已提交
81
try {
D
duanweiling 已提交
82
    data_preferences.getPreferences(context, 'mystore', function (err, val) {
D
duanweiling 已提交
83 84 85 86 87 88
	    if (err) {
	        console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message);
	        return;
	    }
	    console.info("Succeeded in getting preferences.");
	})
D
duanweiling 已提交
89
} catch (err) {
D
duanweiling 已提交
90
	console.info("Failed to get preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
91
}
92
```
P
PaDoBoo 已提交
93

94
## data_preferences.getPreferences
P
PaDoBoo 已提交
95

P
PaDaBoo 已提交
96
getPreferences(context: Context, name: string): Promise&lt;Preferences&gt;
P
PaDoBoo 已提交
97

98
获取Preferences实例,使用Promise异步回调。
P
PaDoBoo 已提交
99

P
PaDaBoo 已提交
100
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
101

G
ge-yafang 已提交
102
**参数:**
103 104 105

| 参数名  | 类型                                  | 必填 | 说明                    |
| ------- | ------------------------------------- | ---- | ----------------------- |
Z
zhongjianfei 已提交
106
| context | Context | 是   | 应用上下文。<br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。            |
107
| name    | string                                | 是   | Preferences实例的名称。 |
P
PaDoBoo 已提交
108

G
ge-yafang 已提交
109
**返回值:**
110

111 112 113
| 类型                                       | 说明                               |
| ------------------------------------------ | ---------------------------------- |
| Promise&lt;[Preferences](#preferences)&gt; | Promise对象,返回Preferences实例。 |
P
PaDoBoo 已提交
114

G
ge-yafang 已提交
115
**示例:**
116

117 118
FA模型示例:

119
```js
120
// 获取context
121 122
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
123

D
duanweiling 已提交
124
let preferences = null;
D
duanweiling 已提交
125
try {
D
duanweiling 已提交
126 127
    let promise = data_preferences.getPreferences(context, 'mystore');
    promise.then((object) => {
D
duanweiling 已提交
128 129
        preferences = object;
        console.info("Succeeded in getting preferences.");
D
duanweiling 已提交
130
    }).catch((err) => {
D
duanweiling 已提交
131
        console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
132 133
    })
} catch(err) {
D
duanweiling 已提交
134
    console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
135
}
P
PaDaBoo 已提交
136
```
P
PaDoBoo 已提交
137

138 139 140 141
Stage模型示例:

```ts
// 获取context
142 143
import Ability from '@ohos.application.Ability';
let context = null;
144 145
class MainAbility extends Ability{
    onWindowStageCreate(windowStage){
146
        context = this.context;
147 148 149
    }
}

D
duanweiling 已提交
150
let preferences = null;
D
duanweiling 已提交
151
try {
D
duanweiling 已提交
152 153
    let promise = data_preferences.getPreferences(context, 'mystore');
    promise.then((object) => {
D
duanweiling 已提交
154 155
        preferences = object;
        console.info("Succeeded in getting preferences.");
D
duanweiling 已提交
156
    }).catch((err) => {
D
duanweiling 已提交
157
        console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
158 159
    })
} catch(err) {
D
duanweiling 已提交
160
    console.log("Failed to get preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
161
}
162
```
P
PaDoBoo 已提交
163

164
## data_preferences.deletePreferences
P
PaDoBoo 已提交
165

166
deletePreferences(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
P
PaDoBoo 已提交
167

168 169 170
从内存中移除指定的Preferences实例,使用callback异步回调。

若Preferences实例有对应的持久化文件,则同时删除其持久化文件。
171

172
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
173

P
PaDaBoo 已提交
174
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
175

G
ge-yafang 已提交
176
**参数:**
177

178 179
| 参数名   | 类型                                  | 必填 | 说明                                                 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
Z
zhongjianfei 已提交
180
| context  | Context | 是   | 应用上下文。<br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。                                         |
181
| name     | string                                | 是   | Preferences实例的名称。                              |
182
| callback | AsyncCallback&lt;void&gt;             | 是   | 回调函数。当移除成功,err为undefined,否则为错误码。 |
P
PaDoBoo 已提交
183

D
duanweiling 已提交
184 185 186 187 188 189 190 191
**错误码:**

以下错误码的详细介绍请参见[首选项错误码](../errorcodes/errorcode-preferences.md)

| 错误码ID | 错误信息                       |
| -------- | ------------------------------|
| 15500010 | Failed to delete preferences. |

G
ge-yafang 已提交
192
**示例:**
193

194 195
FA模型示例:

196
```js
197
// 获取context
198 199
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
200

D
duanweiling 已提交
201
try {
D
duanweiling 已提交
202
    data_preferences.deletePreferences(context, 'mystore', function (err, val) {
D
duanweiling 已提交
203
        if (err) {
D
duanweiling 已提交
204 205
            console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
206
        }
D
duanweiling 已提交
207
        console.info("Succeeded in deleting preferences." );
D
duanweiling 已提交
208 209
    })
} catch (err) {
D
duanweiling 已提交
210
    console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
211
}
P
PaDaBoo 已提交
212
```
P
PaDoBoo 已提交
213

214 215 216 217
Stage模型示例:

```ts
// 获取context
218 219
import Ability from '@ohos.application.Ability';
let context = null;
220 221
class MainAbility extends Ability{
    onWindowStageCreate(windowStage){
222
        context = this.context;
223 224 225
    }
}

D
duanweiling 已提交
226
try {
D
duanweiling 已提交
227
    data_preferences.deletePreferences(context, 'mystore', function (err, val) {
D
duanweiling 已提交
228
        if (err) {
D
duanweiling 已提交
229 230
            console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
231
        }
D
duanweiling 已提交
232
        console.info("Succeeded in deleting preferences." );
D
duanweiling 已提交
233 234
    })
} catch (err) {
D
duanweiling 已提交
235
    console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
236
}
237
```
P
PaDoBoo 已提交
238

239
## data_preferences.deletePreferences
P
PaDoBoo 已提交
240

P
PaDaBoo 已提交
241
deletePreferences(context: Context, name: string): Promise&lt;void&gt;
P
PaDoBoo 已提交
242

243 244 245
从内存中移除指定的Preferences实例,使用Promise异步回调。

若Preferences实例有对应的持久化文件,则同时删除其持久化文件。
246

247
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
248

P
PaDaBoo 已提交
249
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
250

G
ge-yafang 已提交
251
**参数:**
252 253 254

| 参数名  | 类型                                  | 必填 | 说明                    |
| ------- | ------------------------------------- | ---- | ----------------------- |
Z
zhongjianfei 已提交
255
| context | Context | 是   | 应用上下文。<br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。            |
256
| name    | string                                | 是   | Preferences实例的名称。 |
P
PaDoBoo 已提交
257

D
duanweiling 已提交
258 259 260 261 262 263
**返回值:**

| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |

D
duanweiling 已提交
264 265 266 267 268 269 270 271
**错误码:**

以下错误码的详细介绍请参见[首选项错误码](../errorcodes/errorcode-preferences.md)

| 错误码ID | 错误信息                       |
| -------- | ------------------------------|
| 15500010 | Failed to delete preferences. |

G
ge-yafang 已提交
272
**示例:**
273

274 275
FA模型示例:

276
```js
277
// 获取context
278 279
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
280

D
duanweiling 已提交
281
try {
D
duanweiling 已提交
282 283 284
    let promise = data_preferences.deletePreferences(context, 'mystore');
    promise.then(() => {
        console.info("Succeeded in deleting preferences.");
D
duanweiling 已提交
285
    }).catch((err) => {
D
duanweiling 已提交
286
        console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
287 288
    })
} catch(err) {
D
duanweiling 已提交
289
    console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
290
}
P
PaDaBoo 已提交
291
```
P
PaDoBoo 已提交
292

293 294 295 296
Stage模型示例:

```ts
// 获取context
297 298
import Ability from '@ohos.application.Ability';
let context = null;
299 300
class MainAbility extends Ability{
    onWindowStageCreate(windowStage){
301
        context = this.context;
302 303 304
    }
}

D
duanweiling 已提交
305
try{
D
duanweiling 已提交
306 307 308
    let promise = data_preferences.deletePreferences(context, 'mystore');
    promise.then(() => {
        console.info("Succeeded in deleting preferences.");
D
duanweiling 已提交
309
    }).catch((err) => {
D
duanweiling 已提交
310
        console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
311 312
    })
} catch(err) {
D
duanweiling 已提交
313
    console.info("Failed to delete preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
314
}
315
```
P
PaDoBoo 已提交
316

317
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
318

319
removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback&lt;void&gt;): void
P
PaDoBoo 已提交
320

321
从内存中移除指定的Preferences实例,使用callback异步回调。
G
ge-yafang 已提交
322

323
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
324

P
PaDaBoo 已提交
325
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
326

G
ge-yafang 已提交
327
**参数:**
328

329 330
| 参数名   | 类型                                  | 必填 | 说明                                                 |
| -------- | ------------------------------------- | ---- | ---------------------------------------------------- |
Z
zhongjianfei 已提交
331
| context  | Context | 是   | 应用上下文。<br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。                                         |
332
| name     | string                                | 是   | Preferences实例的名称。                              |
333
| callback | AsyncCallback&lt;void&gt;             | 是   | 回调函数。当移除成功,err为undefined,否则为错误码。 |
P
PaDoBoo 已提交
334

G
ge-yafang 已提交
335
**示例:**
336

337 338
FA模型示例:

339
```js
340
// 获取context
341 342
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
343

D
duanweiling 已提交
344
try {
D
duanweiling 已提交
345
    data_preferences.removePreferencesFromCache(context, 'mystore', function (err, val) {
D
duanweiling 已提交
346
        if (err) {
D
duanweiling 已提交
347 348
            console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
349
        }
D
duanweiling 已提交
350 351
        console.info("Succeeded in removing preferences.");
    })
D
duanweiling 已提交
352
} catch (err) {
D
duanweiling 已提交
353
    console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
354
}
P
PaDaBoo 已提交
355
```
P
PaDoBoo 已提交
356

357 358 359 360
Stage模型示例:

```ts
// 获取context
361 362
import Ability from '@ohos.application.Ability';
let context = null;
363 364
class MainAbility extends Ability{
    onWindowStageCreate(windowStage){
365
        context = this.context;
366 367 368
    }
}

D
duanweiling 已提交
369
try {
D
duanweiling 已提交
370
    data_preferences.removePreferencesFromCache(context, 'mystore', function (err, val) {
D
duanweiling 已提交
371
        if (err) {
D
duanweiling 已提交
372 373
            console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
374
        }
D
duanweiling 已提交
375 376
        console.info("Succeeded in removing preferences.");
    })
D
duanweiling 已提交
377
} catch (err) {
D
duanweiling 已提交
378
    console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
379
}
D
duanweiling 已提交
380

381
```
P
PaDoBoo 已提交
382

383
## data_preferences.removePreferencesFromCache
P
PaDoBoo 已提交
384

P
PaDaBoo 已提交
385
removePreferencesFromCache(context: Context, name: string): Promise&lt;void&gt;
P
PaDoBoo 已提交
386

A
Annie_wang 已提交
387
从内存中移除指定的Preferences实例,使用Promise异步回调。
G
ge-yafang 已提交
388

389
调用该接口后,应用不允许再使用该Preferences实例进行数据操作,否则会出现数据一致性问题。
P
PaDoBoo 已提交
390

P
PaDaBoo 已提交
391
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
392

G
ge-yafang 已提交
393
**参数:**
394 395 396

| 参数名  | 类型                                  | 必填 | 说明                    |
| ------- | ------------------------------------- | ---- | ----------------------- |
Z
zhongjianfei 已提交
397
| context | Context | 是   | 应用上下文。<br>FA模型的应用Context定义见[Context](js-apis-inner-app-context.md)<br>Stage模型的应用Context定义见[Context](js-apis-ability-context.md)。            |
398
| name    | string                                | 是   | Preferences实例的名称。 |
P
PaDoBoo 已提交
399

G
ge-yafang 已提交
400
**返回值:**
401

402 403 404
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
405

G
ge-yafang 已提交
406
**示例:**
407

408 409
FA模型示例:

410
```js
411
// 获取context
412 413
import featureAbility from '@ohos.ability.featureAbility';
let context = featureAbility.getContext();
414

D
duanweiling 已提交
415
try {
D
duanweiling 已提交
416 417 418
    let promise = data_preferences.removePreferencesFromCache(context, 'mystore');
	promise.then(() => {
    	console.info("Succeeded in removing preferences.");
D
duanweiling 已提交
419
    }).catch((err) => {
D
duanweiling 已提交
420
        console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
421 422
    })
} catch(err) {
D
duanweiling 已提交
423
    console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
424
}
P
PaDaBoo 已提交
425
```
P
PaDoBoo 已提交
426

427 428 429 430
Stage模型示例:

```ts
// 获取context
431 432
import Ability from '@ohos.application.Ability';
let context = null;
433 434
class MainAbility extends Ability{
    onWindowStageCreate(windowStage){
435
        context = this.context;
436 437 438
    }
}

D
duanweiling 已提交
439
try {
D
duanweiling 已提交
440 441 442
    let promise = data_preferences.removePreferencesFromCache(context, 'mystore');
	promise.then(() => {
    	console.info("Succeeded in removing preferences.");
D
duanweiling 已提交
443
    }).catch((err) => {
D
duanweiling 已提交
444
        console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
445 446
    })
} catch(err) {
D
duanweiling 已提交
447
    console.info("Failed to remove preferences. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
448
}
449
```
P
PaDoBoo 已提交
450 451 452

## Preferences

453 454 455
存储实例,提供获取和修改存储数据的接口。

下列接口都需先使用[data_preferences.getPreferences](#data_preferencesgetpreferences)获取到Preferences实例,再通过此实例调用对应接口。
P
PaDoBoo 已提交
456 457 458 459 460 461


### get

get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void

462
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,使用callback异步回调。
P
PaDoBoo 已提交
463

P
PaDaBoo 已提交
464
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
465

G
ge-yafang 已提交
466
**参数:**
467

468 469 470 471 472
| 参数名   | 类型                                         | 必填 | 说明                                                         |
| -------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                                       | 是   | 要获取的存储Key名称,不能为空。                              |
| defValue | [ValueType](#valuetype)                      | 是   | 默认返回值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
| callback | AsyncCallback&lt;[ValueType](#valuetype)&gt; | 是   | 回调函数。当获取成功时,err为undefined,data为键对应的值;否则err为错误码。 |
P
PaDoBoo 已提交
473

G
ge-yafang 已提交
474
**示例:**
475 476

```js
D
duanweiling 已提交
477
try {
478
    preferences.get('startup', 'default', function (err, val) {
D
duanweiling 已提交
479
        if (err) {
D
duanweiling 已提交
480 481
            console.info("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
482
        }
D
duanweiling 已提交
483
        console.info("Succeeded in getting value of 'startup'. val: " + val);
D
duanweiling 已提交
484 485
    })
} catch (err) {
D
duanweiling 已提交
486
    console.info("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
487
}
P
PaDaBoo 已提交
488
```
P
PaDoBoo 已提交
489 490 491 492 493 494


### get

get(key: string, defValue: ValueType): Promise&lt;ValueType&gt;

495
获取键对应的值,如果值为null或者非默认值类型,返回默认数据,使用Promise异步回调。
P
PaDoBoo 已提交
496

P
PaDaBoo 已提交
497
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
498

499
 **参数:**
500
 
501 502 503 504
| 参数名   | 类型                    | 必填 | 说明                                                         |
| -------- | ----------------------- | ---- | ------------------------------------------------------------ |
| key      | string                  | 是   | 要获取的存储Key名称,不能为空。                              |
| defValue | [ValueType](#valuetype) | 是   | 默认返回值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
P
PaDoBoo 已提交
505

G
ge-yafang 已提交
506
**返回值:**
507 508 509 510

| 类型                                | 说明                          |
| ----------------------------------- | ----------------------------- |
| Promise<[ValueType](#valuetype)&gt; | Promise对象,返回键对应的值。 |
P
PaDoBoo 已提交
511

G
ge-yafang 已提交
512
**示例:**
513

514
```js
D
duanweiling 已提交
515
try {
D
duanweiling 已提交
516 517 518
    let promise = preferences.get('startup', 'default');
    promise.then((data) => {
        console.info("Succeeded in getting value of 'startup'. Data: " + data);
D
duanweiling 已提交
519
    }).catch((err) => {
D
duanweiling 已提交
520
        console.info("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
521 522
    })
} catch(err) {
D
duanweiling 已提交
523
    console.info("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
524
}
P
PaDaBoo 已提交
525
```
P
PaDoBoo 已提交
526

527 528 529 530
### getAll

getAll(callback: AsyncCallback&lt;Object&gt;): void;

531
获取含有所有键值的Object对象。
532 533 534 535

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**参数:**
536

537 538 539
| 参数名   | 类型                        | 必填 | 说明                                                         |
| -------- | --------------------------- | ---- | ------------------------------------------------------------ |
| callback | AsyncCallback&lt;Object&gt; | 是   | 回调函数。当获取成功,err为undefined,value为含有所有键值的Object对象;否则err为错误码。 |
540 541

**示例:**
542 543

```js
D
duanweiling 已提交
544
try {
D
duanweiling 已提交
545
    preferences.getAll(function (err, value) {
D
duanweiling 已提交
546
        if (err) {
D
duanweiling 已提交
547 548
            console.info("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
549
        }
D
duanweiling 已提交
550 551 552
    let allKeys = Object.keys(value);
    console.info("getAll keys = " + allKeys);
    console.info("getAll object = " + JSON.stringify(value));
D
duanweiling 已提交
553 554
    })
} catch (err) {
D
duanweiling 已提交
555
    console.info("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
556
}
557 558 559 560 561 562 563
```


### getAll

getAll(): Promise&lt;Object&gt;

564
获取含有所有键值的Object对象。
565 566 567 568

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**返回值:**
569

570 571 572
| 类型                  | 说明                                        |
| --------------------- | ------------------------------------------- |
| Promise&lt;Object&gt; | Promise对象,返回含有所有键值的Object对象。 |
573 574

**示例:**
575

576
```js
D
duanweiling 已提交
577
try {
D
duanweiling 已提交
578 579
    let promise = preferences.getAll();
    promise.then((value) => {
D
duanweiling 已提交
580 581 582
        let allKeys = Object.keys(value);
        console.info('getAll keys = ' + allKeys);
        console.info("getAll object = " + JSON.stringify(value));
D
duanweiling 已提交
583
    }).catch((err) => {
D
duanweiling 已提交
584
        console.info("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
585 586
    })
} catch (err) {
D
duanweiling 已提交
587
    console.info("Failed to get all key-values. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
588
}
589
```
P
PaDoBoo 已提交
590 591 592 593 594

### put

put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void

595
将数据写入Preferences实例,可通过[flush](#flush)将Preferences实例持久化,使用callback异步回调。
P
PaDoBoo 已提交
596

P
PaDaBoo 已提交
597
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
598

G
ge-yafang 已提交
599
**参数:**
600

601 602 603 604 605
| 参数名   | 类型                      | 必填 | 说明                                                         |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                    | 是   | 要修改的存储的Key,不能为空。                                |
| value    | [ValueType](#valuetype)   | 是   | 存储的新值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当数据写入成功,err为undefined;否则为错误码。     |
P
PaDoBoo 已提交
606

G
ge-yafang 已提交
607
**示例:**
608

609
```js
D
duanweiling 已提交
610
try {
D
duanweiling 已提交
611
    preferences.put('startup', 'auto', function (err) {
D
duanweiling 已提交
612
        if (err) {
D
duanweiling 已提交
613
            console.info("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
614
            return;
D
duanweiling 已提交
615
        }
D
duanweiling 已提交
616
        console.info("Succeeded in putting value of 'startup'.");
D
duanweiling 已提交
617
    })
D
duanweiling 已提交
618
} catch (err) {
D
duanweiling 已提交
619
    console.info("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
620
}
P
PaDaBoo 已提交
621
```
P
PaDoBoo 已提交
622 623 624 625 626 627


### put

put(key: string, value: ValueType): Promise&lt;void&gt;

628
将数据写入Preferences实例,可通过[flush](#flush)将Preferences实例持久化,使用Promise异步回调。
P
PaDoBoo 已提交
629

P
PaDaBoo 已提交
630
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
631

G
ge-yafang 已提交
632
**参数:**
633

634 635 636 637
| 参数名 | 类型                    | 必填 | 说明                                                         |
| ------ | ----------------------- | ---- | ------------------------------------------------------------ |
| key    | string                  | 是   | 要修改的存储的Key,不能为空。                                |
| value  | [ValueType](#valuetype) | 是   | 存储的新值。支持number、string、boolean、Array\<number>、Array\<string>、Array\<boolean>类型。 |
P
PaDoBoo 已提交
638

G
ge-yafang 已提交
639
**返回值:**
640

641 642 643
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
644

G
ge-yafang 已提交
645
**示例:**
646

647
```js
D
duanweiling 已提交
648
try {
D
duanweiling 已提交
649
    let promise = preferences.put('startup', 'auto');
D
duanweiling 已提交
650
    promise.then(() => {
D
duanweiling 已提交
651
        console.info("Succeeded in putting value of 'startup'.");
D
duanweiling 已提交
652
    }).catch((err) => {
D
duanweiling 已提交
653
        console.info("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message);
D
duanweiling 已提交
654 655
    })
} catch(err) {
D
duanweiling 已提交
656
    console.info("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message);
D
duanweiling 已提交
657
}
P
PaDaBoo 已提交
658
```
P
PaDoBoo 已提交
659 660 661 662


### has

663
has(key: string, callback: AsyncCallback&lt;boolean&gt;): void
P
PaDoBoo 已提交
664

665
检查Preferences实例是否包含名为给定Key的存储键值对,使用callback异步回调。
P
PaDoBoo 已提交
666

P
PaDaBoo 已提交
667
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
668

G
ge-yafang 已提交
669
**参数:**
670

671 672 673
| 参数名   | 类型                         | 必填 | 说明                                                         |
| -------- | ---------------------------- | ---- | ------------------------------------------------------------ |
| key      | string                       | 是   | 要检查的存储key名称,不能为空。                              |
674
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调函数。返回Preferences实例是否包含给定key的存储键值对,true表示存在,false表示不存在。 |
P
PaDoBoo 已提交
675

G
ge-yafang 已提交
676
**示例:**
677

678
```js
D
duanweiling 已提交
679
try {
D
duanweiling 已提交
680
    preferences.has('startup', function (err, val) {
D
duanweiling 已提交
681 682 683 684 685 686 687 688 689
        if (err) {
            console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
            return;
        }
        if (val) {
            console.info("The key 'startup' is contained.");
        } else {
            console.info("The key 'startup' dose not contain.");
        }
D
duanweiling 已提交
690
  })
D
duanweiling 已提交
691 692
} catch (err) {
    console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
693
}
P
PaDaBoo 已提交
694
```
P
PaDoBoo 已提交
695 696 697 698 699 700


### has

has(key: string): Promise&lt;boolean&gt;

701
检查Preferences实例是否包含名为给定Key的存储键值对,使用Promise异步回调。
P
PaDoBoo 已提交
702

P
PaDaBoo 已提交
703
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
704

G
ge-yafang 已提交
705
**参数:**
706

707 708 709
| 参数名 | 类型   | 必填 | 说明                            |
| ------ | ------ | ---- | ------------------------------- |
| key    | string | 是   | 要检查的存储key名称,不能为空。 |
P
PaDoBoo 已提交
710

G
ge-yafang 已提交
711
**返回值:**
712

713 714
| 类型                   | 说明                                                         |
| ---------------------- | ------------------------------------------------------------ |
715
| Promise&lt;boolean&gt; | Promise对象。返回Preferences实例是否包含给定key的存储键值对,true表示存在,false表示不存在。 |
P
PaDoBoo 已提交
716

G
ge-yafang 已提交
717
**示例:**
718 719

```js
D
duanweiling 已提交
720
try {
D
duanweiling 已提交
721 722
    let promise = preferences.has('startup');
    promise.then((val) => {
D
duanweiling 已提交
723
        if (val) {
D
duanweiling 已提交
724
            console.info("The key 'startup' is contained.");
D
duanweiling 已提交
725
        } else {
D
duanweiling 已提交
726
            console.info("The key 'startup' dose not contain.");
D
duanweiling 已提交
727
        }
D
duanweiling 已提交
728
    }).catch((err) => {
D
duanweiling 已提交
729
        console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
730 731
  })
} catch(err) {
D
duanweiling 已提交
732
    console.info("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
733
}
P
PaDaBoo 已提交
734
```
P
PaDoBoo 已提交
735 736 737 738 739 740


### delete

delete(key: string, callback: AsyncCallback&lt;void&gt;): void

741
从Preferences实例中删除名为给定Key的存储键值对,使用callback异步回调。
P
PaDoBoo 已提交
742

P
PaDaBoo 已提交
743
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
744

G
ge-yafang 已提交
745
**参数:**
746

747 748 749 750
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| key      | string                    | 是   | 要删除的存储Key名称,不能为空。                      |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当删除成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
751

G
ge-yafang 已提交
752
**示例:**
753

754
```js
D
duanweiling 已提交
755
try {
D
duanweiling 已提交
756
    preferences.delete('startup', function (err) {
D
duanweiling 已提交
757
        if (err) {
D
duanweiling 已提交
758 759
            console.info("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
760
        }
D
duanweiling 已提交
761
        console.info("Succeeded in deleting the key 'startup'.");
D
duanweiling 已提交
762
    })
D
duanweiling 已提交
763 764
} catch (err) {
    console.info("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
765
}
P
PaDaBoo 已提交
766
```
P
PaDoBoo 已提交
767 768 769 770 771 772


### delete

delete(key: string): Promise&lt;void&gt;

773
从Preferences实例中删除名为给定Key的存储键值对,使用Promise异步回调。
P
PaDoBoo 已提交
774

P
PaDaBoo 已提交
775
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
776

G
ge-yafang 已提交
777
**参数:**
778

779 780 781
| 参数名 | 类型   | 必填 | 说明                            |
| ------ | ------ | ---- | ------------------------------- |
| key    | string | 是   | 要删除的存储key名称,不能为空。 |
P
PaDoBoo 已提交
782

G
ge-yafang 已提交
783
**返回值:**
784

785 786 787
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
788

G
ge-yafang 已提交
789
**示例:**
790

791
```js
D
duanweiling 已提交
792
try {
D
duanweiling 已提交
793
    let promise = preferences.delete('startup');
D
duanweiling 已提交
794
	promise.then(() => {
D
duanweiling 已提交
795
        console.info("Succeeded in deleting the key 'startup'.");
D
duanweiling 已提交
796
    }).catch((err) => {
D
duanweiling 已提交
797
        console.log("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message);
D
duanweiling 已提交
798 799
    })
} catch(err) {
D
duanweiling 已提交
800
    console.log("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message);
D
duanweiling 已提交
801
}
P
PaDaBoo 已提交
802
```
P
PaDoBoo 已提交
803 804 805 806 807 808


### flush

flush(callback: AsyncCallback&lt;void&gt;): void

809
将当前Preferences实例的数据异步存储到首选项持久化文件中,使用callback异步回调。
P
PaDoBoo 已提交
810

P
PaDaBoo 已提交
811
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
812

G
ge-yafang 已提交
813
**参数:**
814

815 816 817
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当保存成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
818

G
ge-yafang 已提交
819
**示例:**
820

821
```js
D
duanweiling 已提交
822
try {
D
duanweiling 已提交
823
    preferences.flush(function (err) {
D
duanweiling 已提交
824
        if (err) {
D
duanweiling 已提交
825 826
            console.info("Failed to flush. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
827
        }
D
duanweiling 已提交
828
        console.info("Succeeded in flushing.");
D
duanweiling 已提交
829
    })
D
duanweiling 已提交
830 831
} catch (err) {
    console.info("Failed to flush. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
832
}
P
PaDaBoo 已提交
833
```
P
PaDoBoo 已提交
834 835 836 837 838 839


### flush

flush(): Promise&lt;void&gt;

840
将当前Preferences实例的数据异步存储到首选项持久化文件中,使用Promise异步回调。
P
PaDoBoo 已提交
841

P
PaDaBoo 已提交
842
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
843

G
ge-yafang 已提交
844
**返回值:**
845

846 847 848
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
849

G
ge-yafang 已提交
850
**示例:**
851

852
```js
D
duanweiling 已提交
853
try {
D
duanweiling 已提交
854
    let promise = preferences.flush();
D
duanweiling 已提交
855
    promise.then(() => {
D
duanweiling 已提交
856
        console.info("Succeeded in flushing.");
D
duanweiling 已提交
857
    }).catch((err) => {
D
duanweiling 已提交
858
        console.info("Failed to flush. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
859 860
    })
} catch (err) {
D
duanweiling 已提交
861
    console.info("Failed to flush. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
862
}
P
PaDaBoo 已提交
863
```
P
PaDoBoo 已提交
864 865 866 867 868 869


### clear

clear(callback: AsyncCallback&lt;void&gt;): void

870
清除此Preferences实例中的所有存储,使用callback异步回调。
P
PaDoBoo 已提交
871

P
PaDaBoo 已提交
872
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
873

G
ge-yafang 已提交
874
**参数:**
875

876 877 878
| 参数名   | 类型                      | 必填 | 说明                                                 |
| -------- | ------------------------- | ---- | ---------------------------------------------------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调函数。当清除成功,err为undefined;否则为错误码。 |
P
PaDoBoo 已提交
879

G
ge-yafang 已提交
880
**示例:**
881

882
```js
D
duanweiling 已提交
883 884 885
try {
	preferences.clear(function (err) {
        if (err) {
D
duanweiling 已提交
886 887
            console.info("Failed to clear. code =" + err.code + ", message =" + err.message);
            return;
D
duanweiling 已提交
888
        }
D
duanweiling 已提交
889
        console.info("Succeeded in clearing.");
D
duanweiling 已提交
890
    })
D
duanweiling 已提交
891
} catch (err) {
D
duanweiling 已提交
892
    console.info("Failed to clear. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
893
}
P
PaDaBoo 已提交
894
```
P
PaDoBoo 已提交
895 896 897 898 899 900


### clear

clear(): Promise&lt;void&gt;

901
清除此Preferences实例中的所有存储,使用Promise异步回调。
P
PaDoBoo 已提交
902

P
PaDaBoo 已提交
903
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
904

G
ge-yafang 已提交
905
**返回值:**
906

907 908 909
| 类型                | 说明                      |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
P
PaDoBoo 已提交
910

G
ge-yafang 已提交
911
**示例:**
912

913
```js
D
duanweiling 已提交
914 915
try {
    let promise = preferences.clear();
D
duanweiling 已提交
916 917
	promise.then(() => {
    	console.info("Succeeded in clearing.");
D
duanweiling 已提交
918
    }).catch((err) => {
D
duanweiling 已提交
919
        console.info("Failed to clear. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
920 921
    })
} catch(err) {
D
duanweiling 已提交
922
    console.info("Failed to clear. code =" + err.code + ", message =" + err.message);
D
duanweiling 已提交
923
}
P
PaDaBoo 已提交
924
```
P
PaDoBoo 已提交
925 926 927 928 929 930


### on('change')

on(type: 'change', callback: Callback&lt;{ key : string }&gt;): void

931
订阅数据变更,订阅的Key的值发生变更后,在执行[flush](#flush)方法后,触发callback回调。
P
PaDoBoo 已提交
932

P
PaDaBoo 已提交
933
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
934

G
ge-yafang 已提交
935
**参数:**
936

937 938 939 940
| 参数名   | 类型                             | 必填 | 说明                                     |
| -------- | -------------------------------- | ---- | ---------------------------------------- |
| type     | string                           | 是   | 事件类型,固定值'change',表示数据变更。 |
| callback | Callback&lt;{ key : string }&gt; | 是   | 回调对象实例。                           |
P
PaDoBoo 已提交
941

G
ge-yafang 已提交
942
**示例:**
943

944
```js
D
duanweiling 已提交
945
try {
D
duanweiling 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
	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;
				}
D
duanweiling 已提交
967
				console.info("Succeeded in flushing.");
D
duanweiling 已提交
968 969 970 971 972 973
			})
		})
	})
} catch (err) {
	console.info("Failed to flush. code =" + err.code + ", message =" + err.message);
}
P
PaDaBoo 已提交
974
```
P
PaDoBoo 已提交
975 976 977 978


### off('change')

979
off(type: 'change', callback?: Callback&lt;{ key : string }&gt;): void
P
PaDoBoo 已提交
980

981
取消订阅数据变更。
P
PaDoBoo 已提交
982

P
PaDaBoo 已提交
983
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
984

G
ge-yafang 已提交
985
**参数:**
986

987 988 989 990
| 参数名   | 类型                             | 必填 | 说明                                       |
| -------- | -------------------------------- | ---- | ------------------------------------------ |
| type     | string                           | 是   | 事件类型,固定值'change',表示数据变更。   |
| callback | Callback&lt;{ key : string }&gt; | 否   | 需要取消的回调对象实例,不填写则全部取消。 |
P
PaDoBoo 已提交
991

G
ge-yafang 已提交
992
**示例:**
993

994
```js
D
duanweiling 已提交
995
try {
D
duanweiling 已提交
996
    data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) {
D
duanweiling 已提交
997
        if (err) {
D
duanweiling 已提交
998
            console.info("Failed to get preferences.");
D
duanweiling 已提交
999 1000
            return;
        }
D
duanweiling 已提交
1001 1002 1003 1004 1005
        let observer = function (key) {
            console.info("The key " + key + " changed.");
        }
        preferences.on('change', observer);
        preferences.put('startup', 'auto', function (err) {
D
duanweiling 已提交
1006
            if (err) {
D
duanweiling 已提交
1007
                console.info("Failed to put the value of 'startup'. Cause: " + err);
D
duanweiling 已提交
1008 1009
                return;
            }
D
duanweiling 已提交
1010 1011 1012 1013 1014 1015 1016
            console.info("Succeeded in putting the value of 'startup'.");

            preferences.flush(function (err) {
                if (err) {
                    console.info("Failed to flush. Cause: " + err);
                    return;
                }
D
duanweiling 已提交
1017
                console.info("Succeeded in flushing.");
D
duanweiling 已提交
1018 1019
            })
            preferences.off('change', observer);
D
duanweiling 已提交
1020 1021
        })
    })
D
duanweiling 已提交
1022
} catch (err) {
D
duanweiling 已提交
1023 1024
    console.info("Failed to flush. code =" + err.code + ", message =" + err.message);
}
P
PaDaBoo 已提交
1025
```
G
ge-yafang 已提交
1026 1027 1028 1029 1030

## ValueType

用于表示允许的数据字段类型。

P
PaDaBoo 已提交
1031
**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core
G
ge-yafang 已提交
1032

1033 1034 1035 1036 1037
| 类型            | 说明                           |
| --------------- | ------------------------------ |
| number          | 表示值类型为数字。             |
| string          | 表示值类型为字符串。           |
| boolean         | 表示值类型为布尔值。           |
1038 1039
| Array\<number>  | 表示值类型为数字类型的数组。   |
| Array\<boolean> | 表示值类型为布尔类型的数组。   |
1040
| Array\<string>  | 表示值类型为字符串类型的数组。 |