{"id":30351,"date":"2024-11-25T17:10:17","date_gmt":"2024-11-25T09:10:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/30351\/"},"modified":"2024-11-25T17:10:17","modified_gmt":"2024-11-25T09:10:17","slug":"%e5%be%ae%e4%bf%a1%e5%bc%80%e5%8f%91%e4%b9%8b%e5%bc%95%e7%94%a8%e5%85%b6%e4%bb%96js%e6%96%87%e4%bb%b6%e5%ae%9e%e4%be%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/30351\/","title":{"rendered":"\u5fae\u4fe1\u5f00\u53d1\u4e4b\u5f15\u7528\u5176\u4ed6js\u6587\u4ef6\u5b9e\u4f8b\u8be6\u89e3"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u5176\u4ed6\u6587\u4ef6\u5b9e\u73b0\u4ee3\u7801\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p><strong>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5982\u4f55\u5f15\u7528\u5176\u4ed6js\u6587\u4ef6<\/strong><\/p>\n<p>1.\u6211\u4eec\u5148\u5efa\u7acb\u4e00\u4e2acommon.js\u6587\u4ef6\uff0c\u5728common.js\u7f16\u5199\u6211\u4eec\u7684\u7a0b\u5e8f\uff0c<\/p>\n<pre>function&nbsp;myfunc()&nbsp;{\r\nconsole.log(\"myfunc....\");\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><span style=\"color: #0000ff\">.exports.myfunc = myfunc; \u8fd9\u6837\u66b4\u9732\uff0c\u8fd9\u91cc\u4e0d\u66b4\u9732\u662f\u4e0d\u80fd\u5f15\u7528\u7684\uff0c<\/span><\/p>\n<p><strong>\u5728\u6587\u4ef6\u57dfjs\u5185<\/strong><\/p>\n<p><span style=\"color: #0000ff\">var common = (&#8220;..\/..\/common.js&#8221;);\u53bb\u94fe\u63a5\u8fc7\u6765\uff0c\u5149\u94fe\u63a5\u8fc7\u6765\u8fd8\u4e0d\u884c\uff01<\/span><\/p>\n<pre>&nbsp;var&nbsp;app;\r\nvar&nbsp;common&nbsp;=&nbsp;require(\"..\/..\/common.js\");\r\nPage({\r\ndata:{\r\n\r\n},\r\nonLoad:function()&nbsp;{\r\napp&nbsp;=&nbsp;getApp();\r\nthis.setData({version:app.globalData.appName});\r\ncommon.myfunc();&nbsp;\/\/\u6700\u540e\u6211\u4eec\u9700\u8981\u6267\u884c\u624d\u80fd\u751f\u6548\uff01\r\n}\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u3010\u76f8\u5173\u63a8\u8350\u3011<\/p>\n<p>1.&nbsp;<\/p>\n<p>2.<\/p>\n<p>3.<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5f00\u53d1\u4e4b\u5f15\u7528\u5176\u4ed6js\u6587\u4ef6\u5b9e\u4f8b\u8be6\u89e3\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 \u5176\u4ed6\u6587\u4ef6\u5b9e\u73b0\u4ee3\u7801\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5982\u4f55\u5f15\u7528\u5176\u4ed6js\u6587\u4ef6 1.\u6211\u4eec\u5148\u5efa\u7acb\u4e00\u4e2acommon.js\u6587\u4ef6\uff0c\u5728common.js\u7f16\u5199\u6211\u4eec\u7684\u7a0b\u5e8f\uff0c function&nbsp;myfunc()&nbsp;{ console.log(&#8220;myfunc&#8230;.&#8221;); } \u767b\u5f55\u540e\u590d\u5236 .exports.myfunc = myfunc; \u8fd9\u6837\u66b4\u9732\uff0c\u8fd9\u91cc\u4e0d\u66b4\u9732\u662f\u4e0d\u80fd\u5f15\u7528\u7684\uff0c \u5728\u6587\u4ef6\u57dfjs\u5185 var common = (&#8220;..\/..\/common.js&#8221;);\u53bb\u94fe\u63a5\u8fc7\u6765\uff0c\u5149\u94fe\u63a5\u8fc7\u6765\u8fd8\u4e0d\u884c\uff01 &nbsp;var&nbsp;app; var&nbsp;common&nbsp;=&nbsp;require(&#8220;..\/..\/common.js&#8221;); Page({ data:{ }, onLoad:function()&nbsp;{ app&nbsp;=&nbsp;getApp(); this.setData({version:app.globalData.appName}); common.myfunc();&nbsp;\/\/\u6700\u540e\u6211\u4eec\u9700\u8981\u6267\u884c\u624d\u80fd\u751f\u6548\uff01 } }) \u767b\u5f55\u540e\u590d\u5236 \u3010\u76f8\u5173\u63a8\u8350\u3011 1.&nbsp; 2. 3. \u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5f00\u53d1\u4e4b\u5f15\u7528\u5176\u4ed6js\u6587\u4ef6\u5b9e\u4f8b\u8be6\u89e3\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-30351","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30351","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=30351"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/30351\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=30351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=30351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=30351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}