鸿蒙开发-使用Storage实现数据存储

梦里梦外; 2022-10-11 14:56 315阅读 0赞

场景

鸿蒙开发-从搭建todolist待办事项来学习组件与js之间的交互:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/118384865

在上面实现一个简单的todolist之后,待办事项的数据并没有进行存储。

每次进入页面就会重新加载数据源的数据。

所以怎样将数据存储。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

参照官方的API文档,需要导入模块

  1. import storage from '@system.storage';

读取存储的内容

  1. storage.get(OBJECT)

参数










































参数名

类型

必填

说明

key

string

内容索引。

default

string

key不存在则返回的默认值。如果未指定,则返回空字符串,长度为0。

success

Function

接口调用成功的回调函数,返回存储的内容。

fail

Function

接口调用失败的回调函数。

complete

Function

接口调用结束的回调函数。

示例

  1. storage.get({
  2. key: 'storage_key',
  3. success: function(data) {
  4. console.log('call storage.get success: ' + data);
  5. },
  6. fail: function(data, code) {
  7. console.log('call storage.get fail, code: ' + code + ', data: ' + data);
  8. },
  9. complete: function() {
  10. console.log('call complete');
  11. },
  12. });

修改存储的内容

参数










































参数名

类型

必填

说明

key

string

要修改的存储内容的索引。

value

string

新值。value是长度为0的空字符串,则删除索引为key的数据项。

success

Function

接口调用成功的回调函数。

fail

Function

接口调用失败的回调函数。

complete

Function

接口调用结束的回调函数。

示例

  1. storage.set({
  2. key: 'storage_key',
  3. value: 'storage value',
  4. success: function() {
  5. console.log('call storage.set success.');
  6. },
  7. fail: function(data, code) {
  8. console.log('call storage.set fail, code: ' + code + ', data: ' + data);
  9. },
  10. });

通过key来进行读取和存储。

所以在上面todolist的js中导入模块后,添加生命周期方法onInit,

在初始化完成后从storage中读取数据并赋值给todolist

  1. onInit() {
  2. storage.get({
  3. key: 'todoList',
  4. success: data => {
  5. console.log('获取Storage成功' + data);
  6. this.todoList = JSON.parse(data)
  7. }
  8. });
  9. },

然后再新建一个存储数据的方法

  1. setStorage() {
  2. storage.set({
  3. key: 'todoList',
  4. value: JSON.stringify(this.todoList)
  5. });
  6. },

在对todolist进行增删改时调用存储的方法

  1. remove(index){
  2. console.log(index)
  3. this.todoList.splice(index,1)
  4. this.setStorage();
  5. },
  6. addTodo() {
  7. this.todoList.push({
  8. info:this.inputTodo,
  9. status: false
  10. })
  11. this.setStorage();
  12. },
  13. checkStatus(index){
  14. console.log(index);
  15. this.todoList[index].status = !this.todoList[index].status;
  16. this.setStorage();
  17. },

完整的js代码

  1. import todoList from "../../common/datas/todoList.js"
  2. import router from '@system.router';
  3. import storage from '@system.storage';
  4. export default {
  5. data: {
  6. // 待办事件列表
  7. todoList,
  8. inputTodo: "IDE无法调用输入"
  9. },
  10. onInit() {
  11. storage.get({
  12. key: 'todoList',
  13. success: data => {
  14. console.log('获取Storage成功' + data);
  15. this.todoList = JSON.parse(data)
  16. }
  17. });
  18. },
  19. setStorage() {
  20. storage.set({
  21. key: 'todoList',
  22. value: JSON.stringify(this.todoList)
  23. });
  24. },
  25. computed:{
  26. needTodoNum(){
  27. let num = 0;
  28. this.todoList.forEach(item => {
  29. if(!item.status){
  30. num++;
  31. }
  32. });
  33. return num;
  34. }
  35. },
  36. remove(index){
  37. console.log(index)
  38. this.todoList.splice(index,1)
  39. this.setStorage();
  40. },
  41. addTodo() {
  42. this.todoList.push({
  43. info:this.inputTodo,
  44. status: false
  45. })
  46. this.setStorage();
  47. },
  48. checkStatus(index){
  49. console.log(index);
  50. this.todoList[index].status = !this.todoList[index].status;
  51. this.setStorage();
  52. },
  53. getNewTodo(e){
  54. this.inputTodo = e.value;
  55. },
  56. goback(){
  57. router.back();
  58. }
  59. }

运行效果

20210701154203289.gif

发表评论

表情:
评论列表 (有 0 条评论,315人围观)

还没有评论,来说两句吧...

相关阅读