错误:use* 函数只能在 `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
}
我认为它没有本质上的错误,但它会弹出此错误:
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)
一旦我删除至少一个,它就会消失调用 loadArticles
或 loadServiceStatus
- 这会适得其反。
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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方案,它不是您应该对 urql 采取的默认方法 - 但如果您不想环绕反应式对象来访问从查询返回的单个属性,那么这是您想要的唯一方法。
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.