{"id":30201,"date":"2024-11-25T08:42:04","date_gmt":"2024-11-25T00:42:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/30201\/"},"modified":"2024-11-25T08:42:04","modified_gmt":"2024-11-25T00:42:04","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%bc%80%e5%8f%91%e4%b9%8bapp%ef%bc%88%ef%bc%89%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/30201\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4bAPP\uff08\uff09\u51fd\u6570"},"content":{"rendered":"<p style=\"margin: 20px 0px 10px;padding: 0px;font-family: \u5fae\u8f6f\u96c5\u9ed1;line-height: 1.7;font-size: 16px\">App()<\/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>object\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/\/upload\/image\/843\/948\/140\/1495010554820124.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4bAPP\uff08\uff09\u51fd\u6570\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4bAPP\uff08\uff09\u51fd\u6570\u63d2\u56fe\" \/><\/p>\n<p>\u524d\u53f0\u3001\u540e\u53f0\u5b9a\u4e49\uff1a\u5f53\u7528\u6237\u70b9\u51fb\u5de6\u4e0a\u89d2\u5173\u95ed\uff0c\u6216\u8005\u6309\u4e86\u8bbe\u5907 Home \u952e\u79bb\u5f00\u5fae\u4fe1\uff0c\u5c0f\u7a0b\u5e8f\u5e76\u6ca1\u6709\u76f4\u63a5\u9500\u6bc1\uff0c\u800c\u662f\u8fdb\u5165\u4e86\u540e\u53f0\uff1b\u5f53\u518d\u6b21\u8fdb\u5165\u5fae\u4fe1\u6216\u518d\u6b21\u6253\u5f00\u5c0f\u7a0b\u5e8f\uff0c\u53c8\u4f1a\u4ece\u540e\u53f0\u8fdb\u5165\u524d\u53f0\u3002<\/p>\n<p>\u5173\u95ed\u5c0f\u7a0b\u5e8f(\u516c\u5171\u5e93\u7248\u672c1.1.0\u5f00\u59cb\u652f\u6301)\uff1a\u5f53\u7528\u6237\u4ece\u626b\u4e00\u626b\u3001\u5206\u4eab\u7b49\u5165\u53e3(\u573a\u666f\u503c\u4e3a1007, 1008, 1011, 1025)\u8fdb\u5165\u5c0f\u7a0b\u5e8f\uff0c\u4e14\u6ca1\u6709\u7f6e\u9876\u5c0f\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u5c0f\u7a0b\u5e8f\u4f1a\u88ab\u9500\u6bc1\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;&nbsp;onLaunch:&nbsp;function(options)&nbsp;{&nbsp;\r\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;initial&nbsp;when&nbsp;launch.\r\n&nbsp;&nbsp;},\r\n&nbsp;&nbsp;onShow:&nbsp;function(options)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;show.\r\n&nbsp;&nbsp;},\r\n&nbsp;&nbsp;onHide:&nbsp;function()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;hide.\r\n&nbsp;&nbsp;},\r\n&nbsp;&nbsp;onError:&nbsp;function(msg)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;console.log(msg)\r\n&nbsp;&nbsp;},\r\n&nbsp;&nbsp;globalData:&nbsp;'I&nbsp;am&nbsp;global&nbsp;data'})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>onLaunch, onShow \u53c2\u6570<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/\/upload\/image\/531\/196\/170\/1495010586696821.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4bAPP\uff08\uff09\u51fd\u6570\u63d2\u56fe1\" alt=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4bAPP\uff08\uff09\u51fd\u6570\u63d2\u56fe1\" \/><\/p>\n<p>\u573a\u666f\u503c&nbsp;\u8be6\u89c1\u3002<\/p>\n<p style=\"margin: 20px 0px 10px;padding: 0px;font-family: \u5fae\u8f6f\u96c5\u9ed1;line-height: 1.7;font-size: 16px\">getApp()<\/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.jsvar&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><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<p>App()\u5fc5\u987b\u5728app.js\u4e2d\u6ce8\u518c\uff0c\u4e14\u4e0d\u80fd\u6ce8\u518c\u591a\u4e2a\u3002<\/p>\n<p>\u4e0d\u8981\u5728\u5b9a\u4e49\u4e8eApp()\u5185\u7684\u51fd\u6570\u4e2d\u8c03\u7528getApp()\uff0c\u4f7f\u7528this\u5c31\u53ef\u4ee5\u62ff\u5230app\u5b9e\u4f8b\u3002<\/p>\n<p>\u4e0d\u8981\u5728onLaunch\u7684\u65f6\u5019\u8c03\u7528getCurrentPage()\uff0c\u6b64\u65f6page\u8fd8\u6ca1\u6709\u751f\u6210\u3002<\/p>\n<p>\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>\u3010\u76f8\u5173\u63a8\u8350\u3011<\/p>\n<p>1. <span><strong>\u7279\u522b\u63a8\u8350<\/strong><\/span>\uff1a<\/p>\n<p>2. <\/p>\n<p>3. <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4bAPP\uff08\uff09\u51fd\u6570\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>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 \u524d\u53f0\u3001\u540e\u53f0\u5b9a\u4e49\uff1a\u5f53\u7528\u6237\u70b9\u51fb\u5de6\u4e0a\u89d2\u5173\u95ed\uff0c\u6216\u8005\u6309\u4e86\u8bbe\u5907 Home \u952e\u79bb\u5f00\u5fae\u4fe1\uff0c\u5c0f\u7a0b\u5e8f\u5e76\u6ca1\u6709\u76f4\u63a5\u9500\u6bc1\uff0c\u800c\u662f\u8fdb\u5165\u4e86\u540e\u53f0\uff1b\u5f53\u518d\u6b21\u8fdb\u5165\u5fae\u4fe1\u6216\u518d\u6b21\u6253\u5f00\u5c0f\u7a0b\u5e8f\uff0c\u53c8\u4f1a\u4ece\u540e\u53f0\u8fdb\u5165\u524d\u53f0\u3002 \u5173\u95ed\u5c0f\u7a0b\u5e8f(\u516c\u5171\u5e93\u7248\u672c1.1.0\u5f00\u59cb\u652f\u6301)\uff1a\u5f53\u7528\u6237\u4ece\u626b\u4e00\u626b\u3001\u5206\u4eab\u7b49\u5165\u53e3(\u573a\u666f\u503c\u4e3a1007, 1008, 1011, 1025)\u8fdb\u5165\u5c0f\u7a0b\u5e8f\uff0c\u4e14\u6ca1\u6709\u7f6e\u9876\u5c0f\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u5c0f\u7a0b\u5e8f\u4f1a\u88ab\u9500\u6bc1\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;&nbsp;onLaunch:&nbsp;function(options)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;initial&nbsp;when&nbsp;launch. &nbsp;&nbsp;}, &nbsp;&nbsp;onShow:&nbsp;function(options)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;show. &nbsp;&nbsp;}, &nbsp;&nbsp;onHide:&nbsp;function()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;Do&nbsp;something&nbsp;when&nbsp;hide. &nbsp;&nbsp;}, &nbsp;&nbsp;onError:&nbsp;function(msg)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;console.log(msg) &nbsp;&nbsp;}, &nbsp;&nbsp;globalData:&nbsp;&#8216;I&nbsp;am&nbsp;global&nbsp;data&#8217;}) \u767b\u5f55\u540e\u590d\u5236 onLaunch, onShow \u53c2\u6570 \u573a\u666f\u503c&nbsp;\u8be6\u89c1\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.jsvar&nbsp;appInstance&nbsp;=&nbsp;getApp() console.log(appInstance.globalData)&nbsp;\/\/&nbsp;I&nbsp;am&nbsp;global&nbsp;data \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a App()\u5fc5\u987b\u5728app.js\u4e2d\u6ce8\u518c\uff0c\u4e14\u4e0d\u80fd\u6ce8\u518c\u591a\u4e2a\u3002 \u4e0d\u8981\u5728\u5b9a\u4e49\u4e8eApp()\u5185\u7684\u51fd\u6570\u4e2d\u8c03\u7528getApp()\uff0c\u4f7f\u7528this\u5c31\u53ef\u4ee5\u62ff\u5230app\u5b9e\u4f8b\u3002 \u4e0d\u8981\u5728onLaunch\u7684\u65f6\u5019\u8c03\u7528getCurrentPage()\uff0c\u6b64\u65f6page\u8fd8\u6ca1\u6709\u751f\u6210\u3002 \u901a\u8fc7getApp\u83b7\u53d6\u5b9e\u4f8b\u4e4b\u540e\uff0c\u4e0d\u8981\u79c1\u81ea\u8c03\u7528\u751f\u547d\u5468\u671f\u51fd\u6570\u3002 \u3010\u76f8\u5173\u63a8\u8350\u3011 1. \u7279\u522b\u63a8\u8350\uff1a 2. 3. \u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4bAPP\uff08\uff09\u51fd\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/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-30201","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30201","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=30201"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30201\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=30201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=30201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=30201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}