您的位置:首頁技術文章
文章詳情頁

javascript - vue-cli proxyTable怎么配置

瀏覽:91日期:2023-09-14 18:42:15

問題描述

如何實現線上環境使用setting.host + ’/api/sop/’,本地dev請求localhost:3000呢?

const instance = axios.create({ baseURL: setting.host + ’/api/sop/’, timeout: 20000, headers: { ’Content-Type’: ’application/json’, ’Accept’: ’application/json’, },});

config

proxyTable: { ’/api’: { target: 'http://127.0.0.1:3000', changeOrigin: true, pathRewrite: { ’^/api’: '' } }},

問題解答

回答1:

用的vue-resource,理論上思路是一樣的。proxyTable和nginx的反向代理是一樣的道理,攔截特定的url,轉發到其他服務器。

// configproxyTable: { ’/api’: { target: ’http://10.0.0.10:8080’, changeOrigin: true, pathRewrite: { ’^/api’: ’/api’ } }}// codethis.$http.post(’/api/login’,{ username: ’xxx’, password: ’xxx’}).then((response) => { // ...}, (response) => { // ...});# 生產環境 nginxlocation /api { proxy_pass http://10.0.0.10:8080/api;}回答2:

可以配置一個環境變量,通過判斷環境變量確定使用哪一種配置

process.NODE_ENV === ’LOCAL’ ? proxyTableLocal : proxyTableServer回答3:

設置后, npn run dev階段, 本地如果訪問’/get/apple, 本地服務器會幫你訪問http://api.com:6688/get/apple拿到遠程的數據, 變相的實現了跨域功能

打開config/index.js, 添加proxyTable屬性

module.exports = {

build: {...}dev: { ... proxyTable: {’/’: { target: ’http://api.com:6688’, changeOrigin: true } }, ...}

}

https://github.com/383514580/...

標簽: JavaScript
相關文章:
国产综合久久一区二区三区