{"id":29714,"date":"2024-11-25T09:44:01","date_gmt":"2024-11-25T01:44:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/29714\/"},"modified":"2024-11-25T09:44:01","modified_gmt":"2024-11-25T01:44:01","slug":"%e6%95%99%e4%bd%a0%e7%94%a8app%e5%87%bd%e6%95%b0%e6%b3%a8%e5%86%8c%e7%a8%8b%e5%ba%8f%ef%bc%88%e5%be%ae%e4%bf%a1%e7%a8%8b%e5%ba%8f%e5%bc%80%e5%8f%91%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/29714\/","title":{"rendered":"\u6559\u4f60\u7528app()\u51fd\u6570\u6ce8\u518c\u7a0b\u5e8f\uff08\u5fae\u4fe1\u7a0b\u5e8f\u5f00\u53d1\uff09"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6ce8\u518c\u7a0b\u5e8f\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p>\u7cfb\u5217\u6587\u7ae0\uff1a<\/p>\n<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6a21\u5757\u5316<\/p>\n<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6ce8\u518c\u9875\u9762<\/p>\n<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6ce8\u518c\u7a0b\u5e8f<\/p>\n<p><strong>App()<\/strong><\/p>\n<p>App()\u51fd\u6570\u7528\u6765\u6ce8\u518c\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\u3002\u63a5\u53d7\u4e00\u4e2aobject\u53c2\u6570\uff0c\u5176\u6307\u5b9a\u5c0f\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u51fd\u6570\u7b49\u3002<\/p>\n<p><strong>object\u53c2\u6570\u8bf4\u660e\uff1a<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u5c5e\u6027<\/th>\n<th>\u7c7b\u578b<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u89e6\u53d1\u65f6\u673a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>onLaunch<\/td>\n<td>Function<\/td>\n<td>\u751f\u547d\u5468\u671f\u51fd\u6570&#8211;\u76d1\u542c\u5c0f\u7a0b\u5e8f\u521d\u59cb\u5316<\/td>\n<td>\u5f53\u5c0f\u7a0b\u5e8f\u521d\u59cb\u5316\u5b8c\u6210\u65f6\uff0c\u4f1a\u89e6\u53d1onLaunch\uff08\u5168\u5c40\u53ea\u89e6\u53d1\u4e00\u6b21\uff09<\/td>\n<\/tr>\n<tr>\n<td>onShow<\/td>\n<td>Function<\/td>\n<td>\u751f\u547d\u5468\u671f\u51fd\u6570&#8211;\u76d1\u542c\u5c0f\u7a0b\u5e8f\u663e\u793a<\/td>\n<td>\u5f53\u5c0f\u7a0b\u5e8f\u542f\u52a8\uff0c\u6216\u4ece\u540e\u53f0\u8fdb\u5165\u524d\u53f0\u663e\u793a\uff0c\u4f1a\u89e6\u53d1onShow<\/td>\n<\/tr>\n<tr>\n<td>onHide<\/td>\n<td>Function<\/td>\n<td>\u751f\u547d\u5468\u671f\u51fd\u6570&#8211;\u76d1\u542c\u5c0f\u7a0b\u5e8f\u9690\u85cf<\/td>\n<td>\u5f53\u5c0f\u7a0b\u5e8f\u4ece\u524d\u53f0\u8fdb\u5165\u540e\u53f0\uff0c\u4f1a\u89e6\u53d1onHide<\/td>\n<\/tr>\n<tr>\n<td>\u5176\u4ed6<\/td>\n<td>Any<\/td>\n<td>\u5f00\u53d1\u8005\u53ef\u4ee5\u6dfb\u52a0\u4efb\u610f\u7684\u51fd\u6570\u6216\u6570\u636e\u5230Object\u53c2\u6570\u4e2d\uff0c\u7528this\u53ef\u4ee5\u8bbf\u95ee<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u524d\u53f0\u3001\u540e\u53f0\u5b9a\u4e49\uff1a<\/strong> \u5f53\u7528\u6237\u70b9\u51fb\u5de6\u4e0a\u89d2\u5173\u95ed\uff0c\u6216\u8005\u6309\u4e86\u8bbe\u5907Home\u952e\u79bb\u5f00\u5fae\u4fe1\uff0c\u5c0f\u7a0b\u5e8f\u5e76\u6ca1\u6709\u6b63\u5728\u7684\u9500\u6bc1\uff0c\u800c\u662f\u8fdb\u5165\u4e86\u540e\u53f0\uff1b\u5f53\u518d\u6b21\u542f\u52a8\u5fae\u4fe1\u6216\u518d\u6b21\u6253\u5f00\u5c0f\u7a0b\u5e8f\uff0c\u53c8\u4f1a\u4ece\u540e\u53f0\u8fdb\u5165\u524d\u53f0\u3002<\/p>\n<p>\u53ea\u6709\u5f53\u5c0f\u7a0b\u5e8f\u8fdb\u5165\u540e\u53f0\u4e00\u5b9a\u65f6\u95f4\uff0c\u6216\u8005\u7cfb\u7edf\u8d44\u6e90\u5360\u7528\u8fc7\u9ad8\uff0c\u624d\u4f1a\u88ab\u771f\u6b63\u7684\u9500\u6bc1\u3002<\/p>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>App({\r\n&nbsp;onLaunch:&nbsp;function()&nbsp;{&nbsp;\r\n&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;initial&nbsp;when&nbsp;launch.\r\n&nbsp;},\r\n&nbsp;onShow:&nbsp;function()&nbsp;{\r\n&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;show.\r\n&nbsp;},\r\n&nbsp;onHide:&nbsp;function()&nbsp;{\r\n&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;hide.\r\n&nbsp;},\r\n&nbsp;globalData:&nbsp;'I&nbsp;am&nbsp;global&nbsp;data'\r\n})<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>App.prototype.getCurrentPage()<\/strong><\/p>\n<p>getCurrentPage()\u51fd\u6570\u7528\u6237\u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684\u5b9e\u4f8b\u3002<\/p>\n<p><strong>getApp()<\/strong><\/p>\n<p>\u6211\u4eec\u63d0\u4f9b\u4e86\u5168\u5c40\u7684getApp()\u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u5c0f\u7a0b\u5e8f\u5b9e\u4f8b\u3002<\/p>\n<pre>\/\/&nbsp;other.js\r\nvar&nbsp;appInstance&nbsp;=&nbsp;getApp()\r\nconsole.log(appInstance.globalData)&nbsp;\/\/&nbsp;I&nbsp;am&nbsp;global&nbsp;data<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><span style=\"color: #ff0000\">\u6ce8\u610f\uff1a<\/span><\/p>\n<p>App()\u5fc5\u987b\u5728app.js\u4e2d\u6ce8\u518c\uff0c\u4e14\u4e0d\u80fd\u6ce8\u518c\u591a\u4e2a\u3002<br \/>\u4e0d\u8981\u5728\u5b9a\u4e49\u4e8eApp()\u5185\u7684\u51fd\u6570\u4e2d\u8c03\u7528getApp()\uff0c\u4f7f\u7528this\u5c31\u53ef\u4ee5\u62ff\u5230app\u5b9e\u4f8b\u3002<br \/>\u4e0d\u8981\u5728onLaunch\u7684\u65f6\u5019\u8c03\u7528getCurrentPage()\uff0c\u6b64\u65f6page\u8fd8\u6ca1\u6709\u751f\u6210\u3002<br \/>\u901a\u8fc7getApp\u83b7\u53d6\u5b9e\u4f8b\u4e4b\u540e\uff0c\u4e0d\u8981\u79c1\u81ea\u8c03\u7528\u751f\u547d\u5468\u671f\u51fd\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6559\u4f60\u7528app()\u51fd\u6570\u6ce8\u518c\u7a0b\u5e8f\uff08\u5fae\u4fe1\u7a0b\u5e8f\u5f00\u53d1\uff09\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>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6ce8\u518c\u7a0b\u5e8f\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b \u7cfb\u5217\u6587\u7ae0\uff1a \u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6a21\u5757\u5316 \u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6ce8\u518c\u9875\u9762 \u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6559\u7a0b\u4e4b\u6ce8\u518c\u7a0b\u5e8f App() App()\u51fd\u6570\u7528\u6765\u6ce8\u518c\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\u3002\u63a5\u53d7\u4e00\u4e2aobject\u53c2\u6570\uff0c\u5176\u6307\u5b9a\u5c0f\u7a0b\u5e8f\u7684\u751f\u547d\u5468\u671f\u51fd\u6570\u7b49\u3002 object\u53c2\u6570\u8bf4\u660e\uff1a \u5c5e\u6027 \u7c7b\u578b \u63cf\u8ff0 \u89e6\u53d1\u65f6\u673a onLaunch Function \u751f\u547d\u5468\u671f\u51fd\u6570&#8211;\u76d1\u542c\u5c0f\u7a0b\u5e8f\u521d\u59cb\u5316 \u5f53\u5c0f\u7a0b\u5e8f\u521d\u59cb\u5316\u5b8c\u6210\u65f6\uff0c\u4f1a\u89e6\u53d1onLaunch\uff08\u5168\u5c40\u53ea\u89e6\u53d1\u4e00\u6b21\uff09 onShow Function \u751f\u547d\u5468\u671f\u51fd\u6570&#8211;\u76d1\u542c\u5c0f\u7a0b\u5e8f\u663e\u793a \u5f53\u5c0f\u7a0b\u5e8f\u542f\u52a8\uff0c\u6216\u4ece\u540e\u53f0\u8fdb\u5165\u524d\u53f0\u663e\u793a\uff0c\u4f1a\u89e6\u53d1onShow onHide Function \u751f\u547d\u5468\u671f\u51fd\u6570&#8211;\u76d1\u542c\u5c0f\u7a0b\u5e8f\u9690\u85cf \u5f53\u5c0f\u7a0b\u5e8f\u4ece\u524d\u53f0\u8fdb\u5165\u540e\u53f0\uff0c\u4f1a\u89e6\u53d1onHide \u5176\u4ed6 Any \u5f00\u53d1\u8005\u53ef\u4ee5\u6dfb\u52a0\u4efb\u610f\u7684\u51fd\u6570\u6216\u6570\u636e\u5230Object\u53c2\u6570\u4e2d\uff0c\u7528this\u53ef\u4ee5\u8bbf\u95ee &nbsp; \u524d\u53f0\u3001\u540e\u53f0\u5b9a\u4e49\uff1a \u5f53\u7528\u6237\u70b9\u51fb\u5de6\u4e0a\u89d2\u5173\u95ed\uff0c\u6216\u8005\u6309\u4e86\u8bbe\u5907Home\u952e\u79bb\u5f00\u5fae\u4fe1\uff0c\u5c0f\u7a0b\u5e8f\u5e76\u6ca1\u6709\u6b63\u5728\u7684\u9500\u6bc1\uff0c\u800c\u662f\u8fdb\u5165\u4e86\u540e\u53f0\uff1b\u5f53\u518d\u6b21\u542f\u52a8\u5fae\u4fe1\u6216\u518d\u6b21\u6253\u5f00\u5c0f\u7a0b\u5e8f\uff0c\u53c8\u4f1a\u4ece\u540e\u53f0\u8fdb\u5165\u524d\u53f0\u3002 \u53ea\u6709\u5f53\u5c0f\u7a0b\u5e8f\u8fdb\u5165\u540e\u53f0\u4e00\u5b9a\u65f6\u95f4\uff0c\u6216\u8005\u7cfb\u7edf\u8d44\u6e90\u5360\u7528\u8fc7\u9ad8\uff0c\u624d\u4f1a\u88ab\u771f\u6b63\u7684\u9500\u6bc1\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a App({ &nbsp;onLaunch:&nbsp;function()&nbsp;{&nbsp; &nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;initial&nbsp;when&nbsp;launch. &nbsp;}, &nbsp;onShow:&nbsp;function()&nbsp;{ &nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;show. &nbsp;}, &nbsp;onHide:&nbsp;function()&nbsp;{ &nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;hide. &nbsp;}, &nbsp;globalData:&nbsp;&#8216;I&nbsp;am&nbsp;global&nbsp;data&#8217; }) \u767b\u5f55\u540e\u590d\u5236 App.prototype.getCurrentPage() getCurrentPage()\u51fd\u6570\u7528\u6237\u83b7\u53d6\u5f53\u524d\u9875\u9762\u7684\u5b9e\u4f8b\u3002 getApp() \u6211\u4eec\u63d0\u4f9b\u4e86\u5168\u5c40\u7684getApp()\u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u5c0f\u7a0b\u5e8f\u5b9e\u4f8b\u3002 \/\/&nbsp;other.js var&nbsp;appInstance&nbsp;=&nbsp;getApp() [&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-29714","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29714","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=29714"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29714\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=29714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=29714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=29714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}