Axios在vuex操作中发布具有多个参数的请求 [英] Axios post request with multiple parameters in vuex action
本文介绍了Axios在vuex操作中发布具有多个参数的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在vuex动作中使用axios提取API数据:
Fetching API data with axios in vuex action:
actions: {
login ({commit}, payload) {
axios.post(globalConfig.TOKEN_URL, {
payload
})
.then((resp) => {
commit('auth_success', resp.data)
})
.catch((err) => {
console.log(err)
})
},
}
组件的数据发送方法:
methods: {
authChatClient () {
let payload = {
name: this.clientFio,
number: this.clientNumber
}
this.$store.dispatch('login', payload)
},
}
但是,它不起作用,因为有效负载是一个对象,包装在有效负载对象中.是否可以将多个参数从组件的方法发送到vuex动作?
However it won't work, since payload is an object, wrapped in a payload object. Is it possible to send multiple parameters from component's method to a vuex action?
发布请求如下所示:payload: {name: "aaa", number: "111"}
推荐答案
Vuex仅允许对动作使用1参数.但是,如果我正确理解了您的问题,则可以将多个参数(如果它们包装在一个对象中)发送给vuex动作.示例:
Vuex only allows the use of 1 parameter to an action. However, if I understand your question correctly, you can send multiple parameters to a vuex action if they are wrapped in an object. Example:
login({commit}, {name, number /*, ...more here*/}) {
axios.post(globalConfig.TOKEN_URL, {
name: name,
number: number,
/* more parameters here */
})
/* ... */
}
您可以通过以下方式调用它:
And you can call it with:
methods: {
authChatClient () {
let payload = {
name: this.clientFio,
number: this.clientNumber,
/* more parameters */
}
this.$store.dispatch('login', payload)
},
}
这篇关于Axios在vuex操作中发布具有多个参数的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文