gpt4 book ai didi

vue.js - axios-同时发起多个请求(vue.js)

转载 作者:搜寻专家 更新时间:2023-10-30 22:10:35 25 4
gpt4 key购买 nike

如何使用 axios 和 vue 并行发出多个请求?

最佳答案

Since axios can be used by React and Vue it is pretty much the same code.

请务必阅读 axios docs , 你可以从那里理解它。

无论如何,我将向您展示一个示例:

<template>
<div>
<button @click="make_requests_handler">Make multiple request</button>
{{message}} - {{first_request}} - {{second_request}}
</div>
</template>

和脚本:

import axios from 'axios'

export default {
data: () => ({
message: 'no message',
first_request: 'no request',
second_request: 'no request'
}),
methods: {
make_requests_handler() {
this.message = 'Requests in progress'

axios.all([
this.request_1(), //or direct the axios request
this.request_2()
])
.then(axios.spread((first_response, second_response) => {
this.message = 'Request finished'
this.first_request = 'The response of first request is' + first_response.data.message
this.second_request = 'The response of second request is' + second_response.data.message
}))
},
request_1() {
this.first_request: 'first request began'
return axios.get('you_URL_here')
},
request_2() {
this.second_request: 'second request began'
return axios.get('another_URL', { params: 'example' })
}
}
}

关于vue.js - axios-同时发起多个请求(vue.js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com