useLazyAsyncData
这个围绕 useAsyncData 的包装器会立即触发导航。
描述
默认情况下,useAsyncData
会阻塞导航,直到其异步处理程序解析完成。useLazyAsyncData
提供了一个围绕 useAsyncData
的包装器,通过将 lazy
选项设置为 true
,在处理程序解析完成之前触发导航。
useLazyAsyncData
具有与 useAsyncData
相同的签名。示例
pages/index.vue
<script setup lang="ts">
/* Navigation will occur before fetching is complete.
Handle 'pending' and 'error' states directly within your component's template
*/
const { status, data: count } = await useLazyAsyncData('count', () => $fetch('/api/count'))
watch(count, (newCount) => {
// Because count might start out null, you won't have access
// to its contents immediately, but you can watch it.
})
</script>
<template>
<div>
{{ status === 'pending' ? 'Loading' : count }}
</div>
</template>
useLazyAsyncData
是编译器转换的保留函数名称,因此您不应将自己的函数命名为 useLazyAsyncData
。