{"id":9547,"date":"2024-01-22T11:22:09","date_gmt":"2024-01-22T03:22:09","guid":{"rendered":"https:\/\/sdeno.com\/?p=9547"},"modified":"2024-01-22T11:58:17","modified_gmt":"2024-01-22T03:58:17","slug":"%e6%9c%ac%e5%9c%b0%e5%ad%98%e5%82%a8localforage%e5%8f%af%e4%bb%a5%e5%ad%98%e5%82%a8%e5%a4%a7%e9%87%8f%e6%9c%ac%e5%9c%b0%e7%a6%bb%e7%ba%bf%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=9547","title":{"rendered":"\u672c\u5730\u5b58\u50a8localForage\u53ef\u4ee5\u5b58\u50a8\u5927\u91cf\u672c\u5730\u79bb\u7ebf\u6570\u636e"},"content":{"rendered":"<p>localForage \u662f\u4e00\u4e2a JavaScript \u5e93\u3002localForage \u6709\u4e00\u4e2a\u4f18\u96c5\u964d\u7ea7\u7b56\u7565\uff0c\u4f1a\u4f18\u5148\u7ea7\u4f7f\u7528 IndexedDB\u5176\u6b21WebSQL\uff0c\u5982\u679c\u4e0d\u652f\u6301\u5219\u4f7f\u7528 localStorage\u3002<\/p>\n<p>\u65e2\u7136\u8981\u5b58\u50a8\u7684\u6570\u91cf\u5927\uff0c\u5f97\u6392\u9664cookie<br \/>\nlocalStorage\uff0c\u867d\u7136\u6bd4cookie\u591a\uff0c\u4f46\u662f\u540c\u6837\u6709\u4e0a\u9650\uff085M\uff09\u5de6\u53f3\uff0c\u5907\u9009<br \/>\nwebsql \u4f7f\u7528\u7b80\u5355\uff0c\u5b58\u50a8\u91cf\u5927\uff0c\u517c\u5bb9\u6027\u5dee\uff0c\u5907\u9009<br \/>\nindexDB api\u591a\u4e14\u7e41\u7410\uff0c\u5b58\u50a8\u91cf\u5927\u3001\u9ad8\u7248\u672c\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\u8f83\u597d\uff0c\u7406\u8bba\u4e0a\u662f\u786c\u4ef6\u6709\u591a\u5927\u5185\u5b58\u5c31\u53ef\u4ee5\u5b58\u591a\u5c11\uff0c\u57fa\u672c\u6700\u5c0f\u662f250M\u8d77\u6b65\uff0c\u5907\u9009<\/p>\n<p>\u6240\u4ee5\u5728\u6240\u6709\u4e3b\u6d41\u6d4f\u89c8\u5668\u4e2d\u90fd\u53ef\u7528\uff1aChrome\uff0cFirefox\uff0cIE \u548c Safari\uff08\u5305\u62ec Safari Mobile\uff09\u3002\u4e0b\u9762\u662f indexDB\u3001web sql\u3001localStorage \u7684\u4e00\u4e2a\u6d4f\u89c8\u5668\u652f\u6301\u60c5\u51b5\uff0c\u53ef\u4ee5\u53d1\u73b0\uff0c\u517c\u5bb9\u6027\u65b9\u9762loaclForage\u57fa\u672c\u4e0a\u6ee1\u8db399%\u9700\u6c42\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u5b89\u88c5\uff1a<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">npm install localforage\r\n\u6216\u8005\r\n\/\/ \u76f4\u63a5\u5f15\u7528\r\n&lt;script src=\"localforage.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;console.log('localforage is: ', localforage);&lt;\/script&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u83b7\u53d6\u503c<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">localforage.getItem('somekey').then(function(value) {\r\n    \/\/ \u5f53\u79bb\u7ebf\u4ed3\u5e93\u4e2d\u7684\u503c\u88ab\u8f7d\u5165\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log(value);\r\n}).catch(function(err) {\r\n    \/\/ \u5f53\u51fa\u9519\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log(err);\r\n});\r\n\r\n\/\/ \u56de\u8c03\u7248\u672c\uff1a\r\nlocalforage.getItem('somekey', function(err, value) {\r\n    \/\/ \u5f53\u79bb\u7ebf\u4ed3\u5e93\u4e2d\u7684\u503c\u88ab\u8f7d\u5165\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log(value);\r\n});\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u8bbe\u7f6e\u503c<\/span><\/p>\n<p>\u5b58\u50a8\u7684\u6570\u636e\u7c7b\u578b<\/p>\n<p>\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u79bb\u7ebf\u4ed3\u5e93\u3002\u4f60\u53ef\u4ee5\u5b58\u50a8\u5982\u4e0b\u7c7b\u578b\u7684 JavaScript \u5bf9\u8c61\uff1a<\/p>\n<p>Array<br \/>\nArrayBuffer<br \/>\nBlob<br \/>\nFloat32Array<br \/>\nFloat64Array<br \/>\nInt8Array<br \/>\nInt16Array<br \/>\nInt32Array<br \/>\nNumber<br \/>\nObject<br \/>\nUint8Array<br \/>\nUint8ClampedArray<br \/>\nUint16Array<br \/>\nUint32Array<br \/>\nString<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/\u5b57\u7b26\u4e32\r\nlocalforage\r\n  .setItem(\"somekey\", \"some value\")\r\n  .then(function (value) {\r\n    \/\/ \u5f53\u503c\u88ab\u5b58\u50a8\u540e\uff0c\u53ef\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\r\n    console.log(value);\r\n  })\r\n  .catch(function (err) {\r\n    \/\/ \u5f53\u51fa\u9519\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log(err);\r\n  });\r\n\r\n\r\n\/\/\u6570\u7ec4\r\n\/\/ \u4e0d\u540c\u4e8e localStorage\uff0c\u4f60\u53ef\u4ee5\u5b58\u50a8\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\r\nlocalforage\r\n  .setItem(\"my array\", [1, 2, \"three\"])\r\n  .then(function (value) {\r\n    \/\/ \u5982\u4e0b\u8f93\u51fa `1`\r\n    console.log(value[0]);\r\n  })\r\n  .catch(function (err) {\r\n    \/\/ \u5f53\u51fa\u9519\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log(err);\r\n  });\r\n\r\n\r\n\/\/\u6570\u636e\u6d41\r\n\/\/ \u4f60\u751a\u81f3\u53ef\u4ee5\u5b58\u50a8 AJAX \u54cd\u5e94\u8fd4\u56de\u7684\u4e8c\u8fdb\u5236\u6570\u636e\r\nreq = new XMLHttpRequest();\r\nreq.open(\"GET\", \"\/photo.jpg\", true);\r\nreq.responseType = \"arraybuffer\";\r\n\r\nreq.addEventListener(\"readystatechange\", function () {\r\n  if (req.readyState === 4) {\r\n    \/\/ readyState \u5b8c\u6210\r\n    localforage\r\n      .setItem(\"photo\", req.response)\r\n      .then(function (image) {\r\n        \/\/ \u5982\u4e0b\u4e3a\u4e00\u4e2a\u5408\u6cd5\u7684 &lt;img&gt; \u6807\u7b7e\u7684 blob URI\r\n        var blob = new Blob([image]);\r\n        var imageURI = window.URL.createObjectURL(blob);\r\n      })\r\n      .catch(function (err) {\r\n        \/\/ \u5f53\u51fa\u9519\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n        console.log(err);\r\n      });\r\n  }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u5220\u9664\u5b58\u50a8<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">localforage.removeItem('somekey').then(function() {\r\n    \/\/ \u5f53\u503c\u88ab\u79fb\u9664\u540e\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log('Key is cleared!');\r\n}).catch(function(err) {\r\n    \/\/ \u5f53\u51fa\u9519\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log(err);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u6e05\u7a7a\u5b58\u50a8<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">localforage.clear().then(function() {\r\n    \/\/ \u5f53\u6570\u636e\u5e93\u88ab\u5168\u90e8\u5220\u9664\u540e\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log('Database is now empty.');\r\n}).catch(function(err) {\r\n    \/\/ \u5f53\u51fa\u9519\u65f6\uff0c\u6b64\u5904\u4ee3\u7801\u8fd0\u884c\r\n    console.log(err);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/localforage.docschina.org\/\" target=\"_blank\" rel=\"noopener\">https:\/\/localforage.docschina.org\/<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>localForage \u662f\u4e00\u4e2a JavaScript \u5e93\u3002localForage \u6709\u4e00\u4e2a\u4f18\u96c5\u964d\u7ea7\u7b56\u7565\uff0c\u4f1a\u4f18\u5148\u7ea7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9547","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/9547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9547"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/9547\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}