使用Hbuilder创建的vue-cli 2.x项目时,目录结构是没有vue.config.js的。
此时在项目根目录创建vue.config.js文件。并输入以下代码:
module.exports = {
// ...
devServer: {
proxy: {
'/api': {
//后端访问路径
target: 'http://127.0.0.1:9527',
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
在使用axios访问时,使用api作为前缀即可
this.axios.get('/api/'+url,{})
.then(res => {
}).catch(res=>{
})