当前位置:首页 > VUE

vue实现菜单导航

2026-02-18 14:05:11VUE

Vue 实现菜单导航的方法

使用 Vue Router 实现基础导航

安装 Vue Router 依赖:

npm install vue-router

main.jsrouter/index.js 中配置路由:

import { createRouter, createWebHistory } from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

在 App.vue 中使用 <router-view><router-link>

<template>
  <div>
    <nav>
      <router-link to="/">Home</router-link>
      <router-link to="/about">About</router-link>
    </nav>
    <router-view></router-view>
  </div>
</template>

动态生成菜单项

创建可配置的菜单数据:

// menuItems.js
export default [
  { path: '/', name: 'Home' },
  { path: '/about', name: 'About' },
  { path: '/contact', name: 'Contact' }
]

在组件中动态渲染:

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

<script>
import menuItems from './menuItems'
export default {
  data() {
    return {
      menuItems
    }
  }
}
</script>

嵌套路由实现多级菜单

配置嵌套路由:

const routes = [
  {
    path: '/products',
    component: Products,
    children: [
      { path: 'list', component: ProductList },
      { path: 'detail/:id', component: ProductDetail }
    ]
  }
]

渲染嵌套菜单:

<template>
  <div v-for="category in menuCategories" :key="category.path">
    <router-link :to="category.path">{{ category.name }}</router-link>
    <div v-if="category.children">
      <router-link 
        v-for="child in category.children" 
        :key="child.path"
        :to="child.path"
      >
        {{ child.name }}
      </router-link>
    </div>
  </div>
</template>

添加菜单交互效果

使用 CSS 实现悬停效果:

nav a {
  padding: 8px 16px;
  transition: background-color 0.3s;
}

nav a:hover {
  background-color: #f0f0f0;
}

.router-link-active {
  font-weight: bold;
  border-bottom: 2px solid #42b983;
}

添加折叠功能:

<template>
  <div v-for="item in menuItems" :key="item.path">
    <div @click="toggleSubmenu(item)">
      {{ item.name }}
      <span v-if="item.children">{{ isOpen(item) ? '▲' : '▼' }}</span>
    </div>
    <div v-show="isOpen(item)" v-if="item.children">
      <router-link 
        v-for="child in item.children" 
        :key="child.path"
        :to="child.path"
      >
        {{ child.name }}
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      openItems: []
    }
  },
  methods: {
    toggleSubmenu(item) {
      const index = this.openItems.indexOf(item.path)
      if (index > -1) {
        this.openItems.splice(index, 1)
      } else {
        this.openItems.push(item.path)
      }
    },
    isOpen(item) {
      return this.openItems.includes(item.path)
    }
  }
}
</script>

响应式菜单实现

使用媒体查询和 Vue 状态管理:

<template>
  <button @click="toggleMobileMenu">☰</button>
  <nav :class="{ 'mobile-hidden': !showMobileMenu }">
    <!-- 菜单内容 -->
  </nav>
</template>

<script>
export default {
  data() {
    return {
      showMobileMenu: false,
      isMobile: window.innerWidth < 768
    }
  },
  mounted() {
    window.addEventListener('resize', this.handleResize)
  },
  beforeUnmount() {
    window.removeEventListener('resize', this.handleResize)
  },
  methods: {
    toggleMobileMenu() {
      this.showMobileMenu = !this.showMobileMenu
    },
    handleResize() {
      this.isMobile = window.innerWidth < 768
      if (!this.isMobile) {
        this.showMobileMenu = false
      }
    }
  }
}
</script>

<style>
@media (max-width: 768px) {
  nav {
    display: none;
  }
  .mobile-hidden {
    display: none;
  }
}
</style>

权限控制菜单显示

根据用户角色过滤菜单项:

// 在路由配置中添加 meta 字段
const routes = [
  { 
    path: '/admin', 
    component: Admin,
    meta: { requiresAuth: true, roles: ['admin'] }
  }
]

使用导航守卫和菜单过滤:

// router/index.js
router.beforeEach((to, from, next) => {
  const userRole = getUserRole() // 获取用户角色
  if (to.meta.roles && !to.meta.roles.includes(userRole)) {
    next('/forbidden')
  } else {
    next()
  }
})

在组件中过滤菜单:

vue实现菜单导航

<script>
import menuItems from './menuItems'
export default {
  computed: {
    filteredMenuItems() {
      const userRole = this.$store.state.user.role
      return menuItems.filter(item => {
        return !item.meta?.roles || item.meta.roles.includes(userRole)
      })
    }
  }
}
</script>

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

相关文章

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现购票

vue实现购票

Vue 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue动画实现

vue动画实现

Vue动画实现方法 Vue提供了多种方式实现动画效果,主要通过内置组件和第三方库完成。 使用<transition>组件 Vue内置的<transition>组件可以为元素添…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue实现表单

vue实现表单

Vue 表单实现方法 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定。适用于 input、textarea、select 等元素。 <template> <…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…