{"id":7885,"date":"2023-11-28T20:04:46","date_gmt":"2023-11-28T12:04:46","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7885"},"modified":"2023-12-02T00:23:38","modified_gmt":"2023-12-01T16:23:38","slug":"vue-zhongvuerouter-shi-xian-lu-you-lan-jia-zai-don","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/vue-zhongvuerouter-shi-xian-lu-you-lan-jia-zai-don\/","title":{"rendered":"Vue \u4e2d vue-router \u5b9e\u73b0\u8def\u7531\u61d2\u52a0\u8f7d(\u52a8\u6001\u52a0\u8f7d\u8def\u7531)\uff1f"},"content":{"rendered":"<p>\u5728 Vue \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8def\u7531\u61d2\u52a0\u8f7d\u6765\u52a8\u6001\u52a0\u8f7d\u8def\u7531\u7ec4\u4ef6\uff0c\u4ee5\u4f18\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u8def\u7531\u7ec4\u4ef6\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7 Webpack \u7684\u52a8\u6001 import \u7279\u6027\u5b9e\u73b0\u3002<\/p>\n<h3><a id=\"%E8%B7%AF%E7%94%B1%E6%87%92%E5%8A%A0%E8%BD%BD%E7%9A%84%E6%AD%A5%E9%AA%A4%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8def\u7531\u61d2\u52a0\u8f7d\u7684\u6b65\u9aa4\uff1a<\/h3>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 babel-plugin-syntax-dynamic-import\uff1a<\/strong><\/p>\n<pre><code class=\"language-bash\">npm install babel-plugin-syntax-dynamic-import --save-dev\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5728 babel.config.js \u4e2d\u914d\u7f6e\u63d2\u4ef6\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">module.exports = {\n  plugins: ['syntax-dynamic-import']\n};\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5728\u8def\u7531\u914d\u7f6e\u4e2d\u4f7f\u7528\u52a8\u6001 import\uff1a<\/strong><\/p>\n<pre><code class=\"language-javascript\">import Vue from 'vue';\nimport Router from 'vue-router';\n\nVue.use(Router);\n\nconst router = new Router({\n  routes: [\n    {\n      path: '\/about',\n      name: 'About',\n      component: () =&gt; import(\/* webpackChunkName: &quot;about&quot; *\/ '.\/views\/About.vue')\n    },\n    {\n      path: '\/contact',\n      name: 'Contact',\n      component: () =&gt; import(\/* webpackChunkName: &quot;contact&quot; *\/ '.\/views\/Contact.vue')\n    },\n    \/\/ \u5176\u4ed6\u8def\u7531...\n  ]\n});\n\nexport default router;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>import()<\/code> \u51fd\u6570\u88ab\u7528\u4e8e\u52a8\u6001\u5bfc\u5165\u7ec4\u4ef6\u3002Webpack \u4f1a\u5c06\u6bcf\u4e2a <code>import()<\/code> \u51fd\u6570\u7684\u5185\u5bb9\u62c6\u5206\u4e3a\u5355\u72ec\u7684 bundle\uff0c\u5b9e\u73b0\u4e86\u6309\u9700\u52a0\u8f7d\u8def\u7531\u7ec4\u4ef6\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u5728\u6bcf\u4e2a <code>import()<\/code> \u4e2d\u53ef\u4ee5\u4f7f\u7528\u6ce8\u91ca\uff08\u5982 <code>\/* webpackChunkName: &quot;about&quot; *\/<\/code>\uff09\u6765\u6307\u5b9a\u62c6\u5206\u51fa\u7684 bundle \u540d\u79f0\uff0c\u65b9\u4fbf\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u8bc6\u522b\u548c\u8c03\u8bd5\u3002<\/p>\n<p>\u901a\u8fc7\u8def\u7531\u61d2\u52a0\u8f7d\uff0c\u4f60\u53ef\u4ee5\u6309\u9700\u52a0\u8f7d\u8def\u7531\u7ec4\u4ef6\uff0c\u51cf\u5c11\u521d\u59cb\u52a0\u8f7d\u65f6\u7684\u8d44\u6e90\u5927\u5c0f\uff0c\u63d0\u9ad8\u9875\u9762\u52a0\u8f7d\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Vue \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8def\u7531\u61d2\u52a0\u8f7d\u6765\u52a8\u6001\u52a0\u8f7d\u8def\u7531\u7ec4\u4ef6\uff0c\u4ee5\u4f18\u5316\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u8def\u7531\u7ec4\u4ef6\u3002\u8fd9\u53ef [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114],"tags":[],"class_list":["post-7885","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7885"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7885\/revisions"}],"predecessor-version":[{"id":13978,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7885\/revisions\/13978"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7885"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}