当前位置:首页 > 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>

路由配置示例:

vue导航菜单实现

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

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

相关文章

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <templat…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现键盘

vue实现键盘

Vue 实现键盘功能 在Vue中实现键盘功能通常涉及监听键盘事件、处理输入以及渲染虚拟键盘界面。以下是几种常见的实现方式: 监听键盘事件 通过@keydown或@keyup指令监听键盘事件,适用于需…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…