{"id":33256,"date":"2024-11-25T11:43:25","date_gmt":"2024-11-25T03:43:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33256\/"},"modified":"2024-11-25T11:43:25","modified_gmt":"2024-11-25T03:43:25","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e8%b7%a8%e9%a1%b5%e9%9d%a2%e4%ba%a4%e4%ba%92%e7%9a%84%e4%bd%9c%e7%94%a8%e5%92%8c%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33256\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u8de8\u9875\u9762\u4ea4\u4e92\u7684\u4f5c\u7528\u548c\u65b9\u6cd5"},"content":{"rendered":"<blockquote><p>\n  \u53bb\u5e74\u5e74\u672b\uff0c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u5206\u5305\u5927\u5c0f\u5df2\u7ecf\u5230\u8fbe\u4e86 12m \u5927\u5c0f,\u4e00\u65b9\u9762\u8bf4\u660e\u5c0f\u7a0b\u5e8f\u7684\u786e\u9010\u6b65\u4e3a\u5f00\u53d1\u8005\u653e\u5f00\u66f4\u5927\u7684\u6743\u9650\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u8bf4\u660e\u4e86\u5bf9\u4e8e\u67d0\u4e9b\u5c0f\u7a0b\u5e8f 8m \u7684\u5927\u5c0f\u5df2\u7ecf\u4e0d\u591f\u7528\u4e86\u3002\u6211\u4e2a\u4eba\u4eca\u5e74\u4e5f\u662f\u5728\u5f00\u53d1\u4e00\u4e2a to b \u5c0f\u7a0b\u5e8f\u5e94\u7528\u3002\u8fd9\u91cc\u5217\u4e3e\u4e00\u4e9b\u8de8\u9875\u9762\u4ea4\u4e92\u7684\u573a\u666f\u3002\u5bf9\u4e8e B \u7aef\u5e94\u7528\u7684\u4e1a\u52a1\u9700\u6c42\u6765\u8bf4\uff0c\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u7684\u590d\u6742\u5ea6\u76f8\u5bf9\u6bd4\u7f51\u9875\u5f00\u53d1\u8981\u590d\u6742\u4e00\u4e9b\u3002\u4e00\u4e2a\u662f\u53cc\u7ebf\u7a0b\u7684\u5904\u7406\u673a\u5236\u95ee\u9898\uff0c\u53e6\u4e00\u4e2a\u662f\u9875\u9762\u6808\u4e4b\u95f4\u4ea4\u4e92\u95ee\u9898\u3002\u6ce8: \u7b14\u8005\u76ee\u524d\u53ea\u9700\u8981\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u4e3a\u4e86\u5728\u5c0f\u7a0b\u5e8f\u9875\u9762\u4e2d\u53ef\u4ee5\u4f7f\u7528 properties behaviors observers \u7b49\u65b0\u529f\u80fd\uff0c\u5df2\u7ecf\u4f7f\u7528 Component \u6784\u9020\u5668\u6765\u6784\u9020\u9875\u9762\u3002\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\u5fae\u4fe1\u5c0f\u7a0b\u5e8f Component \u6784\u9020\u5668\u3002\u5982\u679c\u4f60\u4e5f\u6ca1\u6709\u591a\u7aef\u5f00\u53d1\u7684\u9700\u6c42\uff0c\u5efa\u8bae\u5c1d\u8bd5\u4f7f\u7528\uff0c\u53ef\u4ee5\u5f97\u5230\u4e0d\u9519\u7684\u4f53\u9a8c\u3002\n<\/p><\/blockquote>\n<p><strong>\u6027\u80fd\u4f18\u5316\u7c7b<\/strong><\/p>\n<hr>\n<p>\u5bf9\u4e8e\u5c0f\u7a0b\u5e8f\u5728\u9875\u9762\u4e2d\u70b9\u51fb\u89e6\u53d1 wx.navigateTo \u8df3\u8f6c\u5176\u4ed6\u9875\u9762\uff0c\u4e2d\u95f4\u4f1a\u6709\u4e00\u6bb5\u65f6\u95f4\u7684\u7a7a\u767d\u52a0\u8f7d\u671f(\u5bf9\u4e8e\u5206\u5305\u51fa\u53bb\u7684\u9875\u9762\uff0c\u7a7a\u767d\u671f\u5219\u4f1a\u66f4\u957f)\uff0c\u4f46\u662f\u8fd9\u662f\u5c0f\u7a0b\u5e8f\u5185\u90e8\u673a\u5236\uff0c\u6ca1\u6709\u529e\u6cd5\u8fdb\u884c\u4f18\u5316\u3002\u6211\u4eec\u53ea\u80fd\u773c\u7741\u7741\u7684\u7b49\u5f85\u8fd9\u6bb5\u6ca1\u6709\u610f\u601d\u7684\u7a7a\u767d\u671f\u8fc7\u53bb\u3002<\/p>\n<p>\u5f53\u8003\u8651\u5230\u8df3\u8f6c\u9875\u9762\u540e\u7684\u7b2c\u4e00\u4ef6\u4e8b\u60c5\u4fbf\u662f\u53d6\u6570\u903b\u8f91\uff0c\u90a3\u4e48\u6211\u4eec\u662f\u5426\u80fd\u591f\u8fdb\u884c\u4f18\u5316\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u5728\u5f53\u524d\u9875\u9762\u53d6\u5f97\u6570\u636e\u4e4b\u540e\u518d\u8fdb\u884c\u8df3\u8f6c\u64cd\u4f5c(\u8fd9\u6837\u64cd\u4f5c\u66f4\u4e0d\u597d)\uff0c\u4f46\u662f\u6211\u4eec\u5374\u53ef\u4ee5\u5229\u7528\u7f13\u5b58\u5f53\u524d\u7684\u8bf7\u6c42\uff0c\u8be6\u60c5\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u7684\u535a\u5ba2\u6587\u7ae0 \u2014\u2014Promise\u5bf9\u8c61 3 \u79cd\u5999\u7528\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre>const&nbsp;promiseCache&nbsp;=&nbsp;new&nbsp;Map()\n\nexport&nbsp;function&nbsp;setCachePromise(key,&nbsp;promise)&nbsp;{\n&nbsp;&nbsp;promiseCache.set(key,&nbsp;promise)\n}\n\nexport&nbsp;function&nbsp;getCachePromise(key)&nbsp;{\n&nbsp;&nbsp;\/\/&nbsp;\u6839\u636ekey\u83b7\u53d6\u5f53\u524d\u7684\u6570\u636e&nbsp;&nbsp;\n&nbsp;&nbsp;const&nbsp;promise&nbsp;=&nbsp;promiseCache.get(key)\n&nbsp;&nbsp;\/\/&nbsp;\u7528\u5b8c\u5220\u9664\uff0c\u76ee\u524d\u53ea\u505a\u4e2d\u8f6c\u7528\u9014\uff0c\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u5176\u4ed6\u7528\u9014\n&nbsp;&nbsp;promiseCache.delete(key)\n&nbsp;&nbsp;return&nbsp;promise&nbsp;&nbsp;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u505a\u4e00\u4efd\u5168\u5c40\u7684 Map,\u7136\u540e\u5229\u7528 Map \u7f13\u5b58 promise \u5bf9\u8c61\uff0c\u5728\u8df3\u8f6c\u4e4b\u524d\u4ee3\u7801\u4e3a:<\/p>\n<pre>\/\/&nbsp;\u5bfc\u5165&nbsp;setCachePromise&nbsp;\u51fd\u6570\n\nComponent({\n&nbsp;&nbsp;methods:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;getBookData(id)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;promise&nbsp;=&nbsp;\/\/&nbsp;promise&nbsp;\u8bf7\u6c42\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setCachePromise(`xxx:${id}`,&nbsp;promise)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;handleBookDetailShow(e)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;id&nbsp;=&nbsp;e.detail\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.getBookData(id)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wx.navigateTo({url:&nbsp;`xx\/xx\/x?id=${id}`})\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u800c\u8df3\u8f6c\u4e4b\u540e\u7684\u4ee3\u7801\u4e5f\u5982\u4e0b\u6240\u793a:<\/p>\n<pre>\/\/&nbsp;\u5bfc\u5165&nbsp;getCachePromise&nbsp;\u51fd\u6570\n\nComponent({\n&nbsp;&nbsp;&nbsp;&nbsp;properties:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id:&nbsp;Number&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;lifetimes:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attached&nbsp;()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;id&nbsp;=&nbsp;this.data.id&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u53d6\u5f97\u5168\u5c40\u7f13\u5b58\u7684promise\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;bookPromise&nbsp;=&nbsp;getCachePromise(`xxx:${id}`)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bookPromise.then((res)&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u4e1a\u52a1\u5904\u7406\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}).catch(error&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u9519\u8bef\u5904\u7406&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;methods:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getBook()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u83b7\u53d6\u6570\u636e\uff0c\u4ee5\u4fbf\u4e8e&nbsp;\u9519\u8bef\u5904\u7406&nbsp;\u4e0a\u62c9\u5237\u65b0&nbsp;\u7b49\u64cd\u4f5c&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u6b64\u4fbf\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u53d6\u6570\u548c\u9875\u9762\u52a0\u8f7d\u7684\u903b\u8f91\uff0c\u5f53\u7136\uff0c\u8fd9\u4e2a\u5bf9\u4e8e\u9875\u9762\u6709\u8026\u5408\u6027\uff0c\u4e0d\u5229\u4e8e\u540e\u7eed\u7684\u5220\u9664\u4e0e\u4fee\u6539\u3002\u4f46\u8003\u8651\u5982\u679c\u4ec5\u4ec5\u52a0\u5728\u5206\u5305\u8df3\u8f6c\u4e4b\u95f4\u53ef\u80fd\u4f1a\u6709\u4e0d\u9519\u7684\u6548\u679c\u3002<\/p>\n<p>\u60f3\u8981\u65e0\u4fb5\u5165\u5f0f\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5b66\u4e60\u7814\u7a76&nbsp;\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u63d0\u9ad8\u5e94\u7528\u901f\u5ea6\u5c0f\u6280\u5de7&nbsp;\u4ee5\u53ca&nbsp;wxpage&nbsp;\u6846\u67b6\uff0c\u540c\u65f6\u8003\u8651\u5230\u65e0\u8bba\u662f ToC \u8fd8\u662f ToC \u7528\u6237\uff0c\u90fd\u6709\u53ef\u80fd\u5b58\u5728\u786c\u4ef6\u4ee5\u53ca\u7f51\u7edc\u73af\u5883\u7b49\u95ee\u9898\uff0c\u8be5\u4f18\u5316\u8fd8\u662f\u975e\u5e38\u503c\u5f97\u7684\u3002<\/p>\n<p>\u5f53\u7136\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e3a\u4e86\u51cf\u5c11\u51b7\u542f\u52a8\u65f6\u95f4\uff0c\u63d0\u4f9b\u4e86\u5468\u671f\u6027\u66f4\u65b0&nbsp;\u6570\u636e\u9884\u62c9\u53d6&nbsp;\u529f\u80fd\u3002<\/p>\n<p>\u6ce8: \u4e0a\u9762\u7684 promiseCache \u53ea\u4f5c\u4e3a\u4e2d\u8f6c\u7684\u7528\u9014\uff0c\u4e0d\u4f5c\u4e3a\u7f13\u5b58\u7684\u7528\u9014\uff0c\u5982\u679c\u4f60\u8003\u8651\u6dfb\u52a0\u7f13\u5b58\uff0c\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u7684\u535a\u5ba2\u6587\u7ae0\u2014\u2014&nbsp;\u524d\u7aef api \u8bf7\u6c42\u7f13\u5b58\u65b9\u6848\u3002<\/p>\n<p><strong>\u901a\u77e5\u7c7b<\/strong><\/p>\n<hr>\n<p>\u5982\u679c\u662f pc \u7aef\u4e2d\u8fdb\u884c\u4ea4\u4e92\uff0c\u5bf9\u4e8e\u6570\u636e\u7684 CRUD\u3002\u4f8b\u5982\u5728\u8be6\u60c5\u9875\u9762\u8fdb\u884c\u4e86\u6570\u636e\u7684\u4fee\u6539\u548c\u5220\u9664\uff0c\u8fd4\u56de\u5217\u8868\u65f6\u5019\u5c31\u76f4\u63a5\u8c03\u53d6\u4e4b\u524d\u5b58\u50a8\u7684\u5217\u8868\u67e5\u8be2\u6761\u4ef6\u518d\u6b21\u67e5\u8be2\u5373\u53ef\uff0c\u800c\u5bf9\u4e8e\u79fb\u52a8\u7aef\u8fd9\u79cd\u4e0b\u62c9\u6eda\u52a8\u7684\u8bbe\u8ba1\uff0c\u5c31\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u8c03\u7528\u4e4b\u524d\u7684\u67e5\u8be2\u6761\u4ef6\u6765\u8fdb\u884c\u641c\u7d22\u3002<\/p>\n<p>\u5982\u679c\u4ece\u5217\u8868\u9875\u9762\u8fdb\u5165\u8be6\u60c5\u9875\u9762\u540e\uff0c\u5728\u8be6\u60c5\u9875\u9762\u53ea\u4f1a\u8fdb\u884c\u6dfb\u52a0\u6216\u8005\u4fee\u6539\u64cd\u4f5c\u3002\u7136\u540e\u8fd4\u56de\u5217\u8868\u9875\u9762\u3002\u6b64\u65f6\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u6570\u636e\u5df2\u7ecf\u8fdb\u884c\u4e86\u4fee\u6539\uff0c\u8bf7\u7528\u6237\u81ea\u884c\u51b3\u5b9a\u662f\u5426\u8fdb\u884c\u5237\u65b0\u64cd\u4f5c\u3002<\/p>\n<p>\u5982\u5728\u7f16\u8f91\u9875\u9762\u4fee\u6539\u4e86\u6570\u636e:<\/p>\n<pre>const&nbsp;app&nbsp;=&nbsp;getApp()\n\ncomponent({\n&nbsp;&nbsp;methods:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;async&nbsp;handleSave()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/...\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app.globalData.xxxChanged&nbsp;=&nbsp;true\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/...&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5217\u8868\u754c\u9762:<\/p>\n<pre>const&nbsp;app&nbsp;=&nbsp;getApp()\n\ncomponent({\n&nbsp;&nbsp;pageLifetimes:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;show()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.confirmThenRefresh()\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;},\n&nbsp;&nbsp;methods:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;confirmThenRefresh()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u68c0\u67e5&nbsp;globalData\uff0c\u5982\u679c\u5f53\u524d\u6ca1\u6709\u8fdb\u884c\u4fee\u6539\uff0c\u76f4\u63a5\u8fd4\u56de&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(!app.globalData.xxxChanged)&nbsp;return\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wx.showModal({\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;...\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;complete:&nbsp;()&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u65e0\u8bba\u786e\u8ba4\u5237\u65b0\u4e0e\u5426\uff0c\u90fd\u628a\u6570\u636e\u7f6e\u4e3a&nbsp;false&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app.globalData.xxxChanged&nbsp;=&nbsp;false&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}&nbsp;&nbsp;\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5f53\u7136\u4e86\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5229\u7528 wx.setStorage \u6216\u8005 getCurrentPage \u83b7\u53d6\u524d\u9762\u7684\u9875\u9762 setData \u6765\u8fdb\u884c\u6570\u636e\u901a\u77e5,\u4ee5\u4fbf\u7528\u6237\u8fdb\u884c\u9875\u9762\u5237\u65b0\u3002<\/p>\n<p><strong>\u8ba2\u9605\u53d1\u5e03\u7c7b<\/strong><\/p>\n<hr>\n<p>\u5982\u679c\u4ec5\u4ec5\u53ea\u6d89\u53ca\u5230\u4fee\u6539\u6570\u636e\u7684\u524d\u63d0\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u8ba9\u7528\u6237\u8fdb\u884c\u5237\u65b0\u64cd\u4f5c\uff0c\u4f46\u662f\u5982\u679c\u9488\u5bf9\u4e8e\u5220\u9664\u64cd\u4f5c\u800c\u8a00\uff0c\u5982\u679c\u7528\u6237\u9009\u62e9\u4e0d\u8fdb\u884c\u5237\u65b0\uff0c\u7136\u540e\u7528\u6237\u53c8\u4e0d\u5c0f\u5fc3\u70b9\u51fb\u5230\u4e86\u5df2\u7ecf\u88ab\u5220\u9664\u7684\u6570\u636e\uff0c\u5c31\u4f1a\u53d1\u751f\u9519\u8bef\u3002\u6240\u4ee5\u5982\u679c\u6709\u5220\u9664\u7684\u9700\u6c42\uff0c\u6211\u4eec\u6700\u597d\u5728\u8fd4\u56de\u5217\u8868\u9875\u9762\u524d\u5c31\u8fdb\u884c\u5217\u8868\u7684\u4fee\u6539\uff0c\u4ee5\u514d\u9020\u6210\u9519\u8bef\u3002<\/p>\n<p><strong><span style=\"font-family: -apple-system, BlinkMacSystemFont,\">mitt<\/span><\/strong><\/p>\n<p>github \u4e0a\u6709\u5f88\u591a\u7684 pub\/sub \u5f00\u6e90\u5e93\uff0c\u5982\u679c\u6ca1\u6709\u4ec0\u4e48\u7279\u5b9a\u7684\u9700\u6c42\uff0c\u627e\u5230\u4ee3\u7801\u91cf\u6700\u5c11\u7684\u5c31\u662f&nbsp;mitt&nbsp;\u8fd9\u4e2a\u5e93\u4e86,\u4f5c\u8005\u662f\u559c\u6b22\u5f00\u53d1\u5fae\u578b\u5e93\u7684&nbsp;developit&nbsp;\u5927\u4f6c,\u8457\u540d\u7684&nbsp;preact&nbsp;\u4e5f\u662f\u51fa\u4e8e\u8fd9\u4f4d\u5927\u4f6c\u4e4b\u624b\u3002 \u8fd9\u91cc\u5c31\u4e0d\u505a\u8fc7\u591a\u7684\u4ecb\u7ecd\uff0c\u975e\u5e38\u7b80\u5355\u3002\u5927\u5bb6\u53ef\u80fd\u90fd\u80fd\u770b\u660e\u767d\uff0c\u4ee3\u7801\u5982\u4e0b(\u9664\u53bb flow \u5de5\u5177\u7684\u68c0\u67e5):<\/p>\n<pre>export&nbsp;default&nbsp;function&nbsp;mitt(all)&nbsp;{\n&nbsp;&nbsp;all&nbsp;=&nbsp;all&nbsp;||&nbsp;Object.create(null);\n\n&nbsp;&nbsp;return&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;on(type,&nbsp;handler)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(all[type]&nbsp;||&nbsp;(all[type]&nbsp;=&nbsp;[])).push(handler);\n&nbsp;&nbsp;&nbsp;&nbsp;},\n\n&nbsp;&nbsp;&nbsp;&nbsp;off(type,&nbsp;handler)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(all[type])&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all[type].splice(all[type].indexOf(handler)&nbsp;&amp;gt;&amp;gt;&amp;gt;&nbsp;0,&nbsp;1);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;emit(type,&nbsp;evt)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(all[type]&nbsp;||&nbsp;[]).slice().map((handler)&nbsp;=&amp;gt;&nbsp;{&nbsp;handler(evt);&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(all['*']&nbsp;||&nbsp;[]).slice().map((handler)&nbsp;=&amp;gt;&nbsp;{&nbsp;handler(type,&nbsp;evt);&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;};\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ec5\u4ec5\u53ea\u67093\u4e2a\u65b9\u6cd5\uff0con emit\u4ee5\u53ca off\u3002<\/p>\n<p>\u53ea\u8981\u5728\u591a\u4e2a\u9875\u9762\u5bfc\u5165 \u751f\u6210\u7684 mitt() \u51fd\u6570\u751f\u6210\u7684\u5bf9\u8c61\u5373\u53ef(\u6216\u8005\u76f4\u63a5\u653e\u5165 app.globalData \u4e2d\u4e5f\u53ef)\u3002<\/p>\n<pre>Component({\n&nbsp;&nbsp;lifetimes:&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;attached:&nbsp;function()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u521b\u5efa\u65f6\u6267\u884c\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;changeData&nbsp;=&nbsp;(type,&nbsp;data)&nbsp;=&amp;gt;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5904\u7406\u4f20\u9012\u8fc7\u6765\u7684\u7c7b\u578b\u4e0e\u6570\u636e\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this._changed&nbsp;=&nbsp;changeData\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bus.on('xxxchanged',&nbsp;this._changed)\n&nbsp;&nbsp;&nbsp;&nbsp;},\n&nbsp;&nbsp;&nbsp;&nbsp;detached:&nbsp;function()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u9875\u9762\u9500\u6bc1\u65f6\u6267\u884c\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bus.off('xxxchanged',&nbsp;this._changed)\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;}\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91ccmitt\u53ef\u4ee5\u6709\u591a\u4e2a\u9875\u9762\u8fdb\u884c\u7ed1\u5b9a\u4e8b\u4ef6\uff0c\u5982\u679c\u9700\u6c42\u4ec5\u4ec5\u53ea\u6d89\u53ca\u5230\u4e24\u4e2a\u9875\u9762\u4e4b\u95f4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 wx.navigateTo \u4e2d\u7684 EventChannel (\u9875\u9762\u95f4\u4e8b\u4ef6\u4fe1\u606f\u901a\u9053)\u3002\u53ef\u4ee5\u53c2\u8003\u5fae\u4fe1\u5c0f\u7a0b\u5e8fwx.navigateTo\u65b9\u6cd5\u91cc\u7684events\u53c2\u6570\u4f7f\u7528\u8be6\u60c5\u53ca\u573a\u666f,\u8be5\u65b9\u6848\u7684\u5229\u597d\u5728\u4e8e\uff0c\u4f20\u9012\u5230\u4e0b\u4e00\u4e2a\u9875\u9762\u7684\u53c2\u6570\u4e5f\u53ef\u4ee5\u901a\u8fc7 EventChannel \u6765\u901a\u77e5\uff0c\u4ee5\u4fbf\u4e8e\u89e3\u51b3 properties \u4f20\u9012\u6570\u636e\u4e0d\u5b9c\u8fc7\u5927\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6ce8: \u4e00\u4e2a\u9875\u9762\u5c55\u793a\u5f88\u591a\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u4f1a\u9020\u6210\u5c0f\u7a0b\u5e8f\u9875\u9762\u7684\u5361\u987f\u4ee5\u53ca\u767d\u5c4f\u3002\u5c0f\u7a0b\u5e8f\u5b98\u65b9\u4e5f\u6709\u957f\u5217\u8868\u7ec4\u4ef6&nbsp;recycle-view\u3002\u6709\u9700\u6c42\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u81ea\u884c\u7814\u7a76\uff0c\u8fd9\u4e2a\u4e0d\u5728\u8fd9\u91cc\u8be6\u8ff0\u3002<\/p>\n<p>\u9f13\u52b1\u4e00\u4e0b<\/p>\n<p>\u5982\u679c\u4f60\u89c9\u5f97\u8fd9\u7bc7\u6587\u7ae0\u4e0d\u9519\uff0c\u5e0c\u671b\u53ef\u4ee5\u7ed9\u4e0e\u6211\u4e00\u4e9b\u9f13\u52b1\uff0c\u5728\u6211\u7684 github \u535a\u5ba2\u4e0b\u5e2e\u5fd9 star \u4e00\u4e0b\u3002<\/p>\n<p>\u63a8\u8350\u6559\u7a0b\uff1a\u300a\u300b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u8de8\u9875\u9762\u4ea4\u4e92\u7684\u4f5c\u7528\u548c\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53bb\u5e74\u5e74\u672b\uff0c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u5206\u5305\u5927\u5c0f\u5df2\u7ecf\u5230\u8fbe\u4e86 12m \u5927\u5c0f,\u4e00\u65b9\u9762\u8bf4\u660e\u5c0f\u7a0b\u5e8f\u7684\u786e\u9010\u6b65\u4e3a\u5f00\u53d1\u8005\u653e\u5f00\u66f4\u5927\u7684\u6743\u9650\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u8bf4\u660e\u4e86\u5bf9\u4e8e\u67d0\u4e9b\u5c0f\u7a0b\u5e8f 8m \u7684\u5927\u5c0f\u5df2\u7ecf\u4e0d\u591f\u7528\u4e86\u3002\u6211\u4e2a\u4eba\u4eca\u5e74\u4e5f\u662f\u5728\u5f00\u53d1\u4e00\u4e2a to b \u5c0f\u7a0b\u5e8f\u5e94\u7528\u3002\u8fd9\u91cc\u5217\u4e3e\u4e00\u4e9b\u8de8\u9875\u9762\u4ea4\u4e92\u7684\u573a\u666f\u3002\u5bf9\u4e8e B \u7aef\u5e94\u7528\u7684\u4e1a\u52a1\u9700\u6c42\u6765\u8bf4\uff0c\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u7684\u590d\u6742\u5ea6\u76f8\u5bf9\u6bd4\u7f51\u9875\u5f00\u53d1\u8981\u590d\u6742\u4e00\u4e9b\u3002\u4e00\u4e2a\u662f\u53cc\u7ebf\u7a0b\u7684\u5904\u7406\u673a\u5236\u95ee\u9898\uff0c\u53e6\u4e00\u4e2a\u662f\u9875\u9762\u6808\u4e4b\u95f4\u4ea4\u4e92\u95ee\u9898\u3002\u6ce8: \u7b14\u8005\u76ee\u524d\u53ea\u9700\u8981\u5f00\u53d1\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\uff0c\u4e3a\u4e86\u5728\u5c0f\u7a0b\u5e8f\u9875\u9762\u4e2d\u53ef\u4ee5\u4f7f\u7528 properties behaviors observers \u7b49\u65b0\u529f\u80fd\uff0c\u5df2\u7ecf\u4f7f\u7528 Component \u6784\u9020\u5668\u6765\u6784\u9020\u9875\u9762\u3002\u5177\u4f53\u53ef\u4ee5\u53c2\u8003\u5fae\u4fe1\u5c0f\u7a0b\u5e8f Component \u6784\u9020\u5668\u3002\u5982\u679c\u4f60\u4e5f\u6ca1\u6709\u591a\u7aef\u5f00\u53d1\u7684\u9700\u6c42\uff0c\u5efa\u8bae\u5c1d\u8bd5\u4f7f\u7528\uff0c\u53ef\u4ee5\u5f97\u5230\u4e0d\u9519\u7684\u4f53\u9a8c\u3002 \u6027\u80fd\u4f18\u5316\u7c7b \u5bf9\u4e8e\u5c0f\u7a0b\u5e8f\u5728\u9875\u9762\u4e2d\u70b9\u51fb\u89e6\u53d1 wx.navigateTo \u8df3\u8f6c\u5176\u4ed6\u9875\u9762\uff0c\u4e2d\u95f4\u4f1a\u6709\u4e00\u6bb5\u65f6\u95f4\u7684\u7a7a\u767d\u52a0\u8f7d\u671f(\u5bf9\u4e8e\u5206\u5305\u51fa\u53bb\u7684\u9875\u9762\uff0c\u7a7a\u767d\u671f\u5219\u4f1a\u66f4\u957f)\uff0c\u4f46\u662f\u8fd9\u662f\u5c0f\u7a0b\u5e8f\u5185\u90e8\u673a\u5236\uff0c\u6ca1\u6709\u529e\u6cd5\u8fdb\u884c\u4f18\u5316\u3002\u6211\u4eec\u53ea\u80fd\u773c\u7741\u7741\u7684\u7b49\u5f85\u8fd9\u6bb5\u6ca1\u6709\u610f\u601d\u7684\u7a7a\u767d\u671f\u8fc7\u53bb\u3002 \u5f53\u8003\u8651\u5230\u8df3\u8f6c\u9875\u9762\u540e\u7684\u7b2c\u4e00\u4ef6\u4e8b\u60c5\u4fbf\u662f\u53d6\u6570\u903b\u8f91\uff0c\u90a3\u4e48\u6211\u4eec\u662f\u5426\u80fd\u591f\u8fdb\u884c\u4f18\u5316\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002\u6211\u4eec\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5\u5728\u5f53\u524d\u9875\u9762\u53d6\u5f97\u6570\u636e\u4e4b\u540e\u518d\u8fdb\u884c\u8df3\u8f6c\u64cd\u4f5c(\u8fd9\u6837\u64cd\u4f5c\u66f4\u4e0d\u597d)\uff0c\u4f46\u662f\u6211\u4eec\u5374\u53ef\u4ee5\u5229\u7528\u7f13\u5b58\u5f53\u524d\u7684\u8bf7\u6c42\uff0c\u8be6\u60c5\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u7684\u535a\u5ba2\u6587\u7ae0 \u2014\u2014Promise\u5bf9\u8c61 3 \u79cd\u5999\u7528\u3002 \u4ee3\u7801\u5982\u4e0b: const&nbsp;promiseCache&nbsp;=&nbsp;new&nbsp;Map() export&nbsp;function&nbsp;setCachePromise(key,&nbsp;promise)&nbsp;{ &nbsp;&nbsp;promiseCache.set(key,&nbsp;promise) } export&nbsp;function&nbsp;getCachePromise(key)&nbsp;{ &nbsp;&nbsp;\/\/&nbsp;\u6839\u636ekey\u83b7\u53d6\u5f53\u524d\u7684\u6570\u636e&nbsp;&nbsp; &nbsp;&nbsp;const&nbsp;promise&nbsp;=&nbsp;promiseCache.get(key) &nbsp;&nbsp;\/\/&nbsp;\u7528\u5b8c\u5220\u9664\uff0c\u76ee\u524d\u53ea\u505a\u4e2d\u8f6c\u7528\u9014\uff0c\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u5176\u4ed6\u7528\u9014 &nbsp;&nbsp;promiseCache.delete(key) &nbsp;&nbsp;return&nbsp;promise&nbsp;&nbsp; } \u767b\u5f55\u540e\u590d\u5236 \u505a\u4e00\u4efd\u5168\u5c40\u7684 Map,\u7136\u540e\u5229\u7528 Map \u7f13\u5b58 promise \u5bf9\u8c61\uff0c\u5728\u8df3\u8f6c\u4e4b\u524d\u4ee3\u7801\u4e3a: \/\/&nbsp;\u5bfc\u5165&nbsp;setCachePromise&nbsp;\u51fd\u6570 Component({ &nbsp;&nbsp;methods:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;getBookData(id)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;promise&nbsp;=&nbsp;\/\/&nbsp;promise&nbsp;\u8bf7\u6c42 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setCachePromise(`xxx:${id}`,&nbsp;promise)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;handleBookDetailShow(e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;id&nbsp;=&nbsp;e.detail [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-33256","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=33256"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33256\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}