0
0

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.

【Nuxt bridge × axios】でデータを取得する

0
Last updated at Posted at 2023-03-14

環境

nuxt bridge
vue2.6
@nuxtjs/axios

概要

公式に書いているuseLazyAsyncDatauseLazyFetchがなぜか機能しなかったので、axiosを使ってデータを取得することにしました。

正解ではないかもしれませんので、ご参考程度に見てください。

実装方法

show.vue
<template>
    {{ user.id }}
</template>

<script setup>
const nuxtApp = useNuxtApp();
const user = ref({});

(async () => {
  await nuxtApp.$axios
    .$get('/api/users/' + userId)
    .then((response) => {
      user.value = response.data;
    })
    .catch((error) => {
      console.log(error)
    });
})();
</script>

ポイント・注意点としては、、、

composition-apiなのでrefでリアクティブなデータ定義としています。
(あえて、reactiveではなくrefを使う形にしています)

さらに以下のように{}として空のオブジェクトを定義しないと、user.idundefinedとなってしまいました。
const user = ref({});

もっといい書き方があれば、是非とも教えてください。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?