当前位置:   article > 正文

HarmonyOS使用本地存储dataPreferences_datapreferences.preferences

datapreferences.preferences

HarmonyOS中使用类似与浏览器的localStorage.

官方链接:文档中心

1、封装

  1. import dataPreferences from '@ohos.data.preferences';
  2. import promptAction from '@ohos.promptAction';
  3. let context = getContext(this);
  4. let preference: dataPreferences.Preferences;
  5. let preferenceTemp: dataPreferences.Preferences;
  6. /**
  7. * Preference model.
  8. *
  9. * @param fruitData Fruit data.
  10. */
  11. class PreferenceModel {
  12. /**
  13. * Read the specified Preferences persistence file and load the data into the Preferences instance.
  14. */
  15. async getPreferencesFromStorage(db_name: string) {
  16. try {
  17. preference = await dataPreferences.getPreferences(context, db_name);
  18. } catch (err) {
  19. }
  20. }
  21. /**
  22. * 删除本地存储
  23. * Deletes the specified Preferences persistence file from memory and removes the Preferences instance.
  24. */
  25. async deletePreferences(db_name: string) {
  26. try {
  27. await dataPreferences.deletePreferences(context, db_name);
  28. } catch(err) {
  29. };
  30. preference = preferenceTemp;
  31. }
  32. /**
  33. * Save the data to the Preferences.
  34. * 持久化存储
  35. * @param fruit Fruit data.
  36. */
  37. async putPreference(data: any, db_name: string, key: string) {
  38. if (!preference) {
  39. await this.getPreferencesFromStorage(db_name);
  40. }
  41. // The fruit name and fruit quantity data entered by the user are saved to the cached Preference instance.
  42. try {
  43. if (typeof data === 'object' && data !== null) {
  44. await preference.put(key, JSON.stringify(data));
  45. } else {
  46. await preference.put(key, data);
  47. }
  48. } catch (err) {
  49. }
  50. await preference.flush();
  51. }
  52. /**
  53. * 取数据
  54. * Get preference data.
  55. */
  56. async getPreference(db_name: string, key: string) {
  57. let storage;
  58. if (!preference) {
  59. await this.getPreferencesFromStorage(db_name);
  60. }
  61. try {
  62. storage = (await preference.get(key, ''));
  63. } catch (err) {
  64. }
  65. // If the data is empty, a message is displayed indicating that data needs to be written.
  66. if (!storage) {
  67. return '';
  68. }
  69. return storage;
  70. }
  71. /**
  72. * write data.
  73. * 存数据
  74. * @param fruit Fruit data.
  75. */
  76. writeData(data: any, db_name: string, key: string) {
  77. // The data is inserted into the preferences database if it is not empty.
  78. this.putPreference(data, db_name, key);
  79. console.log(`${db_name}-${key}---writeData成功`)
  80. }
  81. /**
  82. * Process the data obtained from the database.
  83. */
  84. async getData(db_name: string, key: string) {
  85. return await this.getPreference(db_name, key);
  86. }
  87. /**
  88. * Popup window prompt message.
  89. *
  90. * @param message Prompt message.
  91. */
  92. showToastMessage(message: Resource) {
  93. promptAction.showToast({
  94. message: message,
  95. duration: 3000
  96. });
  97. };
  98. }
  99. export default new PreferenceModel();

2、存数据

  1. import PreferenceModel from '../mode/PreferenceModel';
  2. PreferenceModel.writeData('存入的数据', 'userInfo', 'user')

3、取数据

  1. import PreferenceModel from '../mode/PreferenceModel';
  2. const user = await PreferenceModel.getPreference('userInfo', 'user')

注意事项:

官方说的:数据存储形式为键值对,键的类型为字符串型,值的存储数据类型包括数字型、字符型、布尔型以及这3种类型的数组类型。

如果你存的是对象,那么会自动转换为json字符串。所以在用的时候,需要JSON.parse()

上一章: HarmonyOS实现Tabs-CSDN博客

下一章: HarmonyOS自定义标题栏-CSDN博客

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

闽ICP备14008679号