前言
在实际项目中,本地开发环境请求服务器接口地址的时候,会存在跨域问题,解决跨域问题,可以关闭浏览器跨域限制,或者使用webpack-dev-server的proxy代理
第一种方式,前面有文章介绍过:各浏览器开启跨域模式
现在说明第二种方式:
1、在webpack.config.js中配置
devServer: {
...
proxy: {
'/service-core': {
target: 'https://test.******.org', // 接口的域名
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
}
}
}
2、vue-cli中proxyTable配置接口地址代理
修改config/index.js
proxyTable: {
'/service-core': {
target: 'https://test.******.org', // 接口的域名
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
}
}
以上配置完以后,然后将dev环境的baseUrl改为
const devUrl = '/service-core'
最后,重启服务