在dev环境下面:ios
proxyTable: { '/api': { target: 'http://api.douban.com/v2', //主域名,之前我都写192.168.2.57:80,这里跨域了 changeOrigin: true, //容许跨域 pathRewrite: { '^/api': '' } //重写路径,其实这里就是和上面的target拼接起来 } }
之前的老项目里是写成这样的:axios
proxyTable: { "/middleware": { target: "http://192.168.2.57:80", pathRewrite: { '^/middleware': '/patient/1116/guangji/middleware' //上下拼接,组成完整的请求路径 }, changeOrigin: true, } }
配合axios使用:api
//在main.js中: Vue.prototype.HOST = '/api' //将api设置成全局属性,而后再给每一个请求地址引用一下。 //这里必须这样写,我试了一下若是不在全局注册api的话,它仍是会去请求192.168.2.57:8888下面的地址 //在代码里: this.axios.get(this.HOST + '/movie/in_theaters').then((response) => { console.log(response); }) //请求的接口是:http://api.douban.com/v2/movie/in_theaters 拼接起来的
注意:跨域
1.这个方法只是讲了proxyTable应该怎样配置。this
2.只能用在开发环境中,生产环境怎么用我还要再研究一下。prototype
3.千万别忘了在main.js中引入api做为全局变量。code