当前位置:首页 > Web开发 > 正文

// 是否跨域 pathRewrite: { " ^/api " : "" // 用‘/api‘ 代替 ‘ :3005

2024-03-31 Web开发

标签:

目的:在配置文件config/index.js中配置统一请求接口

在配置文件config/index.js找到

module.exports={ dev:{ proxyTable:{} } }

变动为:

proxyTable:{ "/api": { target: "", //设置挪用的接口域名和端口 changeOrigin: true, pathRewrite: { "^/api": "" //用‘/api‘ 取代 ‘ } } }

例如:请求地点为:"http://218.78.187.216/api/v1/authentication"
请求的时候可以写为:

this.$axios.post("/api/anthentication",params).then(res=>{ console.log(res); },err=>{ console.log(err); })

proxyTable: { "/api": { target: ":3005/api", //设置挪用的接口域名和端口 changeOrigin: true, //是否跨域 pathRewrite: { "^/api": "" //用‘/api‘ 取代 ‘:3005/api‘ } } },

使用axios挪用示例:

getlunbo() { let _this = this; _this.$axios.get(/api/getlunbo).then(function(result){ console.log(result) }) }

在浏览器中看到的访谒地点:

其实真正访谒的地点是: :3005/api/getlunbo

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32523.html