Vue项目中 config/index.js
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: 'https://1.2.3.4:89', // 接口地址
changeOrigin: true, // 是否跨域
pathRewrite: { 、// 转发
'^/api': ''
},
secure: false
},
}
}
-
src/api/index.js
中如下配置 baseURL
const debug = process.env.NODE_ENV !== 'production'
const axInstance = axios.create({
baseURL: debug ? 'api' : 'http://1.3.4.5.6:89',
timeout: 10000,
responseType: 'json',
withCredentials: false, // 表示跨域请求时是否需要使用凭证
headers: {
token: store.state.axios.token,
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
}
})
- 配置完毕后,运行项目,访问接口
http://0.0.0.0:8080/api/getinfo
就会被转发到 https://1.2.3.4:89/getinfo