@frigear-nu/menu

中文文档
0.4.0发布于 2026/11/13MITTypeScriptES Module构建来源已验证近 30 天下载 3398

Named, shareable navigation menus - modules contribute items (extendMenu / a runtime/menus.ts source) and/or Nuxt Content, consumers read them via useMenu and render them however they like.

原始使用文档
文档目录8 节
  1. Quick Setup
  2. Vocabulary
  3. Contributing items from another module
  4. Static items, known at build time
  5. Dynamic items, needing runtime context (e.g. useRuntimeConf…
  6. Nuxt Content source
  7. Reading a menu
  8. Wildcard reads

@frigear-nu/menu

Named, shareable navigation menus. A menu (main, dashboard, account, ...) is a named collection of items, filled by one or more sources - Nuxt Content, other modules (at build time, or dynamically at runtime), or both at once (content as the base, modules appending). This package is presentation-free: it only exposes data (useMenu) - description/icon are optional fields a consumer may render however it likes, or ignore.

Quick Setup

Install the module to your Nuxt application with one command:

npx nuxt module add @frigear-nu/menu

Vocabulary

  • Menu - a named collection of items (main, dashboard, account, ...).
  • Group - the part after a menu's first dot. dashboard.more addresses group more of menu dashboard.
  • Source - anything that fills a menu: Nuxt Content, extendMenu, or a module's runtime/menus.ts file.

Contributing items from another module

Static items, known at build time

import { createResolver, defineNuxtModule } from '@nuxt/kit'
import { extendMenu } from '@frigear-nu/menu/utils'

export default defineNuxtModule({
  meta: { name: '@frigear-nu/example' },
  setup(options, nuxt) {
    // `'dashboard.more'` is parsed as menu `dashboard`, group `more` (split on the first dot only).
    extendMenu(nuxt, 'dashboard.more', [
      { label: 'Billing', description: 'Manage your subscription', to: '/billing' },
    ])
  },
})

Dynamic items, needing runtime context (e.g. useRuntimeConfig)

import { createResolver, defineNuxtModule } from '@nuxt/kit'
import { registerMenuSource } from '@frigear-nu/menu/utils'

export default defineNuxtModule({
  meta: { name: '@frigear-nu/example' },
  setup(options, nuxt) {
    const resolver = createResolver(import.meta.url)

    registerMenuSource(nuxt, {
      file: resolver.resolve('./runtime/menus'),
      menu: 'dashboard',
    })
  },
})
// runtime/menus.ts
import { defineMenuSource, extendMenuItem } from '@frigear-nu/menu/utils'

export default defineMenuSource((items) => {
  const config = useRuntimeConfig().public.example

  // Pushed into the `dashboard` menu's root group.
  if (config?.enabled) items.push({ label: 'Example', to: '/example' })

  // Pushed into a named group (in this or another menu) instead - created automatically if it
  // doesn't already exist. `extendMenuItem` pushes into shared app state, so it only works from
  // code that actually runs as part of the Nuxt app (like this file) - not from a module's own
  // build-time `setup()` (use `extendMenu` there instead).
  extendMenuItem('dashboard.more', { label: 'Security', to: '/security' })
})

Registration order between modules doesn't matter for either approach - items are only merged once every module has finished setting up.

Nuxt Content source

Enable the @nuxt/content-backed source for a menu (requires @nuxt/content to be installed):

export default defineNuxtConfig({
  menus: {
    content: {
      // Reads `config.navigation` (the collection/field defined by `@frigear-nu/base`).
      main: true,
      // ...or a custom collection/field:
      dashboard: { collection: 'dashboard', field: 'tiles' },
    },
  },
})

Content items are localized automatically when @nuxtjs/i18n is configured with locales, following the same config -> config_en collection-suffix convention as @frigear-nu/base.

Reading a menu

useMenu is auto-imported. It returns the same shape as useAsyncData ({ data, pending, error, refresh }) - rendering is entirely up to the consumer:

<script setup lang="ts">
const { data: items } = useMenu('main')
const { data: more } = useMenu('dashboard.more')
</script>

<template>
  <nav>
    <NuxtLink
      v-for="item in items"
      :key="item.id"
      :to="item.to"
    >
      {{ item.label }}
    </NuxtLink>
  </nav>
</template>

Items are sorted by order (ascending; items without an order render last, in registration order).

Wildcard reads

A path ending in .* reads every matching group, flattened into one list, instead of a single exact group - useful for rendering a whole menu "tree" at once:

// Every group of the `dashboard` menu (root group included).
const { data: all } = useMenu('dashboard.*')

// `account` and every group nested under it (e.g. `dashboard.account.sub`), but not sibling
// groups like `dashboard.more`.
const { data: accountTree } = useMenu('dashboard.account.*')

Wildcards are only supported when reading a menu - extendMenu/extendMenuItem reject a wildcard path, since items can only ever be registered into one exact group.

版本与发布记录

@frigear-nu/menu 共发布 23 个版本,首次发布于 2026/10/09;过去 12 个月发布了 23 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.0latest2026/11/133 天前
0.3.02026/11/123 天前
0.2.52026/11/123 天前
0.2.42026/11/0411 天前
0.2.32026/11/0411 天前
0.2.22026/11/0312 天前

关于 @frigear-nu/menu 的常见问题

@frigear-nu/menu 是什么?

Named, shareable navigation menus - modules contribute items (extendMenu / a runtime/menus.ts source) and/or Nuxt Content, consumers read them via useMenu and render them however they like.

如何安装 @frigear-nu/menu?

在项目目录运行 npm install @frigear-nu/menu,或使用 pnpm add / yarn add / bun add。

@frigear-nu/menu 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frigear-nu/menu 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@frigear-nu/menu 可以商用吗?

@frigear-nu/menu 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@frigear-nu/menu 的下载量有多少?

根据 npm 官方下载统计,@frigear-nu/menu 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,398 次,日均约 113 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frigear-nu/menu 还在维护吗?

最新版本 0.4.0 发布于 2026/11/13(3 天前);至今共发布 23 个版本,其中过去 12 个月发布了 23 个;首次发布于 2026/10/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。