{"id":185632,"date":"2022-07-29T11:07:34","date_gmt":"2022-07-29T03:07:34","guid":{"rendered":"http:\/\/www.idc.net\/help\/185632\/"},"modified":"2022-07-29T11:07:34","modified_gmt":"2022-07-29T03:07:34","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%95%99%e7%a8%8b%ef%bc%9a%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8fapi-%e6%95%b0%e6%8d%ae%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/185632\/","title":{"rendered":"\u540e\u6d6a\u4e91\u5c0f\u7a0b\u5e8f\u6559\u7a0b\uff1a\u5fae\u4fe1\u5c0f\u7a0b\u5e8fAPI \u6570\u636e\u7f13\u5b58"},"content":{"rendered":"<p>\u6bcf\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u672c\u5730\u7f13\u5b58\uff0c\u53ef\u4ee5\u901a\u8fc7wx.setStorage\uff08wx.setStorageSync\uff09\u3001wx.getStorage\uff08wx.getStorageSync\uff09\u3001wx.clearStorage\uff08wx.clearStorageSync\uff09\u53ef\u4ee5\u5bf9\u672c\u5730\u7f13\u5b58\u8fdb\u884c\u8bbe\u7f6e\u3001\u83b7\u53d6\u548c\u6e05\u7406\u3002\u540c\u4e00\u4e2a\u5fae\u4fe1\u7528\u6237\uff0c\u540c\u4e00\u4e2a\u5c0f\u7a0b\u5e8f storage \u4e0a\u9650\u4e3a 10MB\u3002localStorage \u4ee5\u7528\u6237\u7ef4\u5ea6\u9694\u79bb\uff0c\u540c\u4e00\u53f0\u8bbe\u5907\u4e0a\uff0cA \u7528\u6237\u65e0\u6cd5\u8bfb\u53d6\u5230 B \u7528\u6237\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> localStorage\u662f\u6c38\u4e45\u5b58\u50a8\u7684\uff0c\u4f46\u662f\u6211\u4eec\u4e0d\u5efa\u8bae\u5c06\u5173\u952e\u4fe1\u606f\u5168\u90e8\u5b58\u5728localStorage\uff0c\u4ee5\u9632\u7528\u6237\u6362\u8bbe\u5907\u7684\u60c5\u51b5\u3002<\/p>\n<h2>wx.setStorage(OBJECT)<\/h2>\n<hr>\n<p>\u5c06\u6570\u636e\u5b58\u50a8\u5728\u672c\u5730\u7f13\u5b58\u4e2d\u6307\u5b9a\u7684key\u4e2d\uff0c\u4f1a\u8986\u76d6\u6389\u539f\u6765\u8be5key\u5bf9\u5e94\u7684\u5185\u5bb9\uff0c\u8fd9\u662f\u4e00\u4e2a\u5f02\u6b65\u63a5\u53e3\u3002<\/p>\n<p><strong>OBJECT\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>String<\/td>\n<td>\u662f<\/td>\n<td>\u672c\u5730\u7f13\u5b58\u4e2d\u7684\u6307\u5b9a\u7684 key<\/td>\n<\/tr>\n<tr>\n<td>data<\/td>\n<td>Object\/String<\/td>\n<td>\u662f<\/td>\n<td>\u9700\u8981\u5b58\u50a8\u7684\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td>success<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u6210\u529f\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>fail<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>complete<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong> <\/p>\n<pre><code>wx.setStorage({\n  key:\"key\"\n  data:\"value\"\n})<\/code><\/pre>\n<h2>wx.setStorageSync(KEY,DATA)<\/h2>\n<hr>\n<p>\u200b\u5c06data\u5b58\u50a8\u5728\u672c\u5730\u7f13\u5b58\u4e2d\u6307\u5b9a\u7684key\u4e2d\uff0c\u4f1a\u8986\u76d6\u6389\u539f\u6765\u8be5key\u5bf9\u5e94\u7684\u5185\u5bb9\uff0c\u8fd9\u662f\u4e00\u4e2a\u540c\u6b65\u63a5\u53e3\u3002<\/p>\n<p><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>String<\/td>\n<td>\u662f<\/td>\n<td>\u672c\u5730\u7f13\u5b58\u4e2d\u7684\u6307\u5b9a\u7684key<\/td>\n<\/tr>\n<tr>\n<td>data<\/td>\n<td>Object\/String<\/td>\n<td>\u662f<\/td>\n<td>\u9700\u8981\u5b58\u50a8\u7684\u5185\u5bb9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong> <\/p>\n<pre><code>try {\n   wx.setStorageSync(\"key\",\"value\")\n} catch (e) {\n}<\/code><\/pre>\n<h2>wx.getStorage(OBJECT)<\/h2>\n<hr>\n<p>\u4ece\u672c\u5730\u7f13\u5b58\u4e2d\u5f02\u6b65\u83b7\u53d6\u6307\u5b9akey\u5bf9\u5e94\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>OBJECT\u53c2\u6570\u8bf4\u660e\uff1a<\/strong>  <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>String<\/td>\n<td>\u662f<\/td>\n<td>\u672c\u5730\u7f13\u5b58\u4e2d\u7684\u6307\u5b9a\u7684 key<\/td>\n<\/tr>\n<tr>\n<td>success<\/td>\n<td>Function<\/td>\n<td>\u662f<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570,res = {data: key\u5bf9\u5e94\u7684\u5185\u5bb9}<\/td>\n<\/tr>\n<tr>\n<td>fail<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>complete<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>success\u8fd4\u56de\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>data<\/td>\n<td>String<\/td>\n<td>key\u5bf9\u5e94\u7684\u5185\u5bb9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>wx.getStorage({\n  key:'key',\n  success: function(res){\n      console.log(res.data)\n  } \n})<\/code><\/pre>\n<h2>wx.getStorageSync(KEY)<\/h2>\n<hr>\n<p>\u200b\u4ece\u672c\u5730\u7f13\u5b58\u4e2d\u540c\u6b65\u83b7\u53d6\u6307\u5b9akey\u5bf9\u5e94\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>String<\/td>\n<td>\u662f<\/td>\n<td>\u672c\u5730\u7f13\u5b58\u4e2d\u7684\u6307\u5b9a\u7684key<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>try {\n  var value = wx.getStorageSync('key')\n  if (value) {\n      \/\/ Do something with return value\n  }\n} catch (e) {\n  \/\/ Do something when catch error\n}<\/code>\n<\/pre>\n<h2>wx.getStorageInfo(OBJECT) <\/p>\n<hr>\n<\/h2>\n<p>\u5f02\u6b65\u83b7\u53d6\u5f53\u524dstorage\u7684\u76f8\u5173\u4fe1\u606f<\/p>\n<p><strong>OBJECT\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>success<\/td>\n<td>Function<\/td>\n<td>\u662f<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\uff0c\u8be6\u89c1\u8fd4\u56de\u53c2\u6570\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td>fail<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>complete<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>success\u8fd4\u56de\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>keys<\/td>\n<td>String Array<\/td>\n<td>\u5f53\u524dstorage\u4e2d\u6240\u6709\u7684key<\/td>\n<\/tr>\n<tr>\n<td>currentSize<\/td>\n<td>Number<\/td>\n<td>\u5f53\u524d\u5360\u7528\u7684\u7a7a\u95f4\u5927\u5c0f, \u5355\u4f4dkb<\/td>\n<\/tr>\n<tr>\n<td>limitSize<\/td>\n<td>Number<\/td>\n<td>\u9650\u5236\u7684\u7a7a\u95f4\u5927\u5c0f\uff0c\u5355\u4f4dkb<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>wx.getStorageInfo({\n  success: function(res) {\n    console.log(res.keys)\n    console.log(res.currentSize)\n    console.log(res.limitSize)\n  }\n})<\/code>\n<\/pre>\n<h2>wx.getStorageInfoSync <\/p>\n<hr>\n<\/h2>\n<p>\u540c\u6b65\u83b7\u53d6\u5f53\u524dstorage\u7684\u76f8\u5173\u4fe1\u606f<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>try {\n  var res = wx.getStorageInfoSync()\n  console.log(res.keys)\n  console.log(res.currentSize)\n  console.log(res.limitSize)\n} catch (e) {\n  \/\/ Do something when catch error\n}<\/code>\n<\/pre>\n<h2>wx.removeStorage(OBJECT) <\/p>\n<hr>\n<\/h2>\n<p>\u4ece\u672c\u5730\u7f13\u5b58\u4e2d\u5f02\u6b65\u79fb\u9664\u6307\u5b9a key \u3002<\/p>\n<p><strong>OBJECT\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>String<\/td>\n<td>\u662f<\/td>\n<td>\u672c\u5730\u7f13\u5b58\u4e2d\u7684\u6307\u5b9a\u7684 key<\/td>\n<\/tr>\n<tr>\n<td>success<\/td>\n<td>Function<\/td>\n<td>\u662f<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>fail<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>complete<\/td>\n<td>Function<\/td>\n<td>\u5426<\/td>\n<td>\u63a5\u53e3\u8c03\u7528\u7ed3\u675f\u7684\u56de\u8c03\u51fd\u6570\uff08\u8c03\u7528\u6210\u529f\u3001\u5931\u8d25\u90fd\u4f1a\u6267\u884c\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>wx.removeStorage({\n  key: 'key',\n  success: function(res) {\n    console.log(res.data)\n  } \n})<\/code>\n<\/pre>\n<h2>wx.removeStorageSync(KEY) <\/p>\n<hr>\n<\/h2>\n<p>\u4ece\u672c\u5730\u7f13\u5b58\u4e2d\u540c\u6b65\u79fb\u9664\u6307\u5b9a key \u3002<\/p>\n<p><strong>\u53c2\u6570\u8bf4\u660e\uff1a<\/strong> <\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u5fc5\u586b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>key<\/td>\n<td>String<\/td>\n<td>\u662f<\/td>\n<td>\u672c\u5730\u7f13\u5b58\u4e2d\u7684\u6307\u5b9a\u7684 key<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>try {\n  wx.removeStorageSync('key')\n} catch (e) {\n  \/\/ Do something when catch error\n}<\/code><\/pre>\n<h2>wx.clearStorage()<br \/><\/h2>\n<hr>\n<p>\u200b\u6e05\u7406\u672c\u5730\u6570\u636e\u7f13\u5b58\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>wx.clearStorage()<\/code><\/pre>\n<h2>wx.clearStorageSync()<\/h2>\n<hr>\n<p>\u540c\u6b65\u6e05\u7406\u672c\u5730\u6570\u636e\u7f13\u5b58<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong> <\/p>\n<pre><code>try {\n    wx.clearStorageSync()\n} catch(e) {\n  \/\/ Do something when catch error\n}<\/code>\n<\/pre>\n<h2>Bug &amp; Tip<\/h2>\n<ol>\n<li><code>tip<\/code>: \u672c\u5730\u6570\u636e\u5b58\u50a8\u7684\u5927\u5c0f\u9650\u5236\u4e3a 10MB<\/li>\n<\/ol>\n<p> <\/p>\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6bcf\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u6709\u81ea\u5df1\u7684\u672c\u5730\u7f13\u5b58\uff0c\u53ef\u4ee5\u901a\u8fc7wx.setStorage\uff08wx.setStorageSync\uff09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-185632","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=185632"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185632\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=185632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=185632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=185632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}