{"id":31532,"date":"2024-11-25T17:03:12","date_gmt":"2024-11-25T09:03:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31532\/"},"modified":"2024-11-25T17:03:12","modified_gmt":"2024-11-25T09:03:12","slug":"%e5%ae%9e%e6%88%98%e9%a1%b9%e7%9b%ae%e7%bc%96%e8%af%91%e5%90%8e%e4%b8%8d%e5%9c%a8%e6%a0%b9%e7%9b%ae%e5%bd%95%e6%80%8e%e4%b9%88%e5%8a%9e","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31532\/","title":{"rendered":"\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\uff0c\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">eg. \uff1a<\/p>\n<p style=\"text-align: left\">vue-router: history\u6a21\u5f0f \u5185\u7f51\u73af\u5883\uff1a192.168.1.1:8080\/index.html \u5916\u7f51\u73af\u5883\uff1adomain.com\/ttsd\/index.html<\/p>\n<p style=\"text-align: left\">\u7531\u4e8e\u5f00\u53d1\u51fa\u6765\u7684\u9879\u76ee\u662f\u8981\u90e8\u7f72\u5728\u5ba2\u6237\u65b9\uff0c\u4e14\u5ba2\u6237\u5e76\u4e0d\u60f3\u5355\u72ec\u62ff\u4e00\u4e2a\u57df\u540d\uff08\u6216\u5b50\u57df\uff09\u6765\u90e8\u7f72\uff0c\u8fd9\u65f6\uff0c\u6253\u5305\u540e\u7684\u7a0b\u5e8f\u5c31\u8981\u4f5c\u4e00\u4e9b\u914d\u7f6e\u65b9\u9762\u7684\u4fee\u6539\u4e86\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u4fee\u6539<\/strong><\/p>\n<p style=\"text-align: left\">1\u3001\u628a\u6253\u5305\u540e\u7684\u8d44\u6e90\u5f15\u7528\u4fee\u6539\u4e3a\u76f8\u5bf9\u8def\u5f84 \u627e\u5230 config\/index.js \u4e2d build \u5c5e\u6027\u4e0b\u7684 assetsPublicPath<\/p>\n<pre>build: {\r\n ...\r\n assetsPublicPath: '.\/' \/\/ \u672a\u4fee\u6539\u524d\u7684\u914d\u7f6e\u4e3a '\/'\uff0c\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/f7bc3502ee2d042db36d645ba54d87e0-0.png\" class=\"aligncenter\" title=\"\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\u63d2\u56fe\" alt=\"\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\u63d2\u56fe\" \/><\/p>\n<p style=\"text-align: left\">2\u3001\u4fee\u6539\u6837\u5f0f\u5f15\u7528\u7684\u8d44\u6e90\u6587\u4ef6\uff08\u56fe\u7247\u3001\u3001\u5b57\u4f53\u6587\u4ef6\u7b49\uff09\u4e3a\u76f8\u5bf9\u8def\u5f84 \u627e\u5230 build\/utils.js \u4e2d\uff0c\u6dfb\u52a0(\u6216\u4fee\u6539) publicPath \u4e3a &#8216;..\/..\/&#8217;<\/p>\n<pre>if (options.extract) {\r\n return ExtractTextPlugin.extract({\r\n use: loaders,\r\n fallback: 'vue-style-loader',\r\n publicPath: '..\/..\/' \/\/ \u4fee\u6539\u8def\u5f84\r\n })\r\n} else {\r\n return ['vue-style-loader'].concat(loaders)\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/f7bc3502ee2d042db36d645ba54d87e0-1.png\" class=\"aligncenter\" title=\"\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\u63d2\u56fe1\" alt=\"\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\u63d2\u56fe1\" \/>&nbsp;<\/p>\n<p style=\"text-align: left\"><strong>\u4fee\u6539\u8def\u7531<\/strong><\/p>\n<p style=\"text-align: left\">\u5728\u8def\u7531\u7684history\u6a21\u5f0f\u4e0b\uff0c\u6240\u6709\u7684\u8def\u7531\u90fd\u662f\u57fa\u4e8e\u6839\u8def\u5f84\u7684\uff0c\u5982 \/xxxx \uff0c\u7531\u4e8e\u90e8\u7f72\u76ee\u5f55\u672a\u77e5\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u6839\u636e location.pathname \u6765\u83b7\u53d6\u5230\u5f53\u524d\u8bbf\u95ee\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u6765\u4fee\u6539\u8def\u7531\u3002<\/p>\n<p style=\"text-align: left\">vue-router\u91cc\u63d0\u4f9b\u4e86\u4e00\u4e2abase\u7684\u5c5e\u6027<\/p>\n<p style=\"text-align: left\">base\u7c7b\u578b: string \u9ed8\u8ba4\u503c: &#8220;\/&#8221; \u5e94\u7528\u7684\u57fa\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6574\u4e2a\u5355\u9875\u5e94\u7528\u670d\u52a1\u5728 \/app\/ \u4e0b\uff0c\u7136\u540e base \u5c31\u5e94\u8be5\u8bbe\u4e3a &#8220;\/app\/&#8221; \u3002<\/p>\n<p style=\"text-align: left\">\u4fee\u6539\u8def\u7531\u4ee3\u7801<\/p>\n<pre>function getAbsolutePath () {\r\n let path = location.pathname\r\n return path.substring(0, path.lastIndexOf('\/') + 1)\r\n}\r\nconst routers = new Router({\r\n mode: 'history',\r\n base: getAbsolutePath(),\r\n ...\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u81f3\u6b64\uff0c\u6253\u5305\u914d\u7f6e\u7684\u76f8\u5173\u4fee\u6539\u5df2\u5168\u90e8\u5b8c\u6210\uff0c\u9879\u76ee\u4e5f\u80fd\u591f\u6b63\u5e38\u8bbf\u95ee\u3002 \u4f46\u8fd8\u662f\u4f1a\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u8df3\u8f6c\u5230\u67d0\u4e2a\u8def\u7531\u540e\uff0c\u5237\u65b0\u9875\u9762\uff0c\u5c31gg\u4e86\uff0c\u9875\u9762\u4e3a\u7a7a\u767d\uff0c\u6b64\u65f6\u5c31\u8981\u4fee\u6539nginx\u7684\u914d\u7f6e\u4e86\u3002<\/p>\n<p style=\"text-align: left\"><strong>\u4fee\u6539nginx\u7684\u914d\u7f6e <\/strong><\/p>\n<p style=\"text-align: left\">\u5b98\u65b9\u7ed9\u7684nginx\u914d\u7f6e\u662f\u6839\u76ee\u5f55\u4e0b\u7684\uff0c\u5373 router.vuejs.org\/zh-cn\/essentials\/history-mode.html#nginx<\/p>\n<pre>location \/ {\r\n try_files $uri $uri\/ \/index.html;\r\n \/\/ \u9700\u8981\u4fee\u6539\u4e3a\r\n try_files $uri $uri\/ \/dist\/index.html;\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u6ce8\uff1a \/dist \u6839\u636e\u5b9e\u9645\u90e8\u7f72\u7684\u7f51\u7ad9\u76ee\u5f55\uff0c\u4fee\u6539\u4e00\u4e0b\u5c31\u53ef\u4ee5\u3002 \u4e2a\u4eba\u611f\u89c9\u8fd8\u53ef\u4ee5\u901a\u8fc7nginx\u5185\u7f6e\u7684\u6307\u4ee4\u53bb\u52a8\u6001\u83b7\u53d6\uff0c\u5728\u4e0b\u5c31\u4e0d\u592a\u6e05\u695a\u4e86\u3002<\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\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>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\uff0c\u5b9e\u6218\u9879\u76ee\u7f16\u8bd1\u540e\u4e0d\u5728\u6839\u76ee\u5f55\u600e\u4e48\u529e\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 eg. \uff1a vue-router: history\u6a21\u5f0f \u5185\u7f51\u73af\u5883\uff1a192.168.1.1:8080\/index.html \u5916\u7f51\u73af\u5883\uff1adomain.com\/ttsd\/index.html \u7531\u4e8e\u5f00\u53d1\u51fa\u6765\u7684\u9879\u76ee\u662f\u8981\u90e8\u7f72\u5728\u5ba2\u6237\u65b9\uff0c\u4e14\u5ba2\u6237\u5e76\u4e0d\u60f3\u5355\u72ec\u62ff\u4e00\u4e2a\u57df\u540d\uff08\u6216\u5b50\u57df\uff09\u6765\u90e8\u7f72\uff0c\u8fd9\u65f6\uff0c\u6253\u5305\u540e\u7684\u7a0b\u5e8f\u5c31\u8981\u4f5c\u4e00\u4e9b\u914d\u7f6e\u65b9\u9762\u7684\u4fee\u6539\u4e86\u3002 \u4fee\u6539 1\u3001\u628a\u6253\u5305\u540e\u7684\u8d44\u6e90\u5f15\u7528\u4fee\u6539\u4e3a\u76f8\u5bf9\u8def\u5f84 \u627e\u5230 config\/index.js \u4e2d build \u5c5e\u6027\u4e0b\u7684 assetsPublicPath build: { &#8230; assetsPublicPath: &#8216;.\/&#8217; \/\/ \u672a\u4fee\u6539\u524d\u7684\u914d\u7f6e\u4e3a &#8216;\/&#8217;\uff0c } \u767b\u5f55\u540e\u590d\u5236 2\u3001\u4fee\u6539\u6837\u5f0f\u5f15\u7528\u7684\u8d44\u6e90\u6587\u4ef6\uff08\u56fe\u7247\u3001\u3001\u5b57\u4f53\u6587\u4ef6\u7b49\uff09\u4e3a\u76f8\u5bf9\u8def\u5f84 \u627e\u5230 build\/utils.js \u4e2d\uff0c\u6dfb\u52a0(\u6216\u4fee\u6539) publicPath \u4e3a &#8216;..\/..\/&#8217; if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, fallback: &#8216;vue-style-loader&#8217;, publicPath: &#8216;..\/..\/&#8217; \/\/ \u4fee\u6539\u8def\u5f84 }) } else { return [&#8216;vue-style-loader&#8217;].concat(loaders) } \u767b\u5f55\u540e\u590d\u5236 &nbsp; [&hellip;]<\/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-31532","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31532","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=31532"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31532\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}