Vue 跨域配置以及 process.env.NODE_ENV
因为有的时候我们会不停的本地调试和发布到服务器上,因此 相关的服务前缀就要更改
打包时这个全局变量会被赋值
当 npm run dev,本地调试时
main.js
我们可以写成
if (process.env.NODE_ENV !== 'development') {
Vue.prototype.URL_PREFIX = '..'
} else {
Vue.prototype.URL_PREFIX = 'http://localhost:60589'
}
使用
url = this.URL_PREFIX + "/ReqRpt216/savecoordinate";
或者
config文件夹中,打开index.js,在proxyTable一项中设置:
// 'http://localhost:7890' 表示的是要跨域请求的地址
// 如果请求的地址是:'http://localhost:7890/index_data'
// 在请求时就可以写成: '/apis/index_data'
'/apis': {
target: 'http://localhost:7890',
changeOrigin: true,
pathRewrite: {
'^/apis': ''
}
}