当前位置:首页 > VUE

vue导航菜单实现

2026-01-19 21:23:01VUE

基础导航菜单实现

在Vue中实现基础导航菜单可以通过v-for指令动态渲染菜单项,结合router-link实现路由跳转。需要安装vue-router并配置路由信息。

<template>
  <nav>
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.title }}</router-link>
      </li>
    </ul>
  </nav>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        { path: '/', title: '首页' },
        { path: '/about', title: '关于' },
        { path: '/contact', title: '联系我们' }
      ]
    }
  }
}
</script>

<style scoped>
nav ul {
  list-style: none;
  display: flex;
  gap: 20px;
}
.router-link-active {
  font-weight: bold;
}
</style>

嵌套路由菜单

对于多级菜单,可以使用嵌套路由配置和递归组件。路由配置中添加children属性定义子路由。

// router.js
const routes = [
  {
    path: '/products',
    component: ProductsLayout,
    children: [
      { path: '', component: ProductList },
      { path: ':id', component: ProductDetail }
    ]
  }
]

递归组件实现:

<template>
  <ul>
    <li v-for="item in items" :key="item.path">
      <router-link :to="item.path">{{ item.title }}</router-link>
      <MenuTree v-if="item.children" :items="item.children"/>
    </li>
  </ul>
</template>

<script>
export default {
  name: 'MenuTree',
  props: ['items']
}
</script>

动态权限菜单

根据用户权限过滤显示菜单项,通常需要后端返回权限数据。在Vuex或Pinia中管理权限状态。

// store/modules/auth.js
state: {
  permissions: ['dashboard', 'user_management']
},
getters: {
  allowedRoutes: (state) => (routes) => {
    return routes.filter(route => state.permissions.includes(route.meta.permission))
  }
}

菜单组件中使用:

<template>
  <nav>
    <ul>
      <li v-for="route in allowedRoutes" :key="route.path">
        <router-link :to="route.path">{{ route.meta.title }}</router-link>
      </li>
    </ul>
  </nav>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  computed: {
    ...mapGetters(['allowedRoutes'])
  }
}
</script>

响应式侧边栏

结合CSS媒体查询和Vue的状态管理,实现可折叠的响应式侧边栏。

<template>
  <div :class="['sidebar', { 'collapsed': isCollapsed }]">
    <button @click="toggleCollapse">≡</button>
    <nav>
      <!-- 菜单内容 -->
    </nav>
  </div>
</template>

<style>
.sidebar {
  width: 250px;
  transition: width 0.3s;
}
.sidebar.collapsed {
  width: 60px;
}
@media (max-width: 768px) {
  .sidebar {
    width: 60px;
  }
}
</style>

面包屑导航

通过路由元信息和$route.matched数组生成面包屑路径。

<template>
  <div class="breadcrumbs">
    <span v-for="(match, index) in $route.matched" :key="index">
      <router-link v-if="index < $route.matched.length - 1" :to="match.path">
        {{ match.meta.breadcrumb }}
      </router-link>
      <span v-else>{{ match.meta.breadcrumb }}</span>
      <span v-if="index < $route.matched.length - 1"> / </span>
    </span>
  </div>
</template>

路由配置示例:

{
  path: '/user/:id',
  meta: { breadcrumb: '用户详情' }
}

vue导航菜单实现

标签: 菜单vue
分享给朋友:

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Toke…

vue 实现简单登陆

vue 实现简单登陆

以下是一个使用 Vue 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-containe…

vue实现画圆弧并着色

vue实现画圆弧并着色

在 Vue 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要跳…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-…