提交 a749f7e2 编写于 作者: W wangkai

指导文档示例代码添加

Signed-off-by: Nwangkai <wangkai424@huawei.com>
上级 5f958b1c
...@@ -39,16 +39,20 @@ createKVManager(config: KVManagerConfig, callback: AsyncCallback&lt;KVManager&gt ...@@ -39,16 +39,20 @@ createKVManager(config: KVManagerConfig, callback: AsyncCallback&lt;KVManager&gt
| callback | AsyncCallback&lt;[KVManager](#kvmanager)&gt; | 是 | 回调函数。返回创建的KVManager对象实例。 | | callback | AsyncCallback&lt;[KVManager](#kvmanager)&gt; | 是 | 回调函数。返回创建的KVManager对象实例。 |
**示例:** **示例:**
1)ability或extension开发场景:
```js ```js
import AbilityStage from '@ohos.application.Ability'
let kvManager; let kvManager;
try { export default class MyAbilityStage extends AbilityStage {
var contextmock = featureAbility.getContext(); onCreate() {
console.log("MyAbilityStage onCreate")
let context = this.context
const kvManagerConfig = { const kvManagerConfig = {
context : contextmock, context: context,
bundleName : 'com.example.datamanagertest', bundleName: 'com.example.datamanagertest',
userInfo : { userInfo: {
userId : '0', userId: '0',
userType : distributedData.UserType.SAME_USER_ID userType: distributedData.UserType.SAME_USER_ID
} }
} }
distributedData.createKVManager(kvManagerConfig, function (err, manager) { distributedData.createKVManager(kvManagerConfig, function (err, manager) {
...@@ -59,8 +63,35 @@ try { ...@@ -59,8 +63,35 @@ try {
console.log("createKVManager success"); console.log("createKVManager success");
kvManager = manager; kvManager = manager;
}); });
} catch (e) { }
console.log("An unexpected error occurred. Error:" + e); }
```
2)application开发场景:
```js
import AbilityStage from '@ohos.application.Ability'
let kvManager;
export default class MyAbilityStage extends AbilityStage {
onCreate() {
console.log("MyAbilityStage onCreate")
let context = this.context
const kvManagerConfig = {
context: context.getApplicationContext(),
bundleName: 'com.example.datamanagertest',
userInfo: {
userId: '0',
userType: distributedData.UserType.SAME_USER_ID
}
}
distributedData.createKVManager(kvManagerConfig, function (err, manager) {
if (err) {
console.log("createKVManager err: " + JSON.stringify(err));
return;
}
console.log("createKVManager success");
kvManager = manager;
});
}
} }
``` ```
...@@ -85,27 +116,59 @@ createKVManager(config: KVManagerConfig): Promise&lt;KVManager&gt; ...@@ -85,27 +116,59 @@ createKVManager(config: KVManagerConfig): Promise&lt;KVManager&gt;
| Promise&lt;[KVManager](#kvmanager)&gt; | Promise对象。返回创建的KVManager对象实例。 | | Promise&lt;[KVManager](#kvmanager)&gt; | Promise对象。返回创建的KVManager对象实例。 |
**示例:** **示例:**
1)ability或extension开发场景:
```js ```js
import AbilityStage from '@ohos.application.Ability'
let kvManager; let kvManager;
try { export default class MyAbilityStage extends AbilityStage {
var contextmock = featureAbility.getContext(); onCreate() {
console.log("MyAbilityStage onCreate")
let context = this.context
const kvManagerConfig = { const kvManagerConfig = {
context : contextmock, context: context,
bundleName : 'com.example.datamanagertest', bundleName: 'com.example.datamanagertest',
userInfo : { userInfo: {
userId : '0', userId: '0',
userType : distributedData.UserType.SAME_USER_ID userType: distributedData.UserType.SAME_USER_ID
}
} }
distributedData.createKVManager(kvManagerConfig, function (err, manager) {
if (err) {
console.log("createKVManager err: " + JSON.stringify(err));
return;
} }
distributedData.createKVManager(kvManagerConfig).then((manager) => {
console.log("createKVManager success"); console.log("createKVManager success");
kvManager = manager; kvManager = manager;
}).catch((err) => { });
}
}
```
2)application开发场景:
```js
import AbilityStage from '@ohos.application.Ability'
let kvManager;
export default class MyAbilityStage extends AbilityStage {
onCreate() {
console.log("MyAbilityStage onCreate")
let context = this.context
const kvManagerConfig = {
context: context.getApplicationContext(),
bundleName: 'com.example.datamanagertest',
userInfo: {
userId: '0',
userType: distributedData.UserType.SAME_USER_ID
}
}
distributedData.createKVManager(kvManagerConfig, function (err, manager) {
if (err) {
console.log("createKVManager err: " + JSON.stringify(err)); console.log("createKVManager err: " + JSON.stringify(err));
return;
}
console.log("createKVManager success");
kvManager = manager;
}); });
} catch (e) { }
console.log("An unexpected error occurred. Error:" + e);
} }
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册