{"id":1166,"date":"2022-05-15T15:14:10","date_gmt":"2022-05-15T07:14:10","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1166"},"modified":"2022-05-15T16:33:39","modified_gmt":"2022-05-15T08:33:39","slug":"ni-wei-he-yao-bi-mian-shi-yong-indexjs","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1166","title":{"rendered":"\u4f60\u4e3a\u4f55\u8981\u907f\u514d\u4f7f\u7528 index.js ?"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/2022-05-1515_19_50.jpeg\" alt=\"2022-05-15+15_19_50\" \/><\/p>\n<p>\u5728\u5f88\u591a JavaScript \u9879\u76ee\u4e2d\uff0c\u4e0d\u7ba1\u662f\u524d\u7aef React \u9879\u76ee\uff0c\u8fd8\u662f\u540e\u7aef node.js \u9879\u76ee\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a index.js \u6587\u4ef6\u6765\u805a\u5408\u6240\u6709\u7684\u5bfc\u5165\u3002\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5bfc\u51fa\u6240\u6709\u76f8\u5173\u7684 exports \u4f1a\u65b9\u4fbf\u8bb8\u591a\uff0c\u63a5\u7740\u5176\u5b83\u6587\u4ef6\u53ef\u4ee5\u4ece\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5bfc\u5165\u5185\u5bb9\u3002\u5b83\u770b\u4e0a\u53bb\u7b80\u6d01\uff0c\u4f46\u53ef\u80fd\u4f1a\u5f88\u5feb\u5931\u63a7\u3002<\/p>\n<p><strong>TLDR:<\/strong><\/p>\n<p><strong>\u4e3a\u4f55\u4f60\u5e94\u8be5\u907f\u514d\u805a\u5408\u6240\u6709\u7684\u5bfc\u5165\u5230\u4e00\u4e2a\u6587\u4ef6 index.js \u4e2d\uff1f<\/strong><\/p>\n<ol>\n<li>\u5faa\u73af\u5f15\u7528\uff1b<\/li>\n<li>\u4f7f\u7528\u52a8\u6001\u5bfc\u5165\u65f6\u7684\u610f\u5916\u5bfc\u5165\uff1b<\/li>\n<li>nodejs \u7684\u4f5c\u8005\u4e5f\u8bf4\u8981\u907f\u514d\u4f7f\u7528\uff1b<\/li>\n<\/ol>\n<p><strong>\u4ec0\u4e48\u65f6\u5019\u53ef\u4ee5 index.js ?<\/strong><\/p>\n<p>\u5f53\u4f60\u521b\u5efa npm \u5305\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528 index.js \u3002<\/p>\n<h3><a id=\"%E5%85%B8%E5%9E%8B%E7%9A%84index-js%E9%95%BF%E4%BB%80%E4%B9%88%E6%A0%B7%E5%AD%90%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5178\u578b\u7684 index.js \u957f\u4ec0\u4e48\u6837\u5b50\uff1f<\/h3>\n<p>\u5178\u578b\u7684 index.js \u6587\u4ef6\u50cf\u4e0b\u9762\u8fd9\u6837\uff0c\u53ef\u4ee5\u805a\u5408\u6240\u6709\u7684\u5bfc\u51fa\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526002943680.jpg\" alt=\"\" \/><\/p>\n<h3><a id=\"1%E5%BE%AA%E7%8E%AF%E5%BC%95%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. \u5faa\u73af\u5f15\u7528<\/h3>\n<p>\u867d\u7136\u65f6\u95f4\u7684\u63a8\u79fb index.js \u6587\u4ef6\u53d8\u5f97\u8d8a\u6765\u8d8a\u5927\uff0c\u6b64\u65f6\u4e00\u4f4d\u5f00\u53d1\u8005\u5229\u7528\u81ea\u52a8\u5f15\u5165\u529f\u80fd\uff0c\u5728\u4e00\u4e2a\u76f8\u90bb\u6587\u4ef6\u4e2d\u5f15\u5165\u4e86 index.js \u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526004559797.jpg\" alt=\"\" \/><\/p>\n<p>\u73b0\u5728\uff0c\u8be5\u5f00\u53d1\u8005\u53c8\u901a\u8fc7 index.js \u6587\u4ef6\u5bfc\u51fa\u4e86\u4e00\u4e9b\u5185\u5bb9\uff0c\u5c31\u50cf\u5176\u4ed6\u4eba\u505a\u7684\u4e00\u6837\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526005306104.jpg\" alt=\"\" \/><\/p>\n<p>\u73b0\u5728\u95ee\u9898\u662f\u4ec0\u4e48\uff1f\u4ed6\u5236\u9020\u4e86\u4e00\u4e2a\u5faa\u73af\u5f15\u7528\u3002<\/p>\n<p>\u4ec0\u4e48\uff1f&quot;composition.js&quot; \u4ece &quot;index.js&quot; \u4e2d\u5bfc\u5165 Items\uff0c&quot;index.js&quot; \u5bfc\u5165\u4e86 &quot;composition.js&quot; \u4e2d\u7684 &quot;CompostionsOfItems&quot; \u5e76\u5bfc\u51fa\u3002<\/p>\n<blockquote>\n<p>composition.js \u2192 index.js \u2192 composition.js<\/p>\n<\/blockquote>\n<p>\u73b0\u5728\uff0c\u4e00\u4e2a\uff08\u4f8b\u5982\uff0citems.js\uff09\u4ece index.js \u6587\u4ef6\u4e2d\u5bfc\u5165\u4e00\u4e9b\u53d8\u91cf\u7684\u6587\u4ef6\u5c06\u4f1a\u629b\u51fa\u9519\u8bef\uff1a<code>cannot read property &quot;Item1&quot; of undefined<\/code>\u3002<\/p>\n<p>\u4e00\u4e9b\u7f16\u8bd1\u5de5\u5177\u53ef\u4ee5\u5e94\u4ed8\u8fd9\u79cd\u5faa\u73af\u5bfc\u5165\uff08\u4f8b\u5982\uff1a\u5e26\u6709 babel \u7684 webpack\uff09\uff0c\u4f46\u662f\u5176\u4ed6\u4f8b\u5b50\uff08jest + ts-jest\u3001jest + bable\uff09\u7684\u7f16\u8bd1\u65e0\u6cd5\u5904\u7406\u8fd9\u79cd\u95ee\u9898\uff0c\u5c06\u4f1a\u629b\u51fa\u4e0d\u6e05\u6670\u7684\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u5f88\u96be\u8c03\u8bd5\u4f46\u662f\u5f53\u4f60\u77e5\u9053\u907f\u514d\u4f7f\u7528 index.js \u65f6\uff0c\u6b64\u79cd\u95ee\u9898\u5c06\u4f1a\u5f88\u5c11\u3002<\/p>\n<p><strong>\u95ee\u9898<\/strong>\uff1a\u90a3\u6211\u4eec\u8be5\u5982\u4f55\u505a \uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong> \u907f\u514d\u4f7f\u7528 index.js \u540c\u65f6\u53ea\u4ece\u6587\u4ef6\u4e2d\u5f15\u5165\u4f60\u9700\u8981\u7684\u5185\u5bb9\u3002\u5728\u6211\u4eec\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4ece items.js \u4e2d\u5f15\u5165 Item1 \u548c Item2 \u5c31\u4e0d\u4f1a\u4ea7\u751f\u95ee\u9898\u3002<\/p>\n<p>\u907f\u514d\u4f7f\u7528 index.js \u5e76\u4e0d\u80fd\u4fdd\u8bc1\u4e0d\u4f1a\u4ea7\u751f\u5faa\u73af\u5f15\u7528\uff0c\u4f60\u5fc5\u987b\u628a\u5bfc\u5165\u8bed\u53e5\u7ec4\u7ec7\u6210\u4e00\u4e2a\u6811\u72b6\u7ed3\u6784\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526015454734.jpg\" alt=\"\" \/><\/p>\n<p>\u907f\u514d\u5faa\u73af\u5f15\u7528\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528  eslint-plugin-import \u4e2d\u7684 Eslint  <code>no-cycle<\/code>  \u89c4\u5219\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526016222993.jpg\" alt=\"\" \/><\/p>\n<h3><a id=\"2%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81%E5%AF%BC%E5%85%A5%E6%97%B6%E7%9A%84%E6%84%8F%E5%A4%96%E5%AF%BC%E5%85%A5\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. \u4f7f\u7528\u52a8\u6001\u5bfc\u5165\u65f6\u7684\u610f\u5916\u5bfc\u5165<\/h3>\n<p>\u5f53\u5728\u5199\u524d\u7aef\u9879\u76ee\u65f6\uff0c\u901a\u5e38\u662f\u8981\u8003\u8651\u4f7f\u7528\u52a8\u6001\u5bfc\u5165\u6765\u5206\u5272\u4ee3\u7801\u3002\u4f8b\u5982\uff1a\u5728 React \u4e2d\u4f60\u53ef\u4ee5\u4f7f\u7528  <code>React.lazy()<\/code>  \u4f7f\u4ee3\u7801\u5206\u5272\u4e3a\u591a\u5757\u3002\u4f7f\u7528\u54ea\u4e2a\u6846\u67b6\u5e76\u4e0d\u91cd\u8981\uff0c\u4f46\u6211\u4eec\u4ee5\u5199 react app \u4e3a\u4f8b\uff0c\u4f60\u628a\u6240\u6709\u7684 routes \u6574\u6d01\u7684\u805a\u5408\u4e0e routes.js \u6587\u4ef6\u4e2d\uff1a<\/p>\n<pre><code class=\"language-plain_text\">\/\/ routes.js\nexport Route1 from 'route1'\nexport Route2 from 'route2'\nexport Route3 from 'route3'\nexport Route4 from 'route4'\n<\/code><\/pre>\n<p>\u82e5\u4f60\u6253\u7b97\u5229\u7528\u61d2\u52a0\u8f7d\u6765\u4f7f\u4f60\u7684\u5e94\u7528\u66f4\u5feb\uff0c\u4f60\u53ef\u80fd\u4f1a\u5199\u51fa\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const LazyRoute1 = React.lazy(() =&gt; import('.\/routes').then(module =&gt; ({ default: module.Route1 }));\n<\/code><\/pre>\n<p>\u7f6a\u9b41\u7978\u9996\u662f\u4ec0\u4e48\uff1f\u4f60\u5b9e\u9645\u4e0a\u662f\u4e00\u6b21\u6027\u8bf7\u6c42\u4e86\u6240\u6709\u7684\u8def\u7531\u800c\u4e0d\u662f\u53ea\u6309\u9700\u52a0\u8f7d\u4e86 Route1 \uff0c\u5bfc\u81f4\u4f7f\u7528\u4e86\u6bd4\u9884\u671f\u66f4\u591a\u7684\u5e26\u5bbd\u540c\u65f6\u4e5f\u4f7f\u61d2\u52a0\u8f7d\u548c\u4ee3\u7801\u5206\u5272\u529f\u80fd\u5931\u6548\u3002<\/p>\n<h3><a id=\"3-nodejs%E7%9A%84%E4%BD%9C%E8%80%85%E4%B9%9F%E8%AF%B4%E9%81%BF%E5%85%8D%E4%BD%BF%E7%94%A8-index-js\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3. nodejs \u7684\u4f5c\u8005\u4e5f\u8bf4\u907f\u514d\u4f7f\u7528 index.js<\/h3>\n<p>\u5728 JSConf EU \u7684\u4f1a\u8bae\u4e0a\uff0cnodejs \u7684\u4f5c\u8005 Ryan Dahl \u6f14\u793a\u4e86\u5173\u4e8e nodejs \u7684 10 \u4ef6\u9057\u61be\u7684\u4e8b\uff0c\u5176\u4e2d\u4e00\u4e2a\u5c31\u662f index.js \u6587\u4ef6\u3002\u90a3\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u6f14\u8bb2\u6211\u5efa\u8bae\u4f60\u770b\u4e0b\uff1a<\/p>\n<p>\u603b\u7ed3\uff1a<a href=\"https:\/\/medium.com\/@imior\/10-things-i-regret-about-node-js-ryan-dahl-2ba71ff6b4dc\" target=\"_blank\" rel=\"noopener\">10 Things I Regret About Node.js \u2014 Ryan Dahl<\/a><\/p>\n<p>\u89c6\u9891\uff1a<a href=\"https:\/\/youtu.be\/M3BM9TB-8yA?t=876\" target=\"_blank\" rel=\"noopener\">10 Things I Regret About Node.js &#8211; Ryan Dahl &#8211; JSConf EU<\/a><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526029813911.jpg\" alt=\"\" \/><\/p>\n<h3><a id=\"%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E4%BD%A0%E5%BA%94%E8%AF%A5%E4%BD%BF%E7%94%A8index-js\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ec0\u4e48\u65f6\u5019\u4f60\u5e94\u8be5\u4f7f\u7528 index.js ?<\/h3>\n<p>\u5982\u679c\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u5f00\u6e90\u7684\u5305\u540c\u65f6\u5e0c\u671b\u5bfc\u51fa\u516c\u5171\u6a21\u5757\uff0c\u901a\u5e38\u662f\u4f7f\u7528\u4e00\u4e2a\u6587\u4ef6\u6765\u66b4\u9732\u51fa\u6240\u6709\u9700\u8981\u5bfc\u51fa\u7684\u4e1c\u897f\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526030982992.jpg\" alt=\"\" \/><\/p>\n<p><strong>\u53e6\u5916\u6211\u4e2a\u4eba\u52a0\u4e00\u4e2a\u975e\u4f5c\u8005\u6240\u5199<\/strong>\uff0c\u5c31\u662f\u5f53\u641c\u6587\u4ef6\u6216\u6587\u4ef6\u7f16\u8f91\u5668\u91cc\u6253\u5f00\u4e86\u5f88\u591a index.js \u65f6\uff0c\u4f1a\u8ba9\u6211\u6293\u72c2\uff0c\u6839\u672c\u4e0d\u77e5\u9053\u6253\u5f00\u4e86\u4ec0\u4e48\u6587\u4ef6\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/05\/16526034197851.jpg\" alt=\"\" \/><\/p>\n<p>\u6211\u7684\u4e2a\u4eba\u5efa\u8bae\u662f\uff1a<strong>\u4e1a\u52a1\u4ee3\u7801\u4e2d\u5c11\u7528 index.js \u6765\u7ec4\u7ec7\u4ee3\u7801<\/strong>\u3002<\/p>\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/medium.com\/@alonmiz1234\/why-you-should-avoid-index-js-3321a9902120\" target=\"_blank\" rel=\"noopener\">https:\/\/medium.com\/@alonmiz1234\/why-you-should-avoid-index-js-3321a9902120<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f88\u591a JavaScript \u9879\u76ee\u4e2d\uff0c\u4e0d\u7ba1\u662f\u524d\u7aef React \u9879\u76ee\uff0c\u8fd8\u662f\u540e\u7aef node.js \u9879\u76ee\u4f60&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,21,3],"tags":[11],"class_list":["post-1166","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-backend","category-tech","tag-11"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1166"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1166\/revisions"}],"predecessor-version":[{"id":1167,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1166\/revisions\/1167"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}