---
title: "useFetchComponentExample"
description: "Read the source code, file path and name of example components under components/content/examples. ComponentExample uses it to show example code; call it directly to display example source in your own components. Available only with component docs enabled."
canonical_url: "https://docs.mhaibaraai.cn/en/docs/reference/composables/use-fetch-component-example"
---
# useFetchComponentExample

> Read the source code, file path and name of example components under components/content/examples. ComponentExample uses it to show example code; call it directly to display example source in your own components. Available only with component docs enabled.

`useFetchComponentExample()` requests `/api/component-example/{name}.json` and returns the result of `useAsyncData`. Example sources are collected from `components/content/examples` at build time, and the server looks them up by PascalCase name.

> [!WARNING]
> 
> Set 
> 
> movkNuxtDocs.componentDocs: true
> 
>  in 
> 
> nuxt.config
> 
> , see 
> 
> Feature Flags
> 
> . When disabled, the function is not auto-imported.

## Usage

```vue
<script setup lang="ts">
const { data: example } = await useFetchComponentExample('ButtonExample')
</script>

<template>
  <UCodeBlock v-if="example?.code" :code="example.code" language="vue" />
</template>
```

## Signature

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

interface ComponentExampleData {
  code: string
  filePath: string
  pascalName: string
}
```

## Parameters

| Name | Type | Description |
| --- | --- | --- |
| `name` | `string` | Example component name; the server converts it to PascalCase, so `button-example`, `buttonExample` and `ButtonExample` are equivalent |

## Return Value

| Name | Type | Description |
| --- | --- | --- |
| `data` | `Ref<Partial<ComponentExampleData> \| undefined>` | `code` is the example source, `filePath` the source file path, `pascalName` the component name; an empty object when no example is found |
| `status` | `Ref<AsyncDataRequestStatus>` | Request status |
| `refresh` | `() => Promise<void>` | Request again |

On the client the call is lazy; guard against empty data in templates.

## Related

- [ComponentExample](https://docs.mhaibaraai.cn/en/docs/components/component-example)

## Changelog

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

---

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


## Sitemap

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