为什么Uselazyfetch综合返回null数据?

发布于 2025-01-30 15:43:48 字数 2344 浏览 0 评论 0 原文

我正在尝试学习如何在NUXT3中使用数据获取组合。 由于某种原因,我得到了一个“加载”文本,以代替用户名应为:

“在此处输入映像”

我的预期输出应该是:

stackblitz演示:

这是我的可综合代码: USEUSER

export default function (id) {
  const baseURL = 'https://jsonplaceholder.typicode.com';
  const { pending: pendingUsers, data: users } = useLazyFetch(
    `${baseURL}/users`
  );
  const { pending: pendingUser, data: user } = useLazyFetch(
    `${baseURL}/users/${id}`
  );

  return {
    pendingUsers,
    users,
    pendingUser,
    user,
  };
}

以及我想使用的组件在这里: pcardcard.vue

<template>
  <article class="rounded border overflow-hidden">
    ...snip...
      <footer class="mt-6 flex items-center">
        <PostAvatar :post="post" />
        <div class="ml-3">
          <p class="text-sm font-medium text-gray-900">
            <a href="#" class="hover:underline">
              {{ pendingUser ? 'Loading...' : user.name }} /////<---- HERE!
            </a>
          </p>
          <div class="flex space-x-1 text-sm text-gray-500">
            <!-- <time :datetime="post.datetime">
              {{ post.date }}
            </time> -->
            mar 4, 2019
            <span aria-hidden="true">&middot;</span>
            <span>misc</span>
          </div>
        </div>
      </footer>
    ...snip...
  </article>
</template>

<script setup>
const props = defineProps({
  post: {
    type: Object,
    default: () => ({}),
  },
});
const { pendingUser, user } = useUser(props.post.userId);
</script>

有人知道为什么用户名不加载吗?如果i console.log(pendinguser.value,user.value)我获得 true null

I am trying to learn how to use the data fetching composables in Nuxt3. For some reason, I get a "loading" text in place of where the username should be:

enter image description here

My expected output is supposed to be something like:

enter image description here

Stackblitz demo: https://stackblitz.com/edit/nuxt-starter-grcyfr?file=components/PostCard.vue

Here's my composable code:
useUser:

export default function (id) {
  const baseURL = 'https://jsonplaceholder.typicode.com';
  const { pending: pendingUsers, data: users } = useLazyFetch(
    `${baseURL}/users`
  );
  const { pending: pendingUser, data: user } = useLazyFetch(
    `${baseURL}/users/${id}`
  );

  return {
    pendingUsers,
    users,
    pendingUser,
    user,
  };
}

And the component where I want to use it is here:
PostCard.vue:

<template>
  <article class="rounded border overflow-hidden">
    ...snip...
      <footer class="mt-6 flex items-center">
        <PostAvatar :post="post" />
        <div class="ml-3">
          <p class="text-sm font-medium text-gray-900">
            <a href="#" class="hover:underline">
              {{ pendingUser ? 'Loading...' : user.name }} /////<---- HERE!
            </a>
          </p>
          <div class="flex space-x-1 text-sm text-gray-500">
            <!-- <time :datetime="post.datetime">
              {{ post.date }}
            </time> -->
            mar 4, 2019
            <span aria-hidden="true">·</span>
            <span>misc</span>
          </div>
        </div>
      </footer>
    ...snip...
  </article>
</template>

<script setup>
const props = defineProps({
  post: {
    type: Object,
    default: () => ({}),
  },
});
const { pendingUser, user } = useUser(props.post.userId);
</script>

Anyone know why the username is not loading? If I console.log(pendingUser.value, user.value) I get true null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年少掌心 2025-02-06 15:43:48

更新

不确定是否是实际的当前问题,但将 nuxt 降级到 3.0.0.0-rc.1 修复了问题。


给定此组合

/composables/useuser.js

export default (id) => {
  const baseURL = 'https://jsonplaceholder.typicode.com';

  const { pending: pendingUser, data: user } = useLazyFetch(
    `${baseURL}/users/${id}`
  );
  return {
    pendingUser,
    user,
  };
}

,此视图

/pages/index.vue

<script setup>
const { pendingUser, user } = useUser(2);

watch(user, (newUser) => {
  console.log('newUser', newUser);
  user.value = newUser;
})
</script>

<template>
  <div>
    <div>pending? {{ pendingUser }}</div>
    <pre>{{ pendingUser ? 'loading' : user }}</pre>
  </div>
</template>

您应该有一些工作。

手表(不确定)(不确定)(不确定) “ nofollow noreferrer”>文档因为 uselazyfetch 是异步和非块。

Update

Not sure if it's an actual current issue but downgrading nuxt to 3.0.0-rc.1 fixed the issue.


Given this composable

/composables/useUser.js

export default (id) => {
  const baseURL = 'https://jsonplaceholder.typicode.com';

  const { pending: pendingUser, data: user } = useLazyFetch(
    `${baseURL}/users/${id}`
  );
  return {
    pendingUser,
    user,
  };
}

And this view

/pages/index.vue

<script setup>
const { pendingUser, user } = useUser(2);

watch(user, (newUser) => {
  console.log('newUser', newUser);
  user.value = newUser;
})
</script>

<template>
  <div>
    <div>pending? {{ pendingUser }}</div>
    <pre>{{ pendingUser ? 'loading' : user }}</pre>
  </div>
</template>

You should have something working.

The usage of watch may be needed (not sure) as explained in the documentation because useLazyFetch is async and non-blocking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文