如何實現線上環境使用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': ""
}
}
},
用的vue-resource,理論上思路是一樣的。
proxyTable
和nginx
的反向代理是一样的道理,拦截特定的url
,轉送到其他伺服器。可以配置一個環境變量,透過判斷環境變數來決定使用哪一種配置
設定後, npn run dev階段, 本地如果訪問'/get/apple, 本地伺服器會幫你訪問http://api.com:6688/get/apple拿到遠端的資料, 變相的實現了跨域功能
開啟config/index.js, 新增proxyTable屬性
module.exports = {
}
https://github.com/383514580/...