在 index.js 中可以配置后台的地址;代理的方式;
這個文件在 config 中
proxyTable: { // 連接后台 '/api':{ target:"http://new.windhouse.com", //后台地址 changeOrigin:true, //跨域代理 pathRewrite: { '^/api':"" //路徑重寫 },
然后 安裝 axios 依賴
npm install axios
我是在main.js 里面定義了axios
//main.js import axios from 'axios' Vue.prototype.$http = axios //在其他組件中使用 this.http.post()
之后就使用 axios的方式對接上后台的api就可以了
this.$http.post('/api', qs.stringify({ 'phone':this.fromlist.phone, 'password':this.fromlist.password }) ) .then(this.PostFrommsg) .catch((err)=>{console.log(err)}) //這里只是我習慣把成功函數寫在別的函數里面 PostFrommsg (res){ console.log(res) }
上面使用到的 “ qs ” ,是使用了另外一個依賴
因為 ajax 跟 axios 的傳值方式有些不一樣所以需要轉換一下
安裝調用一下就可以了
npm install qs
//然后在項目里面使用就可以了
import qs from 'qs'
qs.stringify({ XXX })