Как отправить несколько запросов параллельно в fetch hook — Nuxt

Давайте объявим метод для получения данных с нескольких конечных точек.

methods:{
// rest of your methods 
async fetchData(){
     let [res1, res2, res3] = await Promise.all(
         this.$axios.$get('api/endpoint/1'),
         this.$axios.$get('api/endpoint/2'),
         this.$axios.$get('api/endpoint/3'),
         )
     // check your response and whatever you want with those response
     console.log(res1)
     console.log(res2)
     console.log(res3)
}
Вход в полноэкранный режим Выход из полноэкранного режима

Затем вы можете вызвать этот метод в fetch hook —

async fetch(){
      await this.fetchData()
}
Войти в полноэкранный режим Выйти из полноэкранного режима

Спасибо.

😎 Читать дальше —

  • https://dev.to/siumhossain/how-to-implement-an-infinite-scroll-with-vue-and-nuxt-4d6k
  • https://dev.to/siumhossain/generating-sitemap-xml-for-static-and-dynamic-from-multiple-api-endpoint-in-nuxt-js-2cij
  • https://dev.to/siumhossain/vue-nuxt-remove-html-tags-from-rendered-text-for-seo-5e47
  • https://dev.to/siumhossain/set-up-deploy-nuxtjs-web-application-on-ubuntu-2204-lts-2gm0
  • https://dev.to/siumhossain/shortnote-redirect-and-catch-errors-in-fetch-hook-nuxt-ssr-33k2
  • https://dev.to/siumhossain/how-to-detect-scroll-up-and-scroll-down-in-nuxtvue-4a3m
  • https://dev.to/siumhossain/error-system-limit-for-number-of-file-watchers-reached-in-nuxt-5g2d

Оцените статью
devanswers.ru
Добавить комментарий