useFetchComponentExample() 请求 /api/component-example/{name}.json,返回 useAsyncData 的结果。示例源码在构建期从 components/content/examples 收集,服务端按 PascalCase 名称查找。
用法
<script setup lang="ts">
const { data: example } = await useFetchComponentExample('ButtonExample')
</script>
<template>
<UCodeBlock v-if="example?.code" :code="example.code" language="vue" />
</template>
签名
function useFetchComponentExample(name: string): AsyncData<Partial<ComponentExampleData> | undefined, NuxtError | undefined>
interface ComponentExampleData {
code: string
filePath: string
pascalName: string
}
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | string | 示例组件名,服务端会转为 PascalCase 后查找,button-example、buttonExample、ButtonExample 等价 |
返回值
| 名称 | 类型 | 说明 |
|---|---|---|
data | Ref<Partial<ComponentExampleData> | undefined> | code 为示例源码,filePath 为源文件路径,pascalName 为组件名;找不到示例时为空对象 |
status | Ref<AsyncDataRequestStatus> | 请求状态 |
refresh | () => Promise<void> | 重新请求 |
客户端调用时为懒加载,模板中要做空值判断。
相关
更新日志
No recent changes