{"id":272885,"date":"2024-05-16T13:26:22","date_gmt":"2024-05-16T05:26:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/272885.html"},"modified":"2024-05-16T13:26:33","modified_gmt":"2024-05-16T05:26:33","slug":"nuxt%e9%a1%b9%e7%9b%ae%e5%a6%82%e4%bd%95%e5%9c%a8%e8%b7%af%e7%94%b1%e5%9c%b0%e5%9d%80%e4%b8%8a%e6%b7%bb%e5%8a%a0-html%e5%90%8e%e7%bc%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/272885.html","title":{"rendered":"nuxt\u9879\u76ee\u5982\u4f55\u5728\u8def\u7531\u5730\u5740\u4e0a\u6dfb\u52a0.html\u540e\u7f00"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195035\/10b3e808-91e5-4bee-8a40-53029580165a.webp\" alt=\"nuxt\u9879\u76ee\u5982\u4f55\u5728\u8def\u7531\u5730\u5740\u4e0a\u6dfb\u52a0.html\u540e\u7f00\" \/><\/p>\n<p><p>Nuxt\u9879\u76ee\u5728\u8def\u7531\u5730\u5740\u4e0a\u6dfb\u52a0.html\u540e\u7f00\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6eNuxt.js\u7684\u8def\u7531\u6269\u5c55\u548c\u751f\u6210\u89c4\u5219\u6765\u5b9e\u73b0\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u4f7f\u7528Nuxt\u7684<code>extendRoutes<\/code>\u65b9\u6cd5\u81ea\u5b9a\u4e49\u8def\u7531\u914d\u7f6e\u3001\u5728<code>nuxt.config.js<\/code>\u4e2d\u4fee\u6539\u8def\u7531\u89c4\u5219\u3001\u4ee5\u53ca\u5229\u7528\u751f\u6210\u914d\u7f6e\u6765\u5e94\u5bf9\u9759\u6001\u7ad9\u70b9\u7684\u9700\u8981\u3002<\/p>\n<\/p>\n<p><p><strong>\u5173\u952e\u6b65\u9aa4<\/strong>\u5305\u62ec\u4fee\u6539<code>nuxt.config.js<\/code>\u6587\u4ef6\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u3001\u8bbe\u7f6egenerate\u5c5e\u6027\u3002\u5bf9\u4e8e\u52a8\u6001\u8def\u7531\uff0c\u8fd8\u9700\u8981\u7279\u522b\u6ce8\u610f\u91c7\u7528\u5408\u9002\u7684\u65b9\u6cd5\u6765\u751f\u6210\u5177\u6709.html\u540e\u7f00\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001NUXT\u914d\u7f6e\u8c03\u6574<\/h3>\n<\/p>\n<p><p>\u5728Nuxt\u9879\u76ee\u4e2d\u6dfb\u52a0<code>.html<\/code>\u540e\u7f00\uff0c\u9996\u5148\u9700\u8981\u5728<code>nuxt.config.js<\/code>\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u914d\u7f6e\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">export default {<\/p>\n<p>  router: {<\/p>\n<p>    extendRoutes(routes, resolve) {<\/p>\n<p>      routes.forEach((route) =&gt; {<\/p>\n<p>        route.path = route.path + &#039;.html&#039;;<\/p>\n<p>      });<\/p>\n<p>    }<\/p>\n<p>  },<\/p>\n<p>  generate: {<\/p>\n<p>    subFolders: false \/\/ \u9759\u6001\u7ad9\u70b9\u751f\u6210\u914d\u7f6e<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u914d\u7f6e\u4e2d\uff0c<code>extendRoutes<\/code>\u7528\u4e8e\u81ea\u5b9a\u4e49\u8def\u7531\uff0c\u901a\u8fc7\u904d\u5386\u6240\u6709\u8def\u7531\u7136\u540e\u5728\u8def\u5f84\u540e\u9762\u6dfb\u52a0<code>.html<\/code>\u540e\u7f00\u3002<code>generate.subFolders<\/code>\u8bbe\u7f6e\u4e3a<code>false<\/code>\u610f\u5473\u7740\u5f53\u4f60\u8fd0\u884c<code>nuxt generate<\/code>\u751f\u6210\u9759\u6001\u6587\u4ef6\u65f6\uff0c\u4e0d\u4f1a\u4e3a\u6bcf\u4e2a\u8def\u7531\u751f\u6210\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u800c\u662f\u76f4\u63a5\u5728\u8def\u7531\u540d\u79f0\u540e\u6dfb\u52a0<code>.html<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u52a8\u6001\u8def\u7531\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u52a8\u6001\u8def\u7531\uff08\u5982<code>pages\/_slug.vue<\/code>\uff09\uff0c\u5982\u679c\u8981\u6dfb\u52a0<code>.html<\/code>\u540e\u7f00\uff0c\u8fd8\u9700\u8981\u5728<code>extendRoutes<\/code>\u4e2d\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">export default {<\/p>\n<p>  router: {<\/p>\n<p>    extendRoutes(routes, resolve) {<\/p>\n<p>      for (let i = 0; i &lt; routes.length; i++) {<\/p>\n<p>        if (routes[i].name === &#039;slug&#039;) {<\/p>\n<p>          routes[i] = {<\/p>\n<p>            ...routes[i],<\/p>\n<p>            path: &#039;\/:slug.html&#039;,<\/p>\n<p>            component: routes[i].component<\/p>\n<p>          };<\/p>\n<p>        }<\/p>\n<p>      }<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u89c4\u5219\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u6267\u884c<code>nuxt generate<\/code>\u6765\u9884\u6e32\u67d3\u4f60\u7684Nuxt\u5e94\u7528\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u751f\u6210\u7684\u6587\u4ef6\u8def\u7531\u8fdb\u884c\u8fdb\u4e00\u6b65\u5b9a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">export default {<\/p>\n<p>  generate: {<\/p>\n<p>    subFolders: false, \/\/ \u751f\u6210\u6587\u4ef6\u800c\u975e\u6587\u4ef6\u5939<\/p>\n<p>    routes: function () {<\/p>\n<p>      return axios.get(&#039;https:\/\/my-api\/posts&#039;)<\/p>\n<p>        .then((res) =&gt; {<\/p>\n<p>          return res.data.map((post) =&gt; {<\/p>\n<p>            return &#039;\/posts\/&#039; + post.id + &#039;.html&#039;;<\/p>\n<p>          });<\/p>\n<p>        });<\/p>\n<p>    }<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5237\u65b0404\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5f53\u76f4\u63a5\u8bbf\u95ee\u6dfb\u52a0\u4e86<code>.html<\/code>\u540e\u7f00\u7684\u9875\u9762\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230404\u9519\u8bef\u3002\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u9700\u8981\u670d\u52a1\u5668\u7aef\u7684\u914d\u7f6e\u914d\u5408\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5bf9\u4e8eNginx\uff1a<\/strong><\/li>\n<\/ul>\n<p><pre><code class=\"language-nginx\">location \/ {<\/p>\n<p>  try_files $uri $uri\/ $uri.html \/index.html;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>\u5bf9\u4e8eApache\uff1a<\/strong><\/li>\n<\/ul>\n<p><p>\u4f60\u9700\u8981\u901a\u8fc7<code>.htaccess<\/code>\u6587\u4ef6\u4fee\u6539mod_rewrite\u89c4\u5219\uff0c\u5141\u8bb8Apache\u91cd\u5199\u5e26\u6709.html\u540e\u7f00\u7684URL\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-apache\">RewriteEngine On<\/p>\n<p>RewriteCond %{REQUEST_FILENAME} !-f<\/p>\n<p>RewriteRule ^(.*)\\.html$ \/$1 [L,QSA]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u8bb0\u4f4f\uff0c<strong>\u8bbe\u7f6e\u670d\u52a1\u5668\u91cd\u5199\u89c4\u5219<\/strong>\u7ed3\u5408Nuxt\u914d\u7f6e\uff0c\u4ee5\u786e\u4fdd\u6574\u4e2a\u5e94\u7528\u987a\u7545\u5730\u9002\u5e94\u4e86.html\u540e\u7f00\u7684\u6539\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6d4b\u8bd5\u4fdd\u8bc1\u529f\u80fd\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e0a\u8ff0\u914d\u7f6e\u66f4\u6539\u540e\uff0c\u4e3a\u786e\u4fdd\u4e00\u5207\u6309\u9884\u671f\u5de5\u4f5c\uff0c\u4f60\u9700\u8981\u8fdb\u884c\u5f7b\u5e95\u7684\u6d4b\u8bd5\u3002\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u672c\u5730\u5f00\u53d1\u73af\u5883<\/strong>\u4e2d\u6d4b\u8bd5\u8def\u7531\u662f\u5426\u6b63\u786e\u5e76\u4e14\u9875\u9762\u52a0\u8f7d\u65e0\u8bef\u3002<\/li>\n<li>\u5bf9\u4e8e<strong>\u9759\u6001\u751f\u6210<\/strong>\u7684Nuxt\u5e94\u7528\uff0c\u786e\u8ba4\u6784\u5efa\u751f\u6210\u7684\u8f93\u51fa\u662f\u5426\u4e5f\u5305\u542b\u4e86<code>.html<\/code>\u540e\u7f00\u3002<\/li>\n<li><strong>\u90e8\u7f72\u81f3\u670d\u52a1\u5668<\/strong>\u540e\u786e\u8ba4\u5237\u65b0\u9875\u9762\u548c\u76f4\u63a5\u8bbf\u95ee\u9875\u9762\u662f\u5426\u6b63\u5e38\uff0c\u65e0404\u9519\u8bef\u53d1\u751f\u3002<\/li>\n<\/ul>\n<p><p>\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610fNuxt\u7248\u672c\u7684\u66f4\u65b0\uff0c\u56e0\u4e3aNuxt\u4e0d\u65ad\u8fdb\u5316\uff0c\u4f60\u7684\u914d\u7f6e\u53ef\u80fd\u9700\u8981\u968f\u4e4b\u66f4\u65b0\u4ee5\u4fdd\u6301\u517c\u5bb9\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3\u8d77\u6765<\/strong>\uff0c\u5728Nuxt\u9879\u76ee\u4e2d\u6dfb\u52a0.html\u540e\u7f00\u6d89\u53ca\u7684\u5173\u952e\u6b65\u9aa4\u662f\u4fee\u6539Nuxt\u7684\u8def\u7531\u914d\u7f6e\u548c\u9759\u6001\u6587\u4ef6\u751f\u6210\u89c4\u5219\uff0c\u4ee5\u53ca\u786e\u4fdd\u670d\u52a1\u5668\u914d\u7f6e\u6b63\u786e\u5904\u7406\u65b0\u7684URL\u7ed3\u6784\u3002\u8fd9\u4e00\u8fc7\u7a0b\u9700\u8981\u4f60\u5bf9Nuxt\u548c\u670d\u52a1\u5668\u7aef\u914d\u7f6e\u6709\u6df1\u5165\u7684\u7406\u89e3\uff0c\u5e76\u901a\u8fc7\u4e00\u7cfb\u5217\u7684\u6d4b\u8bd5\u6765\u4fdd\u8bc1\u5e94\u7528\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u8bbe\u7f6eNuxt\u9879\u76ee\u8def\u7531\u5730\u5740\u4e0a\u6dfb\u52a0.html\u540e\u7f00\uff1f<\/strong><\/p>\n<p>\u6dfb\u52a0.html\u540e\u7f00\u53ef\u4ee5\u4f7f\u5f97\u7f51\u7ad9\u7684URL\u66f4\u52a0\u89c4\u8303\uff0c\u5e76\u4e14\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u53ef\u80fd\u6709\u4e00\u5b9a\u7684\u4f18\u52bf\u3002\u5728Nuxt\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u8def\u7531\u914d\u7f6e\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6253\u5f00Nuxt\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684<code>nuxt.config.js<\/code>\u6587\u4ef6\u3002\u7136\u540e\uff0c\u5728\u8be5\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">export default {\n  router: {\n    extendRoutes(routes, resolve) {\n      routes.forEach(route =&gt; {\n        route.path = route.path + &quot;.html&quot;;\n      });\n    }\n  }\n}\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u4ee5\u5c06\u6240\u6709\u8def\u7531\u5730\u5740\u90fd\u6dfb\u52a0.html\u540e\u7f00\u3002\u5982\u679c\u4f60\u53ea\u60f3\u7ed9\u7279\u5b9a\u7684\u8def\u7531\u6dfb\u52a0.html\u540e\u7f00\uff0c\u53ef\u4ee5\u5728<code>extendRoutes<\/code>\u4e2d\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n<p><strong>2. \u5728Nuxt\u9879\u76ee\u4e2d\u5982\u4f55\u5b9e\u73b0\u8def\u7531\u5730\u5740\u6dfb\u52a0.html\u540e\u7f00\u7684\u6548\u679c\uff1f<\/strong><\/p>\n<p>Nuxt\u7684\u8def\u7531\u7cfb\u7edf\u57fa\u4e8eVue Router\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u8def\u7531\u914d\u7f6e\u6765\u5b9e\u73b0\u8def\u7531\u5730\u5740\u6dfb\u52a0.html\u540e\u7f00\u7684\u6548\u679c\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u4f60\u7684Nuxt\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684<code>nuxt.config.js<\/code>\u6587\u4ef6\u4e2d\u627e\u5230<code>router<\/code>\u914d\u7f6e\u9879\u3002\u5c06<code>router<\/code>\u914d\u7f6e\u9879\u4fee\u6539\u4e3a\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">export default {\n  router: {\n    tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lingSlash: true,\n    extendRoutes(routes, resolve) {\n      routes.forEach(route =&gt; {\n        if (!route.path.endsWith(&#039;.html&#039;)) {\n          route.path = route.path + &quot;.html&quot;;\n        }\n      });\n    }\n  }\n}\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4f1a\u7ed9\u6240\u6709\u8def\u7531\u5730\u5740\u6dfb\u52a0.html\u540e\u7f00\uff0c\u5e76\u4e14\u901a\u8fc7<code>trailingSlash: true<\/code>\u914d\u7f6e\u9879\u6765\u4fdd\u8bc1\u5730\u5740\u672b\u5c3e\u90fd\u6709\u659c\u6760\u3002<\/p>\n<p><strong>3. Nuxt\u9879\u76ee\u4e2d\u6dfb\u52a0.html\u540e\u7f00\u5bf9SEO\u6709\u4ec0\u4e48\u5f71\u54cd\uff1f<\/strong><\/p>\n<p>\u5728\u7ed9Nuxt\u9879\u76ee\u7684\u8def\u7531\u5730\u5740\u6dfb\u52a0.html\u540e\u7f00\u4e4b\u540e\uff0c\u5bf9SEO\u6709\u4ee5\u4e0b\u51e0\u70b9\u5f71\u54cd\uff1a<\/p>\n<ul>\n<li>\u66f4\u89c4\u8303\u7684URL\uff1a\u6dfb\u52a0.html\u540e\u7f00\u53ef\u4ee5\u4f7f\u5f97URL\u66f4\u52a0\u89c4\u8303\uff0c\u63d0\u9ad8\u641c\u7d22\u5f15\u64ce\u5bf9\u7f51\u7ad9\u7684\u7406\u89e3\u548c\u6536\u5f55\u6548\u679c\u3002<\/li>\n<li>\u9759\u6001\u5316\u4f18\u52bf\uff1a\u6dfb\u52a0.html\u540e\u7f00\u53ef\u4ee5\u4f7f\u5f97\u9875\u9762\u5730\u5740\u66f4\u50cf\u9759\u6001\u9875\u9762\uff0c\u6709\u52a9\u4e8e\u52a0\u901f\u7f51\u7ad9\u7684\u52a0\u8f7d\u901f\u5ea6\u548c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/li>\n<li>\u4fdd\u62a4\u76ee\u5f55\u7ed3\u6784\uff1a\u6dfb\u52a0.html\u540e\u7f00\u53ef\u4ee5\u9690\u85cf\u76ee\u5f55\u7ed3\u6784\uff0c\u589e\u52a0\u7f51\u7ad9\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<p>\u7136\u800c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5b9e\u65bd\u8fd9\u79cd\u4fee\u6539\u4e4b\u524d\uff0c\u4f60\u5e94\u8be5\u614e\u91cd\u8bc4\u4f30\u5bf9\u5df2\u6709\u94fe\u63a5\u548c\u641c\u7d22\u5f15\u64ce\u6536\u5f55\u7684\u5f71\u54cd\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u91cd\u5b9a\u5411\u5904\u7406\uff0c\u4ee5\u514d\u9020\u6210\u8d1f\u9762\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Nuxt\u9879\u76ee\u5728\u8def\u7531\u5730\u5740\u4e0a\u6dfb\u52a0.html\u540e\u7f00\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6eNuxt.js\u7684\u8def\u7531\u6269\u5c55\u548c\u751f\u6210\u89c4\u5219\u6765\u5b9e\u73b0\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":272897,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/272885"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=272885"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/272885\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/272897"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=272885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=272885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=272885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}