• uniapp缓存对象数组


    需求:使用uniapp,模拟key(表名)增删改查对象数组,每个key可以单独操作,并模拟面对对象对应表,每个key对应的baseInstance 类似一个操作类,当然如果你场景比较简单,可以改为固定key或者传值key,调普通js而不需要new

    base.js

    1. export default {
    2. data() {
    3. return {}
    4. },
    5. methods: {
    6. // 一是不需要new来调用,二是参数非常灵活,可以不传,也可以这么传
    7. createBaseStore(key) {
    8. return new this.baseInstance(key || {})
    9. },
    10. // 函数创建对象 每个key对应自己的方法 达到实例化效果 使用 baseInstance.addExt(obj) 自动携带固定key
    11. baseInstance(key) {
    12. this.dataKey = key;
    13. this.addExt = function addExt(obj) {
    14. var dataList = this.getAllExt()
    15. if (dataList == null) {
    16. dataList = new Array()
    17. }
    18. var newItemid = 0
    19. const last = dataList.length - 1
    20. if (last >= 0) {
    21. newItemid = dataList[last].id + 1
    22. }
    23. obj.id = newItemid
    24. dataList.push(obj)
    25. this.save(dataList)
    26. }
    27. this.removeExt = function removeExt(param) {
    28. var dataList = this.getAllExt();
    29. var findItemIndex = dataList.findIndex(item => {
    30. if (item.id == param) {
    31. return true
    32. }
    33. })
    34. if (findItemIndex >= 0) {
    35. const newList = dataList.splice(findItemIndex, 1)
    36. console.log("remove item is index", findItemIndex, JSON.stringify(newList))
    37. this.save(dataList)
    38. } else {
    39. console.log("not find remove param", param)
    40. }
    41. }
    42. this.changeExt = function changeExt() {
    43. console.log("change")
    44. }
    45. this.searchExt = function searchExt() {
    46. console.log("search")
    47. }
    48. this.save = function save(dataList) {
    49. var dataJson = JSON.stringify(dataList)
    50. uni.setStorage({
    51. key: key,
    52. data: dataJson,
    53. success: function() {
    54. console.log("key:", key, 'addExt success', dataJson);
    55. console.log('curr size', dataList.length);
    56. }
    57. });
    58. }
    59. this.getAllExt = function getAllExt() {
    60. try {
    61. const value = uni.getStorageSync(this.dataKey);
    62. if (!value) {
    63. console.log('getAllExt is empty');
    64. return null
    65. }
    66. const dataBean = JSON.parse(value)
    67. if (dataBean) {
    68. return dataBean
    69. }
    70. } catch (e) {
    71. console.log("showAllToLogExt error", e);
    72. }
    73. return null
    74. }
    75. this.showAllToLogExt = function showAllToLogExt() {
    76. try {
    77. const value = this.getAllExt();
    78. if (value) {
    79. console.log("showAllToLogExt", value);
    80. }
    81. } catch (e) {
    82. console.log("showAllToLogExt error", e);
    83. }
    84. }
    85. },
    86. clearAllExt() {
    87. console.log("clearAllExt")
    88. uni.clearStorage()
    89. }
    90. }
    91. }

    vue使用

    1. <script>
    2. import base from "@/pages/base/base.js"
    3. export default {
    4. mixins: [base],
    5. data() {
    6. return {
    7. title: 'demo学习',
    8. TestBean: {
    9. id: 0,
    10. a: "",
    11. b: "",
    12. c: "",
    13. },
    14. dataExt: {},
    15. dataExt2: {},
    16. storeInstance1: this.createBaseStore("key111"),
    17. storeInstance2: this.createBaseStore("key222"),
    18. indexId: 0
    19. }
    20. },
    21. onload() {
    22. getAllExt()
    23. },
    24. methods: {
    25. showAllToLog() {
    26. this.storeInstance1.showAllToLogExt()
    27. this.storeInstance2.showAllToLogExt()
    28. },
    29. add() {
    30. // 操作类型1实例 ,存对象2到对象数组2
    31. var currentTime = new Date();
    32. this.dataExt.name = "111"
    33. this.dataExt.goodsTime = currentTime
    34. this.storeInstance1.addExt(this.dataExt)
    35. // key1Store.remove
    36. // key1Store. 等等操作
    37. // 操作类型2实例 ,存对象2到对象数组2
    38. this.dataExt2.name = "param2"
    39. this.dataExt2.param2Time = currentTime
    40. this.storeInstance2.addExt(this.dataExt2)
    41. },
    42. remove() {
    43. // 模拟后续加上id即可目前只是掩饰
    44. this.storeInstance1.removeExt(4)
    45. this.storeInstance2.removeExt(4)
    46. },
    47. change() {
    48. this.changeExt()
    49. },
    50. search() {
    51. this.searchExt()
    52. },
    53. clearAll() {
    54. this.clearAllExt()
    55. },
    56. }
    57. }
    58. script>
    59. <style>
    60. style>

  • 相关阅读:
    Python数学计算工具4、Python求最大公约数
    JavaWeb——Servlet原理、生命周期、IDEA中实现一个Servlet(全过程)
    【论文解读】Prefix-Tuning: Optimizing Continuous Prompts for Generation
    活动报名|9月24日 Apache Flink Meetup · 北京站,Flink 1.16 新版本发布!
    hadoop的yarn部署
    使用两个栈实现一个队列
    LCR 041.数据流中的移动平均值
    码农的转型之路-多年以来的反思
    拉普拉斯Laplace算子(高斯二阶导核)
    2-STM32GPIO输入之按键
  • 原文地址:https://blog.csdn.net/qq_28844947/article/details/133933508