vue-cli跨域代理配置
阿新 • • 發佈:2018-04-03
vue js
參考了好多網上文章 配置代理總是不生效 ,其實不是配置的問題 是在調用的時候出的問題
把完整的調用過程整理下來希望可以幫助其他有疑惑的小夥伴
例如 服務地址是 http://192.168.0.125/test/login 的調用過程
1 找到config ->index.js文件 配置proxyTable參數
proxyTable: {
‘/api‘: {
target: ‘http://192.168.0.125/test‘,
changeOrigin: true,
pathRewrite: {
‘^/api‘: ‘/‘
}
}
/api 代替的是 ‘ http://192.168.0.125/test‘
2 工具 封裝
const service = axios.create({
timeout: 1000 * 30,
withCredentials: true,
baseURL:‘/api/‘,
headers: {
‘Content-Type‘: ‘application/json; charset=utf-8‘
}
})
3 action調用
// 登錄
export function login (params) {
return request({
url:‘login‘,
method: ‘post‘,
data: requestParam(params)
})
}
vue-cli跨域代理配置