vue2 添加代理解决请求跨域和路径别名的使用

在package.json 同级目录 新建 vue.config.js 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const path = require('path');

function resolve(dir) {
return path.join(__dirname, dir);
}
console.log(resolve('src'))
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'https://api.domain.com/',
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true,
},
'/public': {
target: 'https://api.domain.com/',
secure: false,
changeOrigin: true,
}
}
},
chainWebpack: (config) => {
config.resolve.alias
.set('@', resolve('src'))// import xx from '@/xx/xx/' 时会自动补全src目录路径
}
}

另外在修改vue.config.js后需要重新 yarn serve