16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vue.js内で使うaxiosで複数APIリクエストをまとめて行う方法

Posted at

vue.js内でaxiosを使う場合複数のAPIリクエストをしたい場合があります。その時にきれいにやる方法です。

index.vue
<template>
省略
</template>

<script>
import axios from 'axios'

function getData () {
  return Promise.all([
    axios.get(process.env.API_URL + '/posts'),
    axios.get(process.env.API_URL + '/services'),
    axios.get(process.env.API_URL + '/shops'),
  ]).then(([posts, services, shops]) => {
    const data = {}
    data.posts = posts.data
    data.services = services.data
    data.shops = shops.data
    return Promise.resolve(data)
  })
}

export default {
 async asyncData () {
    const data = await getData()
    return {
      posts: data.posts,
      shops: data.shops,
      services: data.services
    }
  }
}

</script>

<style>
省略
</style>
16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?