当前位置:   article > 正文

鸿蒙开发如何通过用户首选项实现数据持久化

鸿蒙开发如何通过用户首选项实现数据持久化

1.导入用户首选项模块。

import preferences from '@ohos.data.preferences';

2.要通过用户首选项实现数据持久化,首先要获取Preferences实例。读取指定文件,将数据加载到Preferences实例,用于数据操作。

所有代码实例:

  1. import preferences from '@ohos.data.preferences';
  2. class PreferencesUtil{
  3. preMap:Map<string,preferences.Preferences>=new Map()
  4. async loadPreference(context,name:string){
  5. try {
  6. let pref = await preferences.getPreferences(context,name)
  7. this.preMap.set(name,pref)
  8. console.log(`加载preference${name}成功`)
  9. }catch(e){
  10. console.log(`加载preference${name}失败`,JSON.stringify(e))
  11. }
  12. }
  13. //存储数据
  14. async putPreferenceVale(name:string,key:string,value:preferences.ValueType){
  15. if(!this.preMap.has(name)){
  16. console.log(`加载preference${name}失败`,`${name},尚未初始化`)
  17. return
  18. }
  19. try{
  20. console.log(JSON.stringify(value),'你好')
  21. let pref=this.preMap.get(name)
  22. // @ts-ignore
  23. await pref.put(key,JSON.stringify(value))
  24. // @ts-ignore
  25. await pref.flush()
  26. console.log("成功",`保存${name}.${key}=${value}成功`)
  27. // return value
  28. }catch(e){
  29. console.log("保存失败",JSON.stringify(e))
  30. }
  31. }
  32. //读取数据
  33. async getPreferenceValue(name:string,key:string,defaultVale:preferences.ValueType){
  34. if(!this.preMap.has(name)){
  35. console.log(`加载preference${name}失败`,`${name},尚未初始化`)
  36. return
  37. }
  38. try{
  39. console.log(JSON.stringify(this.preMap),'hhhh')
  40. let pref=this.preMap.get(name)
  41. // @ts-ignore
  42. console.log(JSON.stringify(pref),"赛666666")
  43. let value= await pref.get(key,defaultVale)
  44. console.log("testTag",`读取${name},${key}=${value}成功}`)
  45. return value
  46. }catch(e){
  47. console.log("读取失败",JSON.stringify(e))
  48. }
  49. }
  50. pref(pref: any,arg1: string) {
  51. throw new Error('Method not implemented.');
  52. }
  53. }
  54. const preferencesUtil = new PreferencesUtil();
  55. export default preferencesUtil as PreferencesUtil;

注意:存储完需要pref.flush() 进行持久化.

3.项目中具体读取示例.

  1. @State _id:string = ""
  2. @State username:string = ""
  3. @State password:string = ""
  4. @State imgPath:string = ""
  5. async aboutToAppear(){
  6. this._id = await SharePre.getPreferenceValue('MyPreferences','_id','666') as string
  7. this.username = await SharePre.getPreferenceValue('MyPreferences','username','666') as string
  8. this.password = await SharePre.getPreferenceValue('MyPreferences','password','666') as string
  9. this.imgPath = await SharePre.getPreferenceValue('MyPreferences','imgPath','666') as string
  10. }

4.写入

  1. SharePre.putPreferenceVale('MyPreferences','username',val.data.data.username)
  2. SharePre.putPreferenceVale('MyPreferences','password',val.data.data.password)
  3. SharePre.putPreferenceVale('MyPreferences','telephone',val.data.data.telephone)
  4. SharePre.putPreferenceVale('MyPreferences','_id',val.data.data._id)
  5. SharePre.putPreferenceVale('MyPreferences','imgPath',val.data.data.imgPath)

修改会替换掉上次的存储

最终拿出来数据 需要进行切割

Image(this.imgPath.slice(1,this.imgPath.length-1))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/339423
推荐阅读
相关标签
  

闽ICP备14008679号