vue-cli3
先安装插件
npm add @cnamts/vue-cli-plugin-proxy # OR npm install @cnamts/vue-cli-plugin-proxy
在项目根目录下的vue.config.js(没有就新建)
请求地址:http:www/baidu.com/api
module.exports = {
//反向代理,跨域
pluginOptions: {
//反向代理,跨域
proxy: {
enabled: true,
context: '/api', //必填,与下面的koi无关联,只是命名
options: {
target: 'http:www/baidu.com/',
//secure: false, // 如果是https接口,需要配置这个参数(但是不加这句也可以)
changeOrigin: true, //是否跨域
// ws:true, //websocket
pathRewrite:{
// '^/koi':'',
// '^/koi/others':'/others'
}
}
}
}
}
全局使用,在mian.js
Vue.prototype.$axios = axios
axios.defaults.baseURL = "/api"