vue本地開發(fā),請求線上域名會存在跨域問題,可在devServer里配置proxy解決
// .env.development文件內(nèi)配置
VUE_APP_BASE_API = '/api' // base api
VUE_APP_URL = 'http://www.xxx.com' // 線上地址
// vue.config.js文件
module.exports = {
//.....
devServer: {
host: 'localhost',
port: 8080,
open: true,
overlay: {
warnings: false,
errors: true
},
proxy: {
[process.env.VUE_APP_BASE_API]: {
target: process.env.VUE_APP_URL, // 請求到/api/xx 就會被代理到http://www.xxx.com/api/xxx
// secure: false, // 如果是https接口,需要配置這個參數(shù)
changeOrigin: true, // 接口跨域 需打開這個參數(shù)
pathRewrite: {
['^/' + process.env.VUE_APP_BASE_API]: '' // 忽略前綴/api 則會被代理到http://www.xxx.com/xxx
}
}
}
},
}