useFetchComponentMeta

GitHub查看源码
useFetchComponentMeta 获取通过 nuxt-component-meta 注册的 Vue 组件元数据,包括 Props、Events 和 Slots 的完整类型定义,返回 useAsyncData 的响应式 ref,支持 SSR 服务端渲染和客户端导航时自动复用缓存,零重复请求。

概述

获取 Vue 组件的元数据,包括 Props、Emits、Slots 等信息。返回 useAsyncData 的 ref 对象,支持客户端缓存复用。

<script setup lang="ts">
const { data: meta } = useFetchComponentMeta('MyButton')

console.log(meta.value?.meta?.props)  // Props 定义
console.log(meta.value?.meta?.events) // Events 定义
console.log(meta.value?.meta?.slots)  // Slots 定义
</script>

显示 Props 文档

<template>
  <ul>
    <li v-for="prop in (meta?.meta?.props || [])" :key="prop.name">
      <code>{{ prop.name }}</code>: {{ prop.type }}
    </li>
  </ul>
</template>

<script setup lang="ts">
const { data: meta } = useFetchComponentMeta('MyButton')
</script>

API

useFetchComponentMeta(name)

获取组件元数据。

参数

name
string required
组件名称(PascalCase)。

返回值

返回 useAsyncData 的结果对象。

data
Ref<{ meta: ComponentMeta } | null>
组件元数据的响应式 ref,包含 propseventsslots 等信息。

相关

Changelog

No recent changes
Copyright © 2024 - 2026