【发布时间】:2020-06-13 11:30:47
【问题描述】:
目前我以 axios post 请求为例:
在 boot/axios.js 中
import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
Vue.prototype.$axios = axios
Vue.prototype.$qs = qs
Vue.prototype.$serverUrl = 'http://localhost:8090/api/';
在我的页面中:
this.$axios.post(this.$serverUrl, null,
{ data: {version: "1", command: "login", email: this.regEmail, password: this.password, API: 2, token: "dummy"},
transformRequest: [(data, headers) => {return this.$qs.stringify(data);}]})
.then((response) => {...})
.catch((err) => {...})
.finally(() => {...})
这工作正常,但我想将其全球化,以便基本 url 和其他固定参数已经存在,我将只发送特定调用的附加参数:
this.$postCall(call specific params...)
.then((res) => {...})
.catch((err) => {...})
.finally(() => {...})
我知道我应该制作类似的原型
Vue.prototype.$postCall = function(param) {
}
但我不确定回调应该如何返回给调用者...
编辑: 我做了以下事情:
Vue.prototype.$postCall = function (params) {
return this.$axios.post('http://localhost:8090/api', null, { data: { API: 2, version: "1", params}});
}
并称它为:
this.$postCall({ command: "login", email: this.regEmail, password: this.password, token: "dummy" })
在调试中,我看到了正确的信息键:参数中的值,我希望将附加参数添加到固定参数中,但它们没有,我错过了什么?
编辑2: 通过一个简单的循环修复它
let theData = {
API: 2,
version: "1",
};
for (var k in params) {
theData[k] = params[k];
}
【问题讨论】: