useFetchComponentExample
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.
movkNuxtDocs.componentDocs: true in nuxt.config, see Feature Flags. When disabled, the function is not auto-imported.Usage
<script setup lang="ts">
const { data: example } = await useFetchComponentExample('ButtonExample')
</script>
<template>
<UCodeBlock v-if="example?.code" :code="example.code" language="vue" />
</template>
Signature
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
Changelog
useFetchComponentMeta
Read the component metadata generated by nuxt-component-meta, including the types and descriptions of props, slots and events. ComponentProps and the other component docs components are built on it; call it directly to render your own API tables. Available only with component docs enabled.
useDocsNavigation
Read the docs navigation tree that the shell has already loaded. The header section bar, sidebar and docs pages all take their navigation from here, so custom components reuse the same data without querying the collection again.