{"id":3430,"date":"2024-11-10T14:33:05","date_gmt":"2024-11-10T06:33:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3430\/"},"modified":"2024-11-10T14:33:05","modified_gmt":"2024-11-10T06:33:05","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8fjs%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5js","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3430\/","title":{"rendered":"\u5c0f\u7a0b\u5e8fjs\u5982\u4f55\u5f15\u5165js"},"content":{"rendered":"<blockquote><p>\n  \u5c0f\u7a0b\u5e8f\u4e2d\u5f15\u5165\u5916\u90e8 js \u6587\u4ef6\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. \u5728 page \u7684 json \u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0 usingcomponents \u5b57\u6bb5\uff1b2. \u5728 wxml \u6a21\u677f\u4e2d\u4f7f\u7528 \u6807\u7b7e\uff1b3. \u5728 js \u6587\u4ef6\u4e2d\u4f7f\u7528 require() \u65b9\u6cd5\uff1b4. \u5728 app.js \u7684 onlaunch \u51fd\u6570\u4e2d\u4f7f\u7528 wx.loadsubpackage \u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812070520009.jpg\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8fjs\u5982\u4f55\u5f15\u5165js\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8fjs\u5982\u4f55\u5f15\u5165js\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5c0f\u7a0b\u5e8f\u4e2d\u5f15\u5165\u5916\u90e8 JS \u6587\u4ef6<\/strong><\/p>\n<p>\u8981\u5c06\u5916\u90e8 JS \u6587\u4ef6\u5f15\u5165\u5c0f\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u5728 Page \u7684 JSON \u914d\u7f6e\u4e2d\u5f15\u5165<\/strong><\/p>\n<p>\u5728 Page \u7684 JSON \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0 usingComponents \u5b57\u6bb5\uff0c\u5e76\u6307\u5b9a\u8981\u5f15\u5165\u7684 JS \u6587\u4ef6\u8def\u5f84\uff1a<\/p>\n<pre>{\n  \"usingComponents\": {\n    \"my-component\": \"\/path\/to\/my-component.js\"\n  }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u5728 WXML \u6a21\u677f\u4e2d\u5f15\u5165<\/strong><\/p>\n<p>\u5728 WXML \u6a21\u677f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528  \u6807\u7b7e\u5f15\u5165\u5916\u90e8 JS \u6587\u4ef6\uff1a <\/p>\n<pre>&lt;import src=\"\/path\/to\/my-utils.js\"&gt;&lt;\/import&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u5728 JS \u6587\u4ef6\u4e2d\u5f15\u5165<\/strong><\/p>\n<p>\u5728 JS \u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 require() \u65b9\u6cd5\u5f15\u5165\u5916\u90e8 JS \u6587\u4ef6\uff1a<\/p>\n<pre>const myUtils = require('\/path\/to\/my-utils.js');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u5728 App.js \u4e2d\u5f15\u5165<\/strong><\/p>\n<p>\u5728 App.js \u7684 onLaunch \u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 wx.loadSubpackage \u65b9\u6cd5\u5f15\u5165\u5916\u90e8 JS \u6587\u4ef6\uff1a<\/p>\n<pre>wx.loadSubpackage({\n  name: 'utils',\n  success: () =&gt; {\n    \/\/ \u5f15\u5165 utils \u5305\u4e2d\u7684 JS \u6587\u4ef6\n    const myUtils = require('\/utils\/my-utils.js');\n  }\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8fjs\u5982\u4f55\u5f15\u5165js\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>\u5c0f\u7a0b\u5e8f\u4e2d\u5f15\u5165\u5916\u90e8 js \u6587\u4ef6\u6709\u56db\u79cd\u65b9\u6cd5\uff1a1. \u5728 page \u7684 json \u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0 usingcomponents \u5b57\u6bb5\uff1b2. \u5728 wxml \u6a21\u677f\u4e2d\u4f7f\u7528 \u6807\u7b7e\uff1b3. \u5728 js \u6587\u4ef6\u4e2d\u4f7f\u7528 require() \u65b9\u6cd5\uff1b4. \u5728 app.js \u7684 onlaunch \u51fd\u6570\u4e2d\u4f7f\u7528 wx.loadsubpackage \u65b9\u6cd5\u3002 \u5c0f\u7a0b\u5e8f\u4e2d\u5f15\u5165\u5916\u90e8 JS \u6587\u4ef6 \u8981\u5c06\u5916\u90e8 JS \u6587\u4ef6\u5f15\u5165\u5c0f\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u5728 Page \u7684 JSON \u914d\u7f6e\u4e2d\u5f15\u5165 \u5728 Page \u7684 JSON \u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0 usingComponents \u5b57\u6bb5\uff0c\u5e76\u6307\u5b9a\u8981\u5f15\u5165\u7684 JS \u6587\u4ef6\u8def\u5f84\uff1a { &#8220;usingComponents&#8221;: { &#8220;my-component&#8221;: &#8220;\/path\/to\/my-component.js&#8221; } } \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3430","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3430","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=3430"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3430\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}