当前位置:首页 > VUE

vue实现导航菜单

2026-02-11 05:36:13VUE

Vue 实现导航菜单的方法

使用 Vue Router 实现基础导航

安装 Vue Router 依赖:

npm install vue-router

在项目中配置路由:

// router/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 中添加导航菜单:

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

实现动态导航菜单

创建可配置的导航菜单数据:

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

在组件中使用动态菜单:

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

<script>
import navItems from '@/data/navItems'
export default {
  data() {
    return {
      navItems
    }
  }
}
</script>

添加菜单激活样式

使用 Vue Router 的 active-class:

vue实现导航菜单

<router-link 
  to="/about" 
  active-class="active"
>
  About
</router-link>

或者使用 exact-active-class 精确匹配:

<router-link 
  to="/" 
  exact-active-class="exact-active"
>
  Home
</router-link>

实现嵌套导航菜单

配置嵌套路由:

// router/index.js
{
  path: '/products',
  component: Products,
  children: [
    { path: '', component: ProductList },
    { path: ':id', component: ProductDetail }
  ]
}

创建嵌套导航组件:

vue实现导航菜单

<template>
  <div>
    <nav>
      <router-link to="/products">All Products</router-link>
      <router-link 
        v-for="product in products"
        :key="product.id"
        :to="`/products/${product.id}`"
      >
        {{ product.name }}
      </router-link>
    </nav>
    <router-view />
  </div>
</template>

添加下拉菜单功能

实现基础下拉菜单:

<template>
  <div class="dropdown">
    <button @click="toggleDropdown">Menu</button>
    <ul v-show="isOpen">
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isOpen: false,
      menuItems: [
        { name: 'Home', path: '/' },
        { name: 'About', path: '/about' }
      ]
    }
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen
    }
  }
}
</script>

<style>
.dropdown {
  position: relative;
  display: inline-block;
}
.dropdown ul {
  position: absolute;
  background: white;
  border: 1px solid #ddd;
}
</style>

实现面包屑导航

创建面包屑组件:

<template>
  <div class="breadcrumbs">
    <router-link 
      v-for="(crumb, index) in crumbs" 
      :key="index"
      :to="crumb.path"
    >
      {{ crumb.name }}
    </router-link>
  </div>
</template>

<script>
export default {
  computed: {
    crumbs() {
      const pathArray = this.$route.path.split('/')
      return pathArray.map((path, i) => {
        return {
          name: path || 'home',
          path: pathArray.slice(0, i + 1).join('/') || '/'
        }
      })
    }
  }
}
</script>

添加响应式导航菜单

实现移动端响应式菜单:

<template>
  <nav>
    <button @click="toggleMenu">☰</button>
    <ul :class="{ 'show': menuOpen }">
      <li v-for="item in navItems" :key="item.path">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </li>
    </ul>
  </nav>
</template>

<script>
export default {
  data() {
    return {
      menuOpen: false,
      navItems: [
        { name: 'Home', path: '/' },
        { name: 'About', path: '/about' }
      ]
    }
  },
  methods: {
    toggleMenu() {
      this.menuOpen = !this.menuOpen
    }
  }
}
</script>

<style>
@media (max-width: 768px) {
  ul {
    display: none;
  }
  ul.show {
    display: block;
  }
}
</style>

使用第三方 UI 库实现导航

使用 Element Plus 的菜单组件:

<template>
  <el-menu mode="horizontal" router>
    <el-menu-item index="/">Home</el-menu-item>
    <el-menu-item index="/about">About</el-menu-item>
    <el-submenu index="products">
      <template #title>Products</template>
      <el-menu-item index="/products/1">Product 1</el-menu-item>
      <el-menu-item index="/products/2">Product 2</el-menu-item>
    </el-submenu>
  </el-menu>
</template>

<script>
import { ElMenu, ElMenuItem, ElSubmenu } from 'element-plus'
export default {
  components: {
    ElMenu,
    ElMenuItem,
    ElSubmenu
  }
}
</script>

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

相关文章

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue系统实现

vue系统实现

Vue 系统实现的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装核心依赖(如 Vue 3、Vue Router、Pinia)。配置开发环境(如 ESLint、Prett…

实现vue模板解析

实现vue模板解析

Vue 模板解析的基本原理 Vue 的模板解析是将模板字符串转换为渲染函数的过程。核心步骤包括模板编译、AST 生成、优化和代码生成。 模板编译阶段 Vue 使用 vue-template-comp…

vue实现路由守卫

vue实现路由守卫

路由守卫的基本概念 路由守卫是 Vue Router 提供的一种机制,用于在路由跳转前后执行特定的逻辑。通过路由守卫,可以实现权限控制、页面访问限制、数据预加载等功能。 全局前置守卫 使用 rout…

vue修改功能实现

vue修改功能实现

Vue 修改功能的实现方法 在 Vue 中实现修改功能通常涉及表单绑定、数据更新和事件处理。以下是几种常见的实现方式: 数据双向绑定 使用 v-model 指令实现表单元素与 Vue 实例数据的双向…