vue.config.js
脚手架搭建的项目,想要修改webpack配置,直接项目下面增加一个vue.config.js文件 会自动识别.然后配置本地代理proxyTable: {
'/api':{ //
target:'http://localhost:8083',
// secure:true, //如果是https的需要在这里设置成true
changeOrigin:true, //允许跨域
pathRewrite:{
"^/api":"" //
}
}
}
1.要理解pathRewrite,首先要明白proxyTable下‘/api’的作用。
使用代理, 首先需要有一个标识, 标明哪些连接需要使用代理,只有有标识的连接才用代理。”/api”就是告知,接口以”/api”开头的才用代理,所以写请求接口时要使用“/api/xx/xx”的形式,使用代理后生成的请求路径就是’http://localhost:8083/api/xx/xx’.
2.pathRewrite中 “^/api”:""的作用
// pathRewrite 的作用是把实际Request Url中的'/aps'用""代替, 比如你要请求的接口为http://localhost:8083/addPerson,则可以在页面中请求时写成/aps/addPerson