第一步在站点根目录下新建vue.config.js(注:针对最新版Vue)
module.exports = {
assetsDir: 'static',
publicPath:'./',
devServer: {
historyApiFallback: true,
watchOptions: {
aggregateTimeout: 300,
poll: 1000
},
disableHostCheck: true,
port: 8080,
proxy: {
"/api": {
target: '你的域名地址/api', // 前端域名
changeOrigin: true, // 如果接⼝跨域,需要进⾏这个参数配置
pathRewrite: {
"^/api": ""
},
},
},
},第二步打开路径src-->main.js文件,增加以下代码
import VueAxios from 'vue-axios';
import axios from 'axios';
Vue.prototype.$axios = axios;
axios.defaults.baseURL = '/api' //关键代码第三部请求写法如下
that.$axios.get("API具体方法路径,比如/data/get_data", {
params: {
type_id: current //这里填写传递的参数
}
}).then(function(res) {
that.lists = res.data.lists;
}, function(err) {
console.log(err);
});以上可以解决axios请求API本地调试跨域问题