@frigear-nu/菜单
已命名, 可共享的导航菜单 。 A 类 菜单 (单位:千美元)main, (中文(简体) ), dashboard, (中文(简体) ), account,)是一个被命名的
由一个或多个填充的项目集 来源 - Nuxt 内容,其他模块(构建中)
时间,或运行时动态),或同时兼用(内容作为基础,模块附加). 这个
软件包是无演示文稿的:它只披露数据(useMenu) - (英语) description编号 :icon 可选
消费者可以随意制造或忽略.
快速设置
用一个命令将模块安装到您的 Nuxt 应用程序 :
npx nuxt module add @frigear-nu/menu
词汇
- 菜单 - 物品名称汇编(
main, (中文(简体) ),dashboard, (中文(简体) ),account(中文(简体) ). - 组 - 菜单的第一个点之后的部分.
dashboard.more地址组more菜单dashboard。 。 。 . - 来源 - 任何填满菜单的东西:,
extendMenu,或者模块的runtime/menus.ts文档.
从另一个模块提供项目
建造时已知的静态物品
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' },
])
},
})
动态项目,需要运行时上下文(例如:. 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' })
})
模块之间的登记顺序对这两种方法都无关紧要 -- -- 仅将项目合并 一旦每个模块完成设置.
Nuxt 内容来源
启用 @nuxt/content- 菜单的支持源( 需要) @nuxt/content 待安装 :
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' },
},
},
})
内容项目在 @nuxtjs/i18n 配置为 locales, (中文(简体) ),
照旧 config - > (中文) config_en 收藏后缀会议 @frigear-nu/base。 。 。 .
读取菜单
useMenu 是自动进口的。 它返回的形状与 useAsyncData
(单位:千美元){ data, pending, error, refresh }- 渲染完全取决于消费者:
<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>
项目按 order (递升;无 order 最后提出,登记时
命令) , (中文(简体) ).
通关卡读取
路径结束于 .* 读取每个匹配组, 平整成一个列表, 而不是单个
精确的组 - 用于同时制作整个菜单"树" :
// 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.*')
只有在读取菜单时才会支持通配符 - extendMenu编号 :extendMenuItem 拒绝一个
通配符路径, 因为项目只能注册成一个精确的组 .