將請求接口定義定義成全局變量,掛載到vue實例上可以全局使用,
隨便寫一個文件放請求接口,並導出。
在 main.js 文件中導入,並掛載。這樣就可以在項目裏使用。
使用案例
this.GLOBAL.myhf 調用的接口
login:地址
參數
username:,
password:
axios .post( this.GLOBAL.myhf + "/login", qs.stringify({
username: that.formData.user.username,
password: that.formData.user.password
}), { withCredentials: true } ) .then(function(response) {
輸出數據
console.log(response)
})
多參數拼接
前臺拿到的數據
this.info.pageNumber
後臺參數
pageNum
pageNum
axios.get( this.GLOBAL.myhf +"/role/search?pageNum=" + this.info.pageNumber + "&pageNum=" + this.info.pageSize,
{ withCredentials: true } ).then(function(response) {
輸出數據
})
es6模板字符串請求接口
模板字符,請求數據不用拼接數據
form(id){
this.$axios.get(this.GLOBAL.myhf +`/caregis/search?pageNum=${this.pageNum}&pageSize=${this.pageSize}&recordId${id}`,
{withCredentials: true}).then(res=>{
if(res.data.info == "success"){
this.regis = res.data.data.list
}
})
}
}