storage.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-common-mt">
  5. <view class="uni-list">
  6. <view class="uni-list-cell">
  7. <view class="uni-list-cell-left">
  8. <view class="uni-label">key</view>
  9. </view>
  10. <view class="uni-list-cell-db">
  11. <input class="uni-input" type="text" placeholder="请输入key" name="key" :value="key" @input="keyChange"/>
  12. </view>
  13. </view>
  14. <view class="uni-list-cell">
  15. <view class="uni-list-cell-left">
  16. <view class="uni-label">value</view>
  17. </view>
  18. <view class="uni-list-cell-db">
  19. <input class="uni-input" type="text" placeholder="请输入value" name="data" :value="data" @input="dataChange"/>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="uni-padding-wrap">
  24. <view class="uni-btn-v">
  25. <button type="primary" class="btn-setstorage" @tap="setStorage">存储数据</button>
  26. <button @tap="getStorage">读取数据</button>
  27. <button @tap="clearStorage">清理数据</button>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. title: 'get/set/clearStorage',
  38. key: '',
  39. data: ''
  40. }
  41. },
  42. methods: {
  43. keyChange: function (e) {
  44. this.key = e.detail.value
  45. },
  46. dataChange: function (e) {
  47. this.data = e.detail.value
  48. },
  49. getStorage: function () {
  50. var key = this.key,
  51. data = this.data;
  52. if (key.length === 0) {
  53. uni.showModal({
  54. title: '读取数据失败',
  55. content: "key 不能为空",
  56. showCancel:false
  57. })
  58. } else {
  59. uni.getStorage({
  60. key: key,
  61. success: (res) => {
  62. uni.showModal({
  63. title: '读取数据成功',
  64. content: "data: '" + res.data + "'",
  65. showCancel:false
  66. })
  67. },
  68. fail: () => {
  69. uni.showModal({
  70. title: '读取数据失败',
  71. content: "找不到 key 对应的数据",
  72. showCancel:false
  73. })
  74. }
  75. })
  76. }
  77. },
  78. setStorage: function () {
  79. var key = this.key
  80. var data = this.data
  81. if (key.length === 0) {
  82. uni.showModal({
  83. title: '保存数据失败',
  84. content: 'key 不能为空',
  85. showCancel:false
  86. })
  87. } else {
  88. uni.setStorage({
  89. key: key,
  90. data: data,
  91. success: (res) => {
  92. uni.showModal({
  93. title: '存储数据成功',
  94. // #ifndef MP-ALIPAY
  95. content: JSON.stringify(res.errMsg),
  96. // #endif
  97. // #ifdef MP-ALIPAY
  98. content: data,
  99. // #endif
  100. showCancel:false
  101. })
  102. },
  103. fail: () => {
  104. uni.showModal({
  105. title: '储存数据失败!',
  106. showCancel:false
  107. })
  108. }
  109. })
  110. }
  111. },
  112. clearStorage: function () {
  113. uni.clearStorageSync()
  114. this.key = '',
  115. this.data = ''
  116. uni.showModal({
  117. title: '清除数据成功',
  118. content: ' ',
  119. showCancel:false
  120. })
  121. }
  122. }
  123. }
  124. </script>
  125. <style>
  126. .btn-setstorage {
  127. background-color: #007aff;
  128. color: #ffffff;
  129. }
  130. </style>