javascript - vue-cli proxyTable怎么配置
问题描述
如何实现线上环境使用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/...
相关文章:
1. 浅谈vue生命周期共有几个阶段?分别是什么?2. index.php错误,求指点3. css3 - 使用less编译css后,后期的项目中less是直接放在项目文件中,后期如何维护呢4. html5 - 前端面试碰到了一个缓存数据的问题,来论坛上请教一下5. css3 - 在css里面,样式不生效问题6. javascript - Jquary的contains如何做到精准匹配7. 视频 - html5 video的autoplay 在智能手機上不運作?8. macos - mac下docker如何设置代理9. javascript - ui-router AngularJS url显示正常 页面没有变化?10. 微信公众号在线生成二维码带参数怎么搞?
