错误:use* 函数只能在 `setup()` 或其他生命周期挂钩期间调用

发布于 2025-01-15 13:48:25 字数 1122 浏览 0 评论 0原文

我在

let isServiceAlive = true;
let articles: IArticle[] = ref(null);

onMounted(async () => 
{
    await loadServiceStatus()

    if (isServiceAlive == false)
        return

    await loadArticles()
})

async function loadServiceStatus()
{
    const { data } = await useQuery(
    { 
        query: GetIsServiceAlive 
    })

    isServiceAlive = data.value.alive
}

async function loadArticles()
{
    const { data } = await useQuery(
    {
        query: GetArticlesQuery
    })

    articles.value = data.value.articles
}

我认为它没有本质上的错误,但它会弹出此错误:

Uncaught (in promise) Error: use* functions may only be called during the `setup()` or other lifecycle hooks.
    at useClient (useClient.ts?f3e5:28:1)
    at callUseQuery (useQuery.ts?cc4d:72:1)
    at useQuery (useQuery.ts?cc4d:64:1)
    at loadPosts (Index.vue?a170:39:1)
    at eval (Index.vue?a170:24:1)

一旦我删除至少一个,它就会消失调用 loadArticlesloadServiceStatus - 这会适得其反。

I have this simple code inside <script lang='ts' setup>:

let isServiceAlive = true;
let articles: IArticle[] = ref(null);

onMounted(async () => 
{
    await loadServiceStatus()

    if (isServiceAlive == false)
        return

    await loadArticles()
})

async function loadServiceStatus()
{
    const { data } = await useQuery(
    { 
        query: GetIsServiceAlive 
    })

    isServiceAlive = data.value.alive
}

async function loadArticles()
{
    const { data } = await useQuery(
    {
        query: GetArticlesQuery
    })

    articles.value = data.value.articles
}

I see nothing inherently wrong in it, but it pops this error:

Uncaught (in promise) Error: use* functions may only be called during the `setup()` or other lifecycle hooks.
    at useClient (useClient.ts?f3e5:28:1)
    at callUseQuery (useQuery.ts?cc4d:72:1)
    at useQuery (useQuery.ts?cc4d:64:1)
    at loadPosts (Index.vue?a170:39:1)
    at eval (Index.vue?a170:24:1)

It disappears as soon as I remove at least one of the calls loadArticles or loadServiceStatus - which is counterproductive.

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

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

发布评论

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

评论(1

蛮可爱 2025-01-22 13:48:25

我找到了一个解决方案,它不是您应该对 urql 采取的默认方法 - 但如果您不想环绕反应式对象来访问从查询返回的单个属性,那么这是您想要的唯一方法。

<script lang='ts' setup>
import { IArticle } from '@/services'
import { ref } from 'vue'
import { GetIsServiceAliveQuery } from './queries/GetIsServiceAliveQuery'
import { GetArticlesQuery } from './queries/GetArticlesQuery'
import { useQuery } from '@urql/vue'
import Article from './Article.vue'

let articles = ref(null)
let serviceAvailable = ref(false);

useQuery(
{
    query: GetIsServiceAliveQuery
})
.then(result => serviceAvailable.value = result.data.value.alive)

useQuery(
{
    query: GetArticlesQuery
})
.then(result => articles.value = result.data.value.articles)
</script>

I found a solution, it's not the default approach you should take with urql - but it's the only approach you want, if you don't want to wrap around reactive objects to access a single property that's returned from the query.

<script lang='ts' setup>
import { IArticle } from '@/services'
import { ref } from 'vue'
import { GetIsServiceAliveQuery } from './queries/GetIsServiceAliveQuery'
import { GetArticlesQuery } from './queries/GetArticlesQuery'
import { useQuery } from '@urql/vue'
import Article from './Article.vue'

let articles = ref(null)
let serviceAvailable = ref(false);

useQuery(
{
    query: GetIsServiceAliveQuery
})
.then(result => serviceAvailable.value = result.data.value.alive)

useQuery(
{
    query: GetArticlesQuery
})
.then(result => articles.value = result.data.value.articles)
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文