{"id":28487,"date":"2024-11-25T17:43:26","date_gmt":"2024-11-25T09:43:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/28487\/"},"modified":"2024-11-25T17:43:26","modified_gmt":"2024-11-25T09:43:26","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b8%ad%e6%96%87%e4%bb%b6%e4%bd%9c%e7%94%a8%e5%9f%9f%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/28487\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u6587\u4ef6\u4f5c\u7528\u57df\u89e3\u6790"},"content":{"rendered":"<p><\/p>\n<h3><strong>\u6587\u4ef6<\/strong><\/h3>\n<p>\u5728javascript\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u548c\u51fd\u6570\u53ea\u5728\u8be5\u6587\u4ef6\u4e2d\u6709\u6548;\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u53ef\u4ee5\u751f\u547d\u76f8\u540c\u7684\u540d\u5b57\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\u3002 <br \/>\u901a\u8fc7\u5168\u5c40\u51fd\u6570getapp() \u53ef\u4ee5\u83b7\u53d6\u5168\u5c40\u7684\u5e94\u7528\u5b9e\u5217\uff0c\u5982\u679c\u9700\u8981\u5168\u5c40\u7684\u6570\u636e\u53ef\u4ee5\u5728app() \u4e2d\u8bbe\u7f6e,\u5982:<\/p>\n<pre>\/\/app.jsapp({\r\n&nbsp;&nbsp;&nbsp;&nbsp;globalData\uff1a1})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/&nbsp;a.js\/\/&nbsp;The&nbsp;localValue&nbsp;can&nbsp;only&nbsp;be&nbsp;used&nbsp;in&nbsp;file&nbsp;a.js.var&nbsp;localValue&nbsp;=&nbsp;'a'\/\/&nbsp;Get&nbsp;the&nbsp;app&nbsp;instance.var&nbsp;app&nbsp;=&nbsp;getApp()\/\/&nbsp;Get&nbsp;the&nbsp;global&nbsp;data&nbsp;and&nbsp;change&nbsp;it.app.globalData++<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/&nbsp;b.js\/\/&nbsp;You&nbsp;can&nbsp;redefine&nbsp;localValue&nbsp;in&nbsp;file&nbsp;b.js,&nbsp;without&nbsp;interference&nbsp;with&nbsp;the&nbsp;localValue&nbsp;in&nbsp;a.js.\r\nvar&nbsp;localValue&nbsp;=&nbsp;'b'\/\/&nbsp;If&nbsp;a.js&nbsp;it&nbsp;run&nbsp;before&nbsp;b.js,&nbsp;now&nbsp;the&nbsp;globalData&nbsp;shoule&nbsp;be&nbsp;2.console.log(getApp().globalData)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3><strong>\u6a21\u5757\u5316<\/strong><\/h3>\n<p>\u53ef\u4ee5\u5c06\u4e00\u4e9b\u516c\u5171\u7684\u4ee3\u7801\u62bd\u79bb\u6210\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684js\u6587\u4ef6\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u6a21\u5757\u5316\u3002\u6a21\u5757\u5316\u53ea\u6709\u901a\u8fc7module.exports \u6216\u8005 exports \u624d\u80fd\u5bf9\u5916\u66b4\u9732\u63a5\u53e3\u3002 <br \/>\u9700\u8981\u6ce8\u610f\u7684\u662f:<\/p>\n<ul>\n<li>\n<p>wxports\u662fmodule.exports \u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u56e0\u6b64\u5728\u6a21\u5757\u5316\u91cc\u8fb9\u968f\u610f\u66f4\u6539exports \u7684\u6307\u5411\u4f1a\u9020\u6210\u672a\u77e5\u7684\u9519\u8bef\u3002\u6240\u4ee5\u66f4\u63a8\u8350\u5f00\u53d1\u8005\u91c7\u7528module.exports \u6765\u66b4\u9732\u6a21\u5757\u63a5\u53e3\uff0c\u9664\u975e\u4f60\u5df2\u7ecf\u6e05\u6670\u77e5\u9053\u8fd9\u4e24\u8005\u7684\u5173\u7cfb\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c0f\u7a0b\u5e8f\u76ee\u524d\u4e0d\u652f\u6301\u76f4\u63a5\u5f15\u5165node_modules\uff0c\u5f00\u53d1\u8005\u9700\u8981\u4f7f\u7528\u5230node_modules \u65f6\u5019\u5efa\u8bae\u62f7\u8d1d\u51fa\u76f8\u5173\u7684\u4ee3\u7801\u5230\u5c0f\u7a0b\u5e8f\u7684\u76ee\u5f55\u4e2d<\/p>\n<\/li>\n<\/ul>\n<pre>\/\/commont.jsfunction&nbsp;sayHello(name){\r\n&nbsp;&nbsp;&nbsp;&nbsp;console.log('------&nbsp;&nbsp;hello&nbsp;&nbsp;&nbsp;&nbsp;'&nbsp;+&nbsp;name&nbsp;+'=====');\r\n}\r\nmodule.exports.sayHello&nbsp;=&nbsp;sayHello;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>\/\/index.jsvar&nbsp;common&nbsp;=&nbsp;require('..\/commont\/commont.js');\r\nPage({&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u52a0\u8f7d\u89c6\u56fe\u7684\u65f6\u5019\r\n&nbsp;&nbsp;onLoad:function&nbsp;(){\r\n&nbsp;&nbsp;&nbsp;\/\/\u8c03\u7528\r\n&nbsp;&nbsp;&nbsp;&nbsp;common.sayHello('dqk');\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a7\u5236\u53f0\u8f93\u51fa:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/023\/547\/b4bd94236b9ba938c85cb36712503125-0.png\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u6587\u4ef6\u4f5c\u7528\u57df\u89e3\u6790\u63d2\u56fe\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u6587\u4ef6\u4f5c\u7528\u57df\u89e3\u6790\u63d2\u56fe\" \/> <\/p>\n<h3><strong>\u63d0\u793a<\/strong><\/h3>\n<p>require \u6682\u65f6\u4e0d\u652f\u6301\u7edd\u5bf9\u8def\u5f84<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u6587\u4ef6\u89e3\u6790\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>\u6587\u4ef6 \u5728javascript\u6587\u4ef6\u4e2d\u58f0\u660e\u7684\u53d8\u91cf\u548c\u51fd\u6570\u53ea\u5728\u8be5\u6587\u4ef6\u4e2d\u6709\u6548;\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u53ef\u4ee5\u751f\u547d\u76f8\u540c\u7684\u540d\u5b57\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u4e0d\u4f1a\u76f8\u4e92\u5f71\u54cd\u3002 \u901a\u8fc7\u5168\u5c40\u51fd\u6570getapp() \u53ef\u4ee5\u83b7\u53d6\u5168\u5c40\u7684\u5e94\u7528\u5b9e\u5217\uff0c\u5982\u679c\u9700\u8981\u5168\u5c40\u7684\u6570\u636e\u53ef\u4ee5\u5728app() \u4e2d\u8bbe\u7f6e,\u5982: \/\/app.jsapp({ &nbsp;&nbsp;&nbsp;&nbsp;globalData\uff1a1}) \u767b\u5f55\u540e\u590d\u5236 \/\/&nbsp;a.js\/\/&nbsp;The&nbsp;localValue&nbsp;can&nbsp;only&nbsp;be&nbsp;used&nbsp;in&nbsp;file&nbsp;a.js.var&nbsp;localValue&nbsp;=&nbsp;&#8216;a&#8217;\/\/&nbsp;Get&nbsp;the&nbsp;app&nbsp;instance.var&nbsp;app&nbsp;=&nbsp;getApp()\/\/&nbsp;Get&nbsp;the&nbsp;global&nbsp;data&nbsp;and&nbsp;change&nbsp;it.app.globalData++ \u767b\u5f55\u540e\u590d\u5236 \/\/&nbsp;b.js\/\/&nbsp;You&nbsp;can&nbsp;redefine&nbsp;localValue&nbsp;in&nbsp;file&nbsp;b.js,&nbsp;without&nbsp;interference&nbsp;with&nbsp;the&nbsp;localValue&nbsp;in&nbsp;a.js. var&nbsp;localValue&nbsp;=&nbsp;&#8216;b&#8217;\/\/&nbsp;If&nbsp;a.js&nbsp;it&nbsp;run&nbsp;before&nbsp;b.js,&nbsp;now&nbsp;the&nbsp;globalData&nbsp;shoule&nbsp;be&nbsp;2.console.log(getApp().globalData) \u767b\u5f55\u540e\u590d\u5236 \u6a21\u5757\u5316 \u53ef\u4ee5\u5c06\u4e00\u4e9b\u516c\u5171\u7684\u4ee3\u7801\u62bd\u79bb\u6210\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684js\u6587\u4ef6\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u6a21\u5757\u5316\u3002\u6a21\u5757\u5316\u53ea\u6709\u901a\u8fc7module.exports \u6216\u8005 exports \u624d\u80fd\u5bf9\u5916\u66b4\u9732\u63a5\u53e3\u3002 \u9700\u8981\u6ce8\u610f\u7684\u662f: wxports\u662fmodule.exports \u7684\u4e00\u4e2a\u5f15\u7528\uff0c\u56e0\u6b64\u5728\u6a21\u5757\u5316\u91cc\u8fb9\u968f\u610f\u66f4\u6539exports \u7684\u6307\u5411\u4f1a\u9020\u6210\u672a\u77e5\u7684\u9519\u8bef\u3002\u6240\u4ee5\u66f4\u63a8\u8350\u5f00\u53d1\u8005\u91c7\u7528module.exports \u6765\u66b4\u9732\u6a21\u5757\u63a5\u53e3\uff0c\u9664\u975e\u4f60\u5df2\u7ecf\u6e05\u6670\u77e5\u9053\u8fd9\u4e24\u8005\u7684\u5173\u7cfb\u3002 \u5c0f\u7a0b\u5e8f\u76ee\u524d\u4e0d\u652f\u6301\u76f4\u63a5\u5f15\u5165node_modules\uff0c\u5f00\u53d1\u8005\u9700\u8981\u4f7f\u7528\u5230node_modules \u65f6\u5019\u5efa\u8bae\u62f7\u8d1d\u51fa\u76f8\u5173\u7684\u4ee3\u7801\u5230\u5c0f\u7a0b\u5e8f\u7684\u76ee\u5f55\u4e2d \/\/commont.jsfunction&nbsp;sayHello(name){ &nbsp;&nbsp;&nbsp;&nbsp;console.log(&#8216;&#8212;&#8212;&nbsp;&nbsp;hello&nbsp;&nbsp;&nbsp;&nbsp;&#8216;&nbsp;+&nbsp;name&nbsp;+&#8217;=====&#8217;); } module.exports.sayHello&nbsp;=&nbsp;sayHello; \u767b\u5f55\u540e\u590d\u5236 \/\/index.jsvar&nbsp;common&nbsp;=&nbsp;require(&#8216;..\/commont\/commont.js&#8217;); Page({&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u52a0\u8f7d\u89c6\u56fe\u7684\u65f6\u5019 &nbsp;&nbsp;onLoad:function&nbsp;(){ &nbsp;&nbsp;&nbsp;\/\/\u8c03\u7528 &nbsp;&nbsp;&nbsp;&nbsp;common.sayHello(&#8216;dqk&#8217;); }) \u767b\u5f55\u540e\u590d\u5236 \u63a7\u5236\u53f0\u8f93\u51fa: \u63d0\u793a require \u6682\u65f6\u4e0d\u652f\u6301\u7edd\u5bf9\u8def\u5f84 \u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u6587\u4ef6\u89e3\u6790\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-28487","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28487","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=28487"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/28487\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=28487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=28487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=28487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}