{"id":1577,"date":"2023-03-25T19:23:55","date_gmt":"2023-03-25T11:23:55","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1577"},"modified":"2023-04-27T21:31:42","modified_gmt":"2023-04-27T13:31:42","slug":"config-axios-to-cross-domain-multiple-proxytable-proxies-in-vue","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/config-axios-to-cross-domain-multiple-proxytable-proxies-in-vue\/","title":{"rendered":"Vue\u4e2d\u914d\u7f6eaxios\u8de8\u57df\u591a\u4e2aproxyTable\u4ee3\u7406"},"content":{"rendered":"<h3>vue.config.js<\/h3>\n<blockquote>\n<p>\u914d\u7f6e\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/cli.vuejs.org\/config\/#devserver-proxy\">https:\/\/cli.vuejs.org\/config\/#devserver-proxy<\/a><\/p>\n<\/blockquote>\n<p><!-- more --><\/p>\n<pre><code class=\"language-javascript\">devServer: {\n  open: true,\n  port: 8081,\n  proxy: {\n    \/\/\u5339\u914d\/gw1-api\u5f00\u5934\u7684\u8bf7\u6c42\n    [process.env.VUE_APP_GW1_BASE_API]: {\n      \/\/\u76ee\u6807\u670d\u52a1\u5668\uff0c\u4ee3\u7406\u8bbf\u95ee\u5230https:\/\/localhost:8080\n      target: process.env.VUE_APP_GW1_URL,\n      \/\/ \u5f00\u542f\u4ee3\u7406\uff1a\u5728\u672c\u5730\u4f1a\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u670d\u52a1\u7aef\uff0c\u7136\u540e\u53d1\u9001\u8bf7\u6c42\u7684\u6570\u636e\uff0c\n      \/\/ \u5e76\u540c\u65f6\u63a5\u6536\u8bf7\u6c42\u7684\u6570\u636e\uff0c\u8fd9\u6837\u670d\u52a1\u7aef\u548c\u670d\u52a1\u7aef\u8fdb\u884c\u6570\u636e\u7684\u4ea4\u4e92\u5c31\u4e0d\u4f1a\u6709\u8de8\u57df\u95ee\u9898\n      changOrigin: true, \/\/\u5f00\u542f\u4ee3\u7406\n      pathRewrite: {\n        \/\/ \u4f1a\u5c06 \/gw1-api \u66ff\u6362\u4e3a &#039;&#039;,\u4e5f\u5c31\u662f \/gw1-api \u4f1a\u79fb\u9664\uff0c\n        \/\/ \u5982 \/gw1-api\/db.json \u4ee3\u7406\u5230 https:\/\/localhost:8080\/db.json\n        \/\/ &#039;^\/gw1-api&#039;: &quot;&quot;,\n        [&quot;^&quot; + process.env.VUE_APP_GW1_BASE_API]: &quot;&quot;\n      }\n    },\n    \/\/\u5339\u914d\/gw2-api\u5f00\u5934\u7684\u8bf7\u6c42\n    [process.env.VUE_APP_GW2_BASE_API]: {\n      \/\/\u76ee\u6807\u670d\u52a1\u5668\uff0c\u4ee3\u7406\u8bbf\u95ee\u5230https:\/\/localhost:8080\n      target: process.env.VUE_APP_GW2_URL,\n      \/\/ \u5f00\u542f\u4ee3\u7406\uff1a\u5728\u672c\u5730\u4f1a\u521b\u5efa\u4e00\u4e2a\u865a\u62df\u670d\u52a1\u7aef\uff0c\u7136\u540e\u53d1\u9001\u8bf7\u6c42\u7684\u6570\u636e\uff0c\n      \/\/ \u5e76\u540c\u65f6\u63a5\u6536\u8bf7\u6c42\u7684\u6570\u636e\uff0c\u8fd9\u6837\u670d\u52a1\u7aef\u548c\u670d\u52a1\u7aef\u8fdb\u884c\u6570\u636e\u7684\u4ea4\u4e92\u5c31\u4e0d\u4f1a\u6709\u8de8\u57df\u95ee\u9898\n      changOrigin: true, \/\/\u5f00\u542f\u4ee3\u7406\n      pathRewrite: {\n        \/\/ \u4f1a\u5c06 \/gw2-api \u66ff\u6362\u4e3a &#039;&#039;,\u4e5f\u5c31\u662f \/gw2-api \u4f1a\u79fb\u9664\uff0c\n        \/\/ \u5982 \/gw2-api\/db.json \u4ee3\u7406\u5230 https:\/\/localhost:8080\/db.json\n        \/\/ &#039;^\/gw2-api&#039;: &quot;&quot;,\n        [&quot;^&quot; + process.env.VUE_APP_GW2_BASE_API]: &quot;&quot;\n      }\n    },\n  }\n},<\/code><\/pre>\n<h3>.env<\/h3>\n<pre><code class=\"language-javascript\"># \u4f7f\u7528 VUE_APP_ \u5f00\u5934\u7684\u53d8\u91cf\u4f1a\u88ab webpack \u81ea\u52a8\u52a0\u8f7d\n# \u5b9a\u4e49\u8bf7\u6c42\u7684\u57fa\u7840URL, \u65b9\u4fbf\u8de8\u57df\u8bf7\u6c42\u65f6\u4f7f\u7528\n\nVUE_APP_GW1_BASE_API = &#039;\/gw1-api&#039;\nVUE_APP_GW2_BASE_API = &#039;\/gw2-api&#039;\n\n# \u63a5\u53e3\u670d\u52a1\u5730\u5740\uff08\u7f51\u5173\u5730\u5740\uff09\n# \u53ef\u66f4\u6539\u4e3a\u81ea\u5df2\u914d\u7f6e\u7684 Easy-Mock \u63a5\u53e3\u670d\u52a1\u5730\u5740\n\nVUE_APP_GW1_URL = &#039;http:\/\/192.168.1.10:8001\/gateway&#039;\nVUE_APP_GW2_URL = &#039;http:\/\/192.168.1.10:8002\/gateway&#039;<\/code><\/pre>\n<h3>api<\/h3>\n<pre><code class=\"language-javascript\">export function test1(data) {\n  return request({\n    url: &#039;\/gw1-api\/test1&#039;,\n    method: &#039;post&#039;,\n    data,\n  });\n}\nexport function test2(data) {\n  return request({\n    url: &#039;\/gw2-api\/test2&#039;,\n    method: &#039;post&#039;,\n    data,\n  });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>vue.config.js \u914d\u7f6e\u53c2\u8003\uff1ahttps:\/\/cli.vuejs.org\/config\/#devser [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1577","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1577"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1577\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}