---
title: "useFetchComponentMeta"
description: "读取 nuxt-component-meta 生成的组件元数据，包括 Props、Slots、Events 的类型与说明。ComponentProps 等组件文档组件都基于它实现，需要自行渲染组件 API 表格时直接调用。仅在开启组件文档功能时可用。"
canonical_url: "https://docs.mhaibaraai.cn/docs/reference/composables/use-fetch-component-meta"
---
# useFetchComponentMeta

> 读取 nuxt-component-meta 生成的组件元数据，包括 Props、Slots、Events 的类型与说明。ComponentProps 等组件文档组件都基于它实现，需要自行渲染组件 API 表格时直接调用。仅在开启组件文档功能时可用。

`useFetchComponentMeta()` 请求 `/api/component-meta/{name}.json`，返回 `useAsyncData` 的结果。服务端渲染时会把该接口加入预渲染列表，客户端导航复用 payload，不会重复请求。

> [!WARNING]
> 
> 需要在 
> 
> nuxt.config
> 
>  中设置 
> 
> movkNuxtDocs.componentDocs: true
> 
>  并安装 
> 
> nuxt-component-meta
> 
> 、
> 
> vue-component-meta
> 
> 、
> 
> prettier
> 
> ，见 
> 
> 功能开关
> 
> 。关闭时该函数不会被自动导入。

## 用法

```vue
<script setup lang="ts">
const { data } = await useFetchComponentMeta('MyButton')
</script>

<template>
  <ul>
    <li v-for="prop in data?.meta?.props" :key="prop.name">
      <code>{{ prop.name }}</code>: {{ prop.type }}
    </li>
  </ul>
</template>
```

## 签名

```ts
function useFetchComponentMeta(name: string): AsyncData<Partial<ComponentMetaData> | undefined, NuxtError | undefined>

interface ComponentMetaData {
  meta: ComponentMeta // 来自 vue-component-meta
}
```

## 参数

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `name` | `string` | 组件的 PascalCase 名称，与 `nuxt-component-meta` 注册的名称一致 |

## 返回值

返回 `useAsyncData` 的结果，常用字段：

| 名称 | 类型 | 说明 |
| --- | --- | --- |
| `data` | `Ref<Partial<ComponentMetaData> \| undefined>` | `data.meta` 含 `props`、`slots`、`events`、`exposed` 等；组件不存在或请求失败时为空对象 |
| `status` | `Ref<AsyncDataRequestStatus>` | 请求状态 |
| `refresh` | `() => Promise<void>` | 重新请求 |

## 注意事项

- 客户端调用时为懒加载（`lazy`），首次渲染 `data` 可能为空，模板中要做空值判断。
- 默认只扫描 layer 自带的文档组件，自己的组件需要加入 `componentMeta.include`，见 [ComponentProps](https://docs.mhaibaraai.cn/docs/components/component-props)。

## 相关

- [ComponentProps](https://docs.mhaibaraai.cn/docs/components/component-props)
- [ComponentSlots](https://docs.mhaibaraai.cn/docs/components/component-slots)
- [ComponentEmits](https://docs.mhaibaraai.cn/docs/components/component-emits)

## 更新日志

See commit history for [layer/modules/component-docs/runtime/composables/useFetchComponentMeta.ts](https://github.com/mhaibaraai/movk-nuxt-docs/commits/main/layer/modules/component-docs/runtime/composables/useFetchComponentMeta.ts).

---

- [GitHub](https://github.com/mhaibaraai/movk-nuxt-docs/blob/main/layer/modules/component-docs/runtime/composables/useFetchComponentMeta.ts)


## Sitemap

See the full [sitemap](https://docs.mhaibaraai.cn/sitemap.md) for all pages.
