diff --git a/zh-cn/application-dev/database/data-persistence-by-preferences.md b/zh-cn/application-dev/database/data-persistence-by-preferences.md index 8385bd06cfc8326bccb04c79e430018b2ff56730..c5a6f64a0c47e540aa14b3ca85f04d106db1ef7e 100644 --- a/zh-cn/application-dev/database/data-persistence-by-preferences.md +++ b/zh-cn/application-dev/database/data-persistence-by-preferences.md @@ -58,11 +58,13 @@ ```js import UIAbility from '@ohos.app.ability.UIAbility'; + import { BusinessError } from '@ohos.base'; + import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - dataPreferences.getPreferences(this.context, 'mystore', (err, preferences) => { + dataPreferences.getPreferences(this.context, 'myStore', (err: BusinessError, preferences: dataPreferences.Preferences) => { if (err) { console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`); return; @@ -82,12 +84,13 @@ ```js import featureAbility from '@ohos.ability.featureAbility'; + import { BusinessError } from '@ohos.base'; // 获å–context let context = featureAbility.getContext(); try { - dataPreferences.getPreferences(context, 'mystore', (err, preferences) => { + dataPreferences.getPreferences(this.context, 'myStore', (err: BusinessError, preferences: dataPreferences.Preferences) => { if (err) { console.error(`Failed to get preferences. Code:${err.code},message:${err.message}`); return; @@ -157,7 +160,7 @@ ```js try { - preferences.flush((err) => { + preferences.flush((err: BusinessError) => { if (err) { console.error(`Failed to flush. Code:${err.code}, message:${err.message}`); return; @@ -174,18 +177,20 @@ 应用订阅数æ®å˜æ›´éœ€è¦æŒ‡å®šobserver作为回调方法。订阅的Key值å‘生å˜æ›´åŽï¼Œå½“执行flush()方法时,observer被触å‘回调。示例代ç 如下所示: ```js - let observer = function (key) { - console.info('The key' + key + 'changed.'); + interface observer { + key: string } - preferences.on('change', observer); + preferences.on('change', (key: observer) => { + console.info('The key' + key + 'changed.'); + }); // æ•°æ®äº§ç”Ÿå˜æ›´ï¼Œç”±'auto'å˜ä¸º'manual' - preferences.put('startup', 'manual', (err) => { + preferences.put('startup', 'manual', (err: BusinessError) => { if (err) { console.error(`Failed to put the value of 'startup'. Code:${err.code},message:${err.message}`); return; } console.info("Succeeded in putting the value of 'startup'."); - preferences.flush((err) => { + preferences.flush((err: BusinessError) => { if (err) { console.error(`Failed to flush. Code:${err.code}, message:${err.message}`); return; @@ -210,7 +215,7 @@ ```js try { - dataPreferences.deletePreferences(this.context, 'mystore', (err, val) => { + dataPreferences.deletePreferences(this.context, 'myStore', (err: BusinessError) => { if (err) { console.error(`Failed to delete preferences. Code:${err.code}, message:${err.message}`); return; diff --git a/zh-cn/application-dev/database/data-sync-of-distributed-data-object.md b/zh-cn/application-dev/database/data-sync-of-distributed-data-object.md index 4dd9be9bcc82e00fef53b5ad901e2582ccc18af7..8f3cca0be0fac2dc1ec8bb1e1d7738309c914d68 100644 --- a/zh-cn/application-dev/database/data-sync-of-distributed-data-object.md +++ b/zh-cn/application-dev/database/data-sync-of-distributed-data-object.md @@ -151,17 +151,26 @@ // å¯¼å…¥æ¨¡å— import distributedDataObject from '@ohos.data.distributedDataObject'; import UIAbility from '@ohos.app.ability.UIAbility'; - + import { BusinessError } from '@ohos.base'; + import window from '@ohos.window'; + + interface sourceObject{ + name: string, + age: number, + isVis: boolean + parent: { [key: string]: string }, + list: { [key: string]: string }[] + } class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { - // 创建对象,该对象包å«4个属性类型:stringã€numberã€booleanå’ŒObject - let localObject = distributedDataObject.create(this.context, { + onWindowStageCreate(windowStage: window.WindowStage) { + let source: sourceObject = { name: 'jack', age: 18, isVis: false, parent: { mother: 'jack mom', father: 'jack Dad' }, list: [{ mother: 'jack mom' }, { father: 'jack Dad' }] - }); + } + let localObject: distributedDataObject.DataObject = distributedDataObject.create(this.context, source); } } ``` @@ -175,34 +184,43 @@ import featureAbility from '@ohos.ability.featureAbility'; // 获å–context let context = featureAbility.getContext(); - // 创建对象,该对象包å«4个属性类型:stringã€numberã€booleanå’ŒObject - let localObject = distributedDataObject.create(context, { + interface sourceObject{ + name: string, + age: number, + isVis: boolean + parent: { [key: string]: string }, + list: { [key: string]: string }[] + } + let source: sourceObject = { name: 'jack', age: 18, isVis: false, parent: { mother: 'jack mom', father: 'jack Dad' }, list: [{ mother: 'jack mom' }, { father: 'jack Dad' }] - }); + } + // 创建对象,该对象包å«4个属性类型:stringã€numberã€booleanå’ŒObject + let localObject: distributedDataObject.DataObject = distributedDataObject.create(context, source); ``` 4. åŠ å…¥åŒæ¥ç»„网。åŒæ¥ç»„网ä¸çš„æ•°æ®å¯¹è±¡åˆ†ä¸ºå‘起方和被拉起方。 ```js // 设备1åŠ å…¥sessionId - let sessionId = '123456'; + let sessionId: string = '123456'; localObject.setSessionId(sessionId); // 和设备1ååŒçš„设备2åŠ å…¥åŒä¸€ä¸ªsession // 创建对象,该对象包å«4个属性类型:stringã€numberã€booleanå’ŒObject - let remoteObject = distributedDataObject.create(this.context, { + let remoteSource: sourceObject = { name: undefined, age: undefined, // undefined表示数æ®æ¥è‡ªå¯¹ç«¯ isVis: true, parent: undefined, list: undefined - }); + } + let remoteObject: distributedDataObject.DataObject = distributedDataObject.create(this.context, remoteSource); // 收到status上线åŽremoteObjectåŒæ¥æ•°æ®ï¼Œå³nameå˜æˆjack,age是18 remoteObject.setSessionId(sessionId); ``` @@ -210,18 +228,19 @@ 5. 监å¬å¯¹è±¡æ•°æ®å˜æ›´ã€‚å¯ç›‘å¬å¯¹ç«¯æ•°æ®çš„å˜æ›´ï¼Œä»¥callback作为å˜æ›´å›žè°ƒå®žä¾‹ã€‚ ```js - function changeCallback(sessionId, changeData) { - console.info(`change: ${sessionId}`); - - if (changeData !== null && changeData !== undefined) { - changeData.forEach(element => { - console.info(`The element ${localObject[element]} changed.`); - }); - } + interface ChangeCallback { + sessionId: string, + fields: Array<string> } - // å‘èµ·æ–¹è¦åœ¨changeCallback里刷新界é¢ï¼Œåˆ™éœ€è¦å°†æ£ç¡®çš„this绑定给changeCallback - localObject.on("change", this.changeCallback.bind(this)); + localObject.on("change", (changeData:ChangeCallback) => { + console.info("change" + changeData.sessionId); + if (changeData.fields != null && changeData.fields != undefined) { + for (let index: number = 0; index < changeData.fields.length; index++) { + console.info(`The element ${localObject[changeData.fields[index]]} changed.`); + } + } + }); ``` 6. 修改对象属性,对象属性支æŒåŸºæœ¬ç±»åž‹ï¼ˆæ•°å—类型ã€å¸ƒå°”类型ã€å—符串类型)以åŠå¤æ‚类型(数组ã€åŸºæœ¬ç±»åž‹åµŒå¥—ç‰ï¼‰ã€‚ @@ -256,7 +275,14 @@ ```js // åˆ é™¤å˜æ›´å›žè°ƒchangeCallback - localObject.off('change', this.changeCallback); + localObject.off('change',(changeData: ChangeCallback) => { + console.info("change" + changeData.sessionId); + if (changeData.fields != null && changeData.fields != undefined) { + for (let index: number = 0; index < changeData.fields.length; index++) { + console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]); + } + } + }); // åˆ é™¤æ‰€æœ‰çš„å˜æ›´å›žè°ƒ localObject.off('change'); ``` @@ -264,27 +290,32 @@ 9. 监å¬åˆ†å¸ƒå¼æ•°æ®å¯¹è±¡çš„上下线。å¯ä»¥ç›‘å¬å¯¹ç«¯åˆ†å¸ƒå¼æ•°æ®å¯¹è±¡çš„上下线。 ```js - function statusCallback(sessionId, networkId, status) { - // ä¸šåŠ¡å¤„ç† + interface onStatusCallback { + sessionId: string, + networkId: string, + status: 'online' | 'offline' } - localObject.on('status', this.statusCallback); + localObject.on('status', (statusCallback: onStatusCallback) => { + console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId); + // ä¸šåŠ¡å¤„ç† + }); ``` 10. ä¿å˜å’Œæ’¤å›žå·²ä¿å˜çš„æ•°æ®å¯¹è±¡ã€‚ ```js // ä¿å˜æ•°æ®å¯¹è±¡ï¼Œå¦‚果应用退出åŽç»„网内设备需è¦æ¢å¤å¯¹è±¡æ•°æ®æ—¶è°ƒç”¨ - localObject.save('local').then((result) => { + localObject.save("local").then((result: distributedDataObject.SaveSuccessResponse) => { console.info(`Succeeded in saving. SessionId:${result.sessionId},version:${result.version},deviceId:${result.deviceId}`); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error(`Failed to save. Code:${err.code},message:${err.message}`); }); // 撤回ä¿å˜çš„æ•°æ®å¯¹è±¡ - localObject.revokeSave().then((result) => { + localObject.revokeSave().then((result: distributedDataObject.RevokeSaveSuccessResponse) => { console.info(`Succeeded in revokeSaving. Session:${result.sessionId}`); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error(`Failed to revokeSave. Code:${err.code},message:${err.message}`); }); ``` @@ -292,8 +323,16 @@ 11. åˆ é™¤ç›‘å¬åˆ†å¸ƒå¼æ•°æ®å¯¹è±¡çš„上下线。å¯ä»¥æŒ‡å®šåˆ 除监å¬çš„上下线回调;也å¯ä»¥ä¸æŒ‡å®šï¼Œè¿™å°†ä¼šåˆ 除该分布å¼æ•°æ®å¯¹è±¡çš„所有上下线回调。 ```js + interface offStatusCallback { + sessionId: string, + deviceId: string, + status: 'online' | 'offline' + } // åˆ é™¤ä¸Šä¸‹çº¿å›žè°ƒstatusCallback - localObject.off('status', this.statusCallback); + localObject.off('status', (statusCallback: offStatusCallback) => { + console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.deviceId); + // ä¸šåŠ¡å¤„ç† + }); // åˆ é™¤æ‰€æœ‰çš„ä¸Šä¸‹çº¿å›žè°ƒ localObject.off('status'); ``` @@ -302,7 +341,7 @@ ```js localObject.setSessionId(() => { - console.info('leave all lession.'); + console.info('leave all session.'); }); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md b/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md index 26a3254206949582ceb3d71d253d2f14bc9e1b8c..4cc5b90e71aea7bb82123ba6b47ff0648ff2e756 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-distributedobject.md @@ -42,10 +42,20 @@ FA模型示例: // å¯¼å…¥æ¨¡å— import distributedObject from '@ohos.data.distributedDataObject'; import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; // 获å–context let context = featureAbility.getContext(); -// 创建对象,该对象包å«4个属性类型:stringã€numberã€booleanå’ŒObject -let g_object = distributedObject.create(context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +interface sourceObject{ + name: string, + age: number, + isVis: boolean +} +let source: sourceObject = { + name: "amy", + age:18, + isVis:false +} +let g_object: distributedObject.DataObject = distributedObject.create(context, source); ``` Stage模型示例: @@ -54,13 +64,23 @@ Stage模型示例: // å¯¼å…¥æ¨¡å— import distributedObject from '@ohos.data.distributedDataObject'; import UIAbility from '@ohos.app.ability.UIAbility'; - -let g_object = null; - +import { BusinessError } from '@ohos.base'; +import window from '@ohos.window'; + +let g_object: distributedObject.DataObject = null; +interface sourceObject{ + name: string, + age: number, + isVis: boolean +} class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage){ - // 创建对象,该对象包å«4个属性类型:stringã€numberã€booleanå’ŒObject - g_object = distributedObject.create(this.context, {name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); + onWindowStageCreate(windowStage: window.WindowStage) { + let source: sourceObject = { + name: "amy", + age:18, + isVis:false + } + g_object = distributedObject.create(this.context, source); } } ``` @@ -83,7 +103,7 @@ genSessionId(): string ```js import distributedObject from '@ohos.data.distributedDataObject'; -let sessionId = distributedObject.genSessionId(); +let sessionId: string = distributedObject.genSessionId(); ``` ## SaveSuccessResponse<sup>9+</sup> @@ -179,7 +199,7 @@ g_object.setSessionId(distributedObject.genSessionId(), ()=>{ }); // 退出分布å¼ç»„网 g_object.setSessionId(() => { - console.info("leave all lession."); + console.info("leave all session."); }); ``` @@ -219,13 +239,13 @@ setSessionId(sessionId?: string): Promise<void> // g_objectåŠ å…¥åˆ†å¸ƒå¼ç»„网 g_object.setSessionId(distributedObject.genSessionId()).then (()=>{ console.info("join session."); - }).catch((error)=>{ + }).catch((error: BusinessError)=>{ console.info("error:" + error.code + error.message); }); // 退出分布å¼ç»„网 g_object.setSessionId().then (()=>{ - console.info("leave all lession."); - }).catch((error)=>{ + console.info("leave all session."); + }).catch((error: BusinessError)=>{ console.info("error:" + error.code + error.message); }); ``` @@ -248,15 +268,18 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array<stri **示例:** ```js -globalThis.changeCallback = (sessionId, changeData) => { - console.info("change" + sessionId); - if (changeData != null && changeData != undefined) { - changeData.forEach(element => { - console.info("changed !" + element + " " + g_object[element]); - }); - } +interface ChangeCallback { + sessionId: string, + fields: Array<string> } -g_object.on("change", globalThis.changeCallback); +g_object.on("change", (changeData: ChangeCallback) => { + console.info("change" + changeData.sessionId); + if (changeData.fields != null && changeData.fields != undefined) { + for (let index: number = 0; index < changeData.fields.length; index++) { + console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]); + } + } +}); ``` ### off('change')<sup>9+</sup> @@ -279,7 +302,14 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array<st ```js // åˆ é™¤æ•°æ®å˜æ›´å›žè°ƒchangeCallback -g_object.off("change", globalThis.changeCallback); +g_object.off("change", (changeData:ChangeCallback) => { + console.info("change" + changeData.sessionId); + if (changeData.fields != null && changeData.fields != undefined) { + for (let index: number = 0; index < changeData.fields.length; index++) { + console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]); + } + } +}); // åˆ é™¤æ‰€æœ‰çš„æ•°æ®å˜æ›´å›žè°ƒ g_object.off("change"); ``` @@ -302,10 +332,15 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st **示例:** ```js -globalThis.statusCallback = (sessionId, networkId, status) => { - globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; +interface onStatusCallback { + sessionId: string, + networkId: string, + status: 'online' | 'offline' } -g_object.on("status", globalThis.statusCallback); + +g_object.on("status", (statusCallback:onStatusCallback) => { + console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId); +}); ``` ### off('status')<sup>9+</sup> @@ -327,11 +362,15 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s **示例:** ```js -globalThis.statusCallback = (sessionId, networkId, status) => { - globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; +interface offStatusCallback { + sessionId: string, + networkId: string, + status: 'online' | 'offline' } // åˆ é™¤ä¸Šä¸‹çº¿å›žè°ƒchangeCallback -g_object.off("status",globalThis.statusCallback); +g_object.off("status", (statusCallback:StatusCallback) => { + console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId); +}); // åˆ é™¤æ‰€æœ‰çš„ä¸Šä¸‹çº¿å›žè°ƒ g_object.off("status"); ``` @@ -363,7 +402,7 @@ save(deviceId: string, callback: AsyncCallback<SaveSuccessResponse>): void ```ts g_object.setSessionId("123456"); -g_object.save("local", (err, result) => { +g_object.save("local", (err: BusinessError, result:distributedObject.SaveSuccessResponse) => { if (err) { console.info("save failed, error code = " + err.code); console.info("save failed, error message: " + err.message); @@ -408,12 +447,12 @@ save(deviceId: string): Promise<SaveSuccessResponse> ```js g_object.setSessionId("123456"); -g_object.save("local").then((result) => { +g_object.save("local").then((result: distributedObject.SaveSuccessResponse) => { console.info("save callback"); console.info("save sessionId " + result.sessionId); console.info("save version " + result.version); console.info("save deviceId " + result.deviceId); -}).catch((err) => { +}).catch((err: BusinessError) => { console.info("save failed, error code = " + err.code); console.info("save failed, error message: " + err.message); }); @@ -441,7 +480,7 @@ revokeSave(callback: AsyncCallback<RevokeSaveSuccessResponse>): void ```js g_object.setSessionId("123456"); // æŒä¹…åŒ–æ•°æ® -g_object.save("local", (err, result) => { +g_object.save("local", (err: BusinessError, result: distributedObject.SaveSuccessResponse) => { if (err) { console.info("save failed, error code = " + err.code); console.info("save failed, error message: " + err.message); @@ -453,7 +492,7 @@ g_object.save("local", (err, result) => { console.info("save deviceId: " + result.deviceId); }); // åˆ é™¤æŒä¹…化ä¿å˜çš„æ•°æ® -g_object.revokeSave((err, result) => { +g_object.revokeSave((err: BusinessError, result: distributedObject.RevokeSaveSuccessResponse) => { if (err) { console.info("revokeSave failed, error code = " + err.code); console.info("revokeSave failed, error message: " + err.message); @@ -486,20 +525,20 @@ revokeSave(): Promise<RevokeSaveSuccessResponse> ```ts g_object.setSessionId("123456"); // æŒä¹…åŒ–æ•°æ® -g_object.save("local").then((result) => { +g_object.save("local").then((result: distributedObject.SaveSuccessResponse) => { console.info("save callback"); console.info("save sessionId " + result.sessionId); console.info("save version " + result.version); console.info("save deviceId " + result.deviceId); -}).catch((err) => { +}).catch((err: BusinessError) => { console.info("save failed, error code = " + err.code); console.info("save failed, error message: " + err.message); }); // åˆ é™¤æŒä¹…化ä¿å˜çš„æ•°æ® -g_object.revokeSave().then((result) => { +g_object.revokeSave().then((result: distributedObject.RevokeSaveSuccessResponse) => { console.info("revokeSave callback"); console.info("sessionId" + result.sessionId); -}).catch((err)=> { +}).catch((err: BusinessError)=> { console.info("revokeSave failed, error code = " + err.code); console.info("revokeSave failed, error message = " + err.message); }); @@ -534,8 +573,17 @@ createDistributedObject(source: object): DistributedObject ```js import distributedObject from '@ohos.data.distributedDataObject'; -// 创建对象,对象包å«4个属性类型,string,number,booleanå’ŒObject -let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +interface sourceObject{ + name: string, + age: number, + isVis: boolean +} +let source: sourceObject = { + name: "amy", + age:18, + isVis:false +} +let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); ``` ## DistributedObject<sup>(deprecated)</sup> @@ -572,7 +620,17 @@ setSessionId(sessionId?: string): boolean ```js import distributedObject from '@ohos.data.distributedDataObject'; -let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}});; +interface sourceObject{ + name: string, + age: number, + isVis: boolean +} +let source: sourceObject = { + name: "amy", + age:18, + isVis:false +} +let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); // g_objectåŠ å…¥åˆ†å¸ƒå¼ç»„网 g_object.setSessionId(distributedObject.genSessionId()); // 设置为""退出分布å¼ç»„网 @@ -602,16 +660,29 @@ on(type: 'change', callback: Callback<{ sessionId: string, fields: Array<stri ```js import distributedObject from '@ohos.data.distributedDataObject'; -let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -globalThis.changeCallback = (sessionId, changeData) => { - console.info("change" + sessionId); - if (changeData != null && changeData != undefined) { - changeData.forEach(element => { - console.info("changed !" + element + " " + g_object[element]); - }); - } +interface sourceObject{ + name: string, + age: number, + isVis: boolean } -g_object.on("change", globalThis.changeCallback); +interface ChangeCallback { + sessionId: string, + fields: Array<string> +} +let source: sourceObject = { + name: "amy", + age:18, + isVis:false +} +let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); +g_object.on("change", (changeData:ChangeCallback) => { + console.info("change" + changeData.sessionId); + if (changeData.fields != null && changeData.fields != undefined) { + for (let index: number = 0; index < changeData.fields.length; index++) { + console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]); + } + } +}); ``` ### off('change')<sup>(deprecated)</sup> @@ -638,9 +709,30 @@ off(type: 'change', callback?: Callback<{ sessionId: string, fields: Array<st ```js import distributedObject from '@ohos.data.distributedDataObject'; -let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); +interface sourceObject{ + name: string, + age: number, + isVis: boolean +} +interface ChangeCallback { + sessionId: string, + fields: Array<string> +} +let source: sourceObject = { + name: "amy", + age:18, + isVis:false +} +let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); // åˆ é™¤æ•°æ®å˜æ›´å›žè°ƒchangeCallback -g_object.off("change", globalThis.changeCallback); +g_object.off("change", (changeData:ChangeCallback) => { + console.info("change" + changeData.sessionId); + if (changeData.fields != null && changeData.fields != undefined) { + for (let index: number = 0; index < changeData.fields.length; index++) { + console.info("changed !" + changeData.fields[index] + " " + g_object[changeData.fields[index]]); + } + } +}); // åˆ é™¤æ‰€æœ‰çš„æ•°æ®å˜æ›´å›žè°ƒ g_object.off("change"); ``` @@ -668,11 +760,27 @@ on(type: 'status', callback: Callback<{ sessionId: string, networkId: string, st ```js import distributedObject from '@ohos.data.distributedDataObject'; -globalThis.statusCallback = (sessionId, networkId, status) => { - globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; + +interface sourceObject{ + name: string, + age: number, + isVis: boolean +} +interface StatusCallback { + sessionId: string, + networkId: string, + status: 'online' | 'offline' } -let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -g_object.on("status", globalThis.statusCallback); +let source: sourceObject = { + name: "amy", + age:18, + isVis:false +} +let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); + +g_object.on("status", (statusCallback:StatusCallback) => { + console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.networkId); +}); ``` ### off('status')<sup>(deprecated)</sup> @@ -699,12 +807,26 @@ off(type: 'status', callback?: Callback<{ sessionId: string, deviceId: string, s ```js import distributedObject from '@ohos.data.distributedDataObject'; -let g_object = distributedObject.createDistributedObject({name:"Amy", age:18, isVis:false, parent:{mother:"jack mom",father:"jack Dad"}}); -globalThis.statusCallback = (sessionId, networkId, status) => { - globalThis.response += "status changed " + sessionId + " " + status + " " + networkId; +interface sourceObject{ + name: string, + age: number, + isVis: boolean +} +interface offStatusCallback { + sessionId: string, + deviceId: string, + status: 'online' | 'offline' } +let source: sourceObject = { + name: "amy", + age:18, + isVis:false +} +let g_object: distributedObject.DistributedObject = distributedObject.createDistributedObject(source); // åˆ é™¤ä¸Šä¸‹çº¿å›žè°ƒchangeCallback -g_object.off("status",globalThis.statusCallback); +g_object.off("status", (statusCallback:offStatusCallback) => { + console.info("status changed " + statusCallback.sessionId + " " + statusCallback.status + " " + statusCallback.deviceId); +}); // åˆ é™¤æ‰€æœ‰çš„ä¸Šä¸‹çº¿å›žè°ƒ g_object.off("status"); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md index fc0638333c97761d9bb12700d7083a56b4d7b3fd..9d95df9da9dbdb8417e9ff2dfaac27009bc4af55 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md +++ b/zh-cn/application-dev/reference/apis/js-apis-data-preferences.md @@ -49,11 +49,13 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base'; + let context = featureAbility.getContext(); -let preferences = null; +let preferences: data_preferences.Preferences = null; try { - data_preferences.getPreferences(context, 'mystore', function (err, val) { + data_preferences.getPreferences(context, 'myStore', (err: BusinessError, val: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; @@ -70,16 +72,18 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base'; +import window from '@ohos.window'; -let preferences = null; +let preferences: data_preferences.Preferences = null; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - data_preferences.getPreferences(this.context, 'mystore', function (err, val) { + data_preferences.getPreferences(this.context, 'myStore', (err: BusinessError, val: data_preferences.Preferences) => { if (err) { - console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); - return; + console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); + return; } preferences = val; console.info("Succeeded in getting preferences."); @@ -119,15 +123,17 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); -let preferences = null; +let preferences: data_preferences.Preferences = null; try { - let promise = data_preferences.getPreferences(context, 'mystore'); - promise.then((object) => { + let promise = data_preferences.getPreferences(context, 'myStore'); + promise.then((object: data_preferences.Preferences) => { preferences = object; console.info("Succeeded in getting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -139,17 +145,19 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; -let preferences = null; +let preferences: data_preferences.Preferences = null; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - let promise = data_preferences.getPreferences(this.context, 'mystore'); - promise.then((object) => { + let promise = data_preferences.getPreferences(this.context, 'myStore'); + promise.then((object: data_preferences.Preferences) => { preferences = object; console.info("Succeeded in getting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -191,11 +199,14 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); -let preferences = null; +let preferences: data_preferences.Preferences = null; try { - data_preferences.getPreferences(context, { name: 'mystore' }, function (err, val) { + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + data_preferences.getPreferences(context, options, (err: BusinessError, val: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; @@ -213,13 +224,16 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; -let preferences = null; +let preferences: data_preferences.Preferences = null; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, val) { + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + data_preferences.getPreferences(this.context, options, (err: BusinessError, val: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; @@ -271,15 +285,17 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' let context = featureAbility.getContext(); -let preferences = null; +let preferences: data_preferences.Preferences = null; try { - let promise = data_preferences.getPreferences(context, { name: 'mystore' }); - promise.then((object) => { + let options: data_preferences.Options = { name: 'myStore' }; + let promise = data_preferences.getPreferences(context, options); + promise.then((object: data_preferences.Preferences) => { preferences = object; console.info("Succeeded in getting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -291,17 +307,20 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; -let preferences = null; +let preferences: data_preferences.Preferences = null; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - let promise = data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }); - promise.then((object) => { + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + let promise = data_preferences.getPreferences(this.context, options); + promise.then((object: data_preferences.Preferences) => { preferences = object; console.info("Succeeded in getting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -344,10 +363,12 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); try { - data_preferences.deletePreferences(context, 'mystore', function (err) { + data_preferences.deletePreferences(context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; @@ -363,11 +384,13 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - data_preferences.deletePreferences(this.context, 'mystore', function (err) { + data_preferences.deletePreferences(this.context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; @@ -419,13 +442,15 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); try { - let promise = data_preferences.deletePreferences(context, 'mystore'); + let promise = data_preferences.deletePreferences(context, 'myStore'); promise.then(() => { console.info("Succeeded in deleting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -437,14 +462,16 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try{ - let promise = data_preferences.deletePreferences(this.context, 'mystore'); + let promise = data_preferences.deletePreferences(this.context, 'myStore'); promise.then(() => { console.info("Succeeded in deleting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -489,10 +516,13 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); try { - data_preferences.deletePreferences(context, { name: 'mystore' }, function (err) { + let options: data_preferences.Options = { name: 'myStore' }; + data_preferences.deletePreferences(context, options, (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; @@ -508,11 +538,14 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - data_preferences.deletePreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err) { + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + data_preferences.deletePreferences(this.context, options, (err: BusinessError) => { if (err) { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); return; @@ -567,13 +600,16 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); try { - let promise = data_preferences.deletePreferences(context, { name: 'mystore' }); + let options: data_preferences.Options = { name: 'myStore' }; + let promise = data_preferences.deletePreferences(context, options); promise.then(() => { console.info("Succeeded in deleting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -585,14 +621,17 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try{ - let promise = data_preferences.deletePreferences(this.context, { name: 'mystore', dataGroupId:'myId' }); + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + let promise = data_preferences.deletePreferences(this.context, options); promise.then(() => { console.info("Succeeded in deleting preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to delete preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -630,9 +669,11 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); try { - data_preferences.removePreferencesFromCache(context, 'mystore', function (err) { + data_preferences.removePreferencesFromCache(context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; @@ -648,11 +689,13 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - data_preferences.removePreferencesFromCache(this.context, 'mystore', function (err) { + data_preferences.removePreferencesFromCache(this.context, 'myStore', (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; @@ -698,12 +741,14 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' + let context = featureAbility.getContext(); try { - let promise = data_preferences.removePreferencesFromCache(context, 'mystore'); + let promise = data_preferences.removePreferencesFromCache(context, 'myStore'); promise.then(() => { console.info("Succeeded in removing preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -715,14 +760,16 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - let promise = data_preferences.removePreferencesFromCache(this.context, 'mystore'); + let promise = data_preferences.removePreferencesFromCache(this.context, 'myStore'); promise.then(() => { console.info("Succeeded in removing preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -760,7 +807,7 @@ FA模型示例: import featureAbility from '@ohos.ability.featureAbility'; let context = featureAbility.getContext(); try { - data_preferences.removePreferencesFromCacheSync(context, 'mystore'); + data_preferences.removePreferencesFromCacheSync(context, 'myStore'); } catch(err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } @@ -772,9 +819,9 @@ Stage模型示例: import UIAbility from '@ohos.app.ability.UIAbility'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - data_preferences.removePreferencesFromCacheSync(this.context, 'mystore'); + data_preferences.removePreferencesFromCacheSync(this.context, 'myStore'); } catch(err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); } @@ -818,9 +865,11 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' let context = featureAbility.getContext(); try { - data_preferences.removePreferencesFromCache(context, { name: 'mystore' }, function (err) { + let options: data_preferences.Options = { name: 'myStore' }; + data_preferences.removePreferencesFromCache(context, options, (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; @@ -836,11 +885,14 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - data_preferences.removePreferencesFromCache(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err) { + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + data_preferences.removePreferencesFromCache(this.context, options, (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; @@ -895,12 +947,14 @@ FA模型示例: ```js // 获å–context import featureAbility from '@ohos.ability.featureAbility'; +import { BusinessError } from '@ohos.base' let context = featureAbility.getContext(); try { - let promise = data_preferences.removePreferencesFromCache(context, { name: 'mystore' }); + let options: data_preferences.Options = { name: 'myStore' }; + let promise = data_preferences.removePreferencesFromCache(context, options); promise.then(() => { console.info("Succeeded in removing preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -912,14 +966,17 @@ Stage模型示例: ```ts import UIAbility from '@ohos.app.ability.UIAbility'; +import { BusinessError } from '@ohos.base' +import window from '@ohos.window'; class EntryAbility extends UIAbility { - onWindowStageCreate(windowStage) { + onWindowStageCreate(windowStage: window.WindowStage) { try { - let promise = data_preferences.removePreferencesFromCache(this.context, { name: 'mystore', dataGroupId:'myId' }); + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + let promise = data_preferences.removePreferencesFromCache(this.context, options); promise.then(() => { console.info("Succeeded in removing preferences."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -967,7 +1024,7 @@ get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): ```js try { - preferences.get('startup', 'default', function (err, val) { + preferences.get('startup', 'default', (err: BusinessError, val: data_preferences.ValueType) => { if (err) { console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); return; @@ -1006,9 +1063,9 @@ get(key: string, defValue: ValueType): Promise<ValueType> ```js try { let promise = preferences.get('startup', 'default'); - promise.then((data) => { + promise.then((data: data_preferences.ValueType) => { console.info("Succeeded in getting value of 'startup'. Data: " + data); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -1041,7 +1098,7 @@ getSync(key: string, defValue: ValueType): ValueType ```js try { - let value = preferences.getSync('startup', 'default'); + let value: data_preferences.ValueType = preferences.getSync('startup', 'default'); console.info("Succeeded in getting value of 'startup'. Data: " + value); } catch(err) { console.error("Failed to get value of 'startup'. code =" + err.code + ", message =" + err.message); @@ -1066,14 +1123,14 @@ getAll(callback: AsyncCallback<Object>): void; ```js try { - preferences.getAll(function (err, value) { + preferences.getAll((err: BusinessError, value: data_preferences.ValueType) => { if (err) { console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); return; } - let allKeys = Object.keys(value); - console.info("getAll keys = " + allKeys); - console.info("getAll object = " + JSON.stringify(value)); + let allKeys = Object.keys(value); + console.info("getAll keys = " + allKeys); + console.info("getAll object = " + JSON.stringify(value)); }) } catch (err) { console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); @@ -1100,11 +1157,11 @@ getAll(): Promise<Object> ```js try { let promise = preferences.getAll(); - promise.then((value) => { + promise.then((value: data_preferences.ValueType) => { let allKeys = Object.keys(value); console.info('getAll keys = ' + allKeys); console.info("getAll object = " + JSON.stringify(value)); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to get all key-values. code =" + err.code + ", message =" + err.message); }) } catch (err) { @@ -1159,7 +1216,7 @@ put(key: string, value: ValueType, callback: AsyncCallback<void>): void ```js try { - preferences.put('startup', 'auto', function (err) { + preferences.put('startup', 'auto', (err: BusinessError) => { if (err) { console.error("Failed to put value of 'startup'. code =" + err.code + ", message =" + err.message); return; @@ -1200,7 +1257,7 @@ try { let promise = preferences.put('startup', 'auto'); promise.then(() => { console.info("Succeeded in putting value of 'startup'."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to put value of 'startup'. code =" + err.code +", message =" + err.message); }) } catch(err) { @@ -1254,7 +1311,7 @@ has(key: string, callback: AsyncCallback<boolean>): void ```js try { - preferences.has('startup', function (err, val) { + preferences.has('startup', (err: BusinessError, val: boolean) => { if (err) { console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); return; @@ -1264,7 +1321,7 @@ try { } else { console.info("The key 'startup' dose not contain."); } - }) + }) } catch (err) { console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); } @@ -1296,15 +1353,15 @@ has(key: string): Promise<boolean> ```js try { let promise = preferences.has('startup'); - promise.then((val) => { + promise.then((val: boolean) => { if (val) { console.info("The key 'startup' is contained."); } else { console.info("The key 'startup' dose not contain."); } - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); - }) + }) } catch(err) { console.error("Failed to check the key 'startup'. code =" + err.code + ", message =" + err.message); } @@ -1335,7 +1392,7 @@ hasSync(key: string): boolean ```js try { - let isExist = preferences.hasSync('startup'); + let isExist: boolean = preferences.hasSync('startup'); if (isExist) { console.info("The key 'startup' is contained."); } else { @@ -1366,7 +1423,7 @@ delete(key: string, callback: AsyncCallback<void>): void ```js try { - preferences.delete('startup', function (err) { + preferences.delete('startup', (err: BusinessError) => { if (err) { console.error("Failed to delete the key 'startup'. code =" + err.code + ", message =" + err.message); return; @@ -1406,7 +1463,7 @@ try { let promise = preferences.delete('startup'); promise.then(() => { console.info("Succeeded in deleting the key 'startup'."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to delete the key 'startup'. code =" + err.code +", message =" + err.message); }) } catch(err) { @@ -1458,7 +1515,7 @@ flush(callback: AsyncCallback<void>): void ```js try { - preferences.flush(function (err) { + preferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. code =" + err.code + ", message =" + err.message); return; @@ -1492,7 +1549,7 @@ try { let promise = preferences.flush(); promise.then(() => { console.info("Succeeded in flushing."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to flush. code =" + err.code + ", message =" + err.message); }) } catch (err) { @@ -1519,7 +1576,7 @@ clear(callback: AsyncCallback<void>): void ```js try { - preferences.clear(function (err) { + preferences.clear((err: BusinessError) =>{ if (err) { console.error("Failed to clear. code =" + err.code + ", message =" + err.message); return; @@ -1553,7 +1610,7 @@ try { let promise = preferences.clear(); promise.then(() => { console.info("Succeeded in clearing."); - }).catch((err) => { + }).catch((err: BusinessError) => { console.error("Failed to clear. code =" + err.code + ", message =" + err.message); }) } catch(err) { @@ -1599,29 +1656,31 @@ on(type: 'change', callback: Callback<{ key : string }>): void **示例:** ```js +interface observer { + key: string +} try { - data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { + data_preferences.getPreferences(this.context, 'myStore', (err: BusinessError, preferences: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences."); return; } - let observer = function (key) { + preferences.on('change', (key: observer) => { console.info("The key " + key + " changed."); - } - preferences.on('change', observer); - preferences.put('startup', 'manual', function (err) { + }); + preferences.put('startup', 'manual', (err: BusinessError) => { if (err) { - console.error("Failed to put the value of 'startup'. Cause: " + err); - return; + console.error("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.error("Failed to flush. Cause: " + err); - return; - } - console.info("Succeeded in flushing."); + preferences.flush((err: BusinessError) => { + if (err) { + console.error("Failed to flush. Cause: " + err); + return; + } + console.info("Succeeded in flushing."); }) }) }) @@ -1658,24 +1717,26 @@ on(type: 'multiProcessChange', callback: Callback<{ key : string }>): void **示例1:** ```js +interface observer { + key: string +} try { - data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, preferences) { + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + data_preferences.getPreferences(this.context, options, (err: BusinessError, preferences: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences."); return; } - let observer = function (key) { + preferences.on('multiProcessChange', (key: observer) => { console.info("The key " + key + " changed."); - } - preferences.on('multiProcessChange', observer); - preferences.put('startup', 'manual', function (err) { + }); + preferences.put('startup', 'manual', (err: BusinessError) => { if (err) { console.error("Failed to put the value of 'startup'. Cause: " + err); return; } console.info("Succeeded in putting the value of 'startup'."); - - preferences.flush(function (err) { + preferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; @@ -1692,17 +1753,21 @@ try { **示例2:** ```js +interface observer { + key: string +} try { - data_preferences.getPreferences(this.context, { name: 'mystore' }, function (err, val) { + let options: data_preferences.Options = { name: 'myStore' }; + data_preferences.getPreferences(this.context, options, (err: BusinessError, val: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences."); return; } preferences = val; - let observer = function (key) { + preferences.on('multiProcessChange', (key: observer) => { console.info("The key " + key + " changed."); try { - data_preferences.removePreferencesFromCache(this.context, { name: 'mystore' }, function (err) { + data_preferences.removePreferencesFromCache(this.context, options, (err: BusinessError) => { if (err) { console.error("Failed to remove preferences. code =" + err.code + ", message =" + err.message); return; @@ -1715,7 +1780,7 @@ try { } try { - data_preferences.getPreferences(this.context, { name: 'mystore' }, function (err, val) { + data_preferences.getPreferences(this.context, options, (err: BusinessError, val: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); return; @@ -1726,16 +1791,15 @@ try { } catch (err) { console.error("Failed to get preferences. code =" + err.code + ", message =" + err.message); } - } - preferences.on('multiProcessChange', observer); - preferences.put('startup', 'manual', function (err) { + }); + preferences.put('startup', 'manual', (err: BusinessError) => { if (err) { console.error("Failed to put the value of 'startup'. Cause: " + err); return; } console.info("Succeeded in putting the value of 'startup'."); - preferences.flush(function (err) { + preferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; @@ -1767,31 +1831,35 @@ off(type: 'change', callback?: Callback<{ key : string }>): void **示例:** ```js +interface observer { + key: string +} try { - data_preferences.getPreferences(this.context, 'mystore', function (err, preferences) { + data_preferences.getPreferences(this.context, 'myStore', (err: BusinessError, val: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences."); return; } - let observer = function (key) { + preferences.on('change', (key: observer) => { console.info("The key " + key + " changed."); - } - preferences.on('change', observer); - preferences.put('startup', 'auto', function (err) { + }); + preferences.put('startup', 'auto', (err: BusinessError) => { if (err) { console.error("Failed to put the value of 'startup'. Cause: " + err); return; } console.info("Succeeded in putting the value of 'startup'."); - preferences.flush(function (err) { + preferences.flush((err: BusinessError) =>{ if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Succeeded in flushing."); }) - preferences.off('change', observer); + preferences.off('change', (key: observer) => { + console.info("The key " + key + " changed."); + }); }) }) } catch (err) { @@ -1817,31 +1885,36 @@ off(type: 'multiProcessChange', callback?: Callback<{ key : string }>): vo **示例:** ```js +interface observer { + key: string +} try { - data_preferences.getPreferences(this.context, { name: 'mystore', dataGroupId:'myId' }, function (err, preferences) { + let options: data_preferences.Options = { name: 'myStore', dataGroupId:'myId' }; + data_preferences.getPreferences(this.context, options, (err: BusinessError, preferences: data_preferences.Preferences) => { if (err) { console.error("Failed to get preferences."); return; } - let observer = function (key) { + preferences.on('multiProcessChange', (key: observer) => { console.info("The key " + key + " changed."); - } - preferences.on('multiProcessChange', observer); - preferences.put('startup', 'auto', function (err) { + }); + preferences.put('startup', 'auto', (err: BusinessError) => { if (err) { console.error("Failed to put the value of 'startup'. Cause: " + err); return; } console.info("Succeeded in putting the value of 'startup'."); - preferences.flush(function (err) { + preferences.flush((err: BusinessError) => { if (err) { console.error("Failed to flush. Cause: " + err); return; } console.info("Succeeded in flushing."); }) - preferences.off('multiProcessChange', observer); + preferences.off('multiProcessChange', (key: observer) => { + console.info("The key " + key + " changed."); + }); }) }) } catch (err) {