~~~~~ 在Vue.js中,可以通过配置代理服务器来解决跨域请求的问题。代理服务器可以将请求转发到目标服务器,并将响应返回给Vue应用。
在Vue项目中配置代理服务器可以通过以下方法实现:
配置vue.config.js
module.exports = {
lintOnSave:false /*关闭语法检查*/,
pages: {
index: {
// page 的入口
entry: 'src/main.js',
}
},
//开启代理服务器(方式一)
// devServer: {
// proxy: 'http://localhost:5000'
// },
//开启代理服务器(方式二)
devServer: {
proxy: {
'/api': {
target: 'http://localhost:5000',
pathRewrite:{
'^/api':''},
// ws: true,//用于支持websocket,默认值为true
// changeOrigin: true //用于控制请求头中的host值,默认值为true
},
'/demo': {
target: 'http://localhost:5001',
pathRewrite:{
'^/demo':''},
// ws: true,//用于支持websocket,默认值为true
// changeOrigin: true //用于控制请求头中的host值,默认值为true
}
}
}
}