解决 Vue 中前后端跨域问题的代码如下:
- 在 Vue 项目的根目录下创建 vue.config.js 文件,在该文件中添加代码:
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}