#鸿蒙学习大百科#FA模型下如何获取KvManager实例?

FA模型下如何获取KvManager实例?

HarmonyOS
2024-10-23 11:19:23
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
花鸟吹雪
// 导入模块
import distributedKVStore from '@ohos.data.distributedKVStore';
import { BusinessError } from '@ohos.base';

// FA模型
import featureAbility from '@ohos.ability.featureAbility';

let kvManager: distributedKVStore.KVManager | undefined = undefined;
let context = featureAbility.getContext(); // 获取context
const kvManagerConfig: distributedKVStore.KVManagerConfig = {
  context: context,
  bundleName: 'com.example.datamanagertest'
};
try {
  kvManager = distributedKVStore.createKVManager(kvManagerConfig);
  console.info('Succeeded in creating KVManager.');
  // 继续创建获取数据库
} catch (e) {
  let error = e as BusinessError;
  console.error(`Failed to create KVManager. Code:${error.code},message:${error.message}`);
}
if (kvManager !== undefined) {
  kvManager = kvManager as distributedKVStore.KVManager;
  //进行后续操作
  //...
}
分享
微博
QQ
微信
回复
2024-10-23 17:28:31
相关问题
如何获取FA模型的Context?
349浏览 • 1回复 待解决