Axios-Make multiple request at once (vue.js)

后端 未结 2 1756
攒了一身酷
攒了一身酷 2021-02-06 05:02

How to make multiple requests in parallel using axios and vue ?

2条回答
  •  既然无缘
    2021-02-06 05:32

    You can pass your asynchronous calls to Promise.all. As long as each of them returns a promise they will execute at the same time. I'm using store.dispatch but you could equally use axios calls or fetch.

    In this example i'm making the calls when the vue component gets created:

    ...
    async created() {
        const templates = this.$store.dispatch(TEMPLATES_LOAD);
        const userTemplates = this.$store.dispatch(USER_TEMPLATES_LOAD);
        const players = this.$store.dispatch(OTHER_PLAYERS_LOAD);
        return await Promise.all([templates, userTemplates, players])
            .then(() => {
                console.log('Loaded data for form elements');
            });
    }
    

提交回复
热议问题