当前位置:首页 > VUE

vue实现导航栏

2026-02-10 22:28:33VUE

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: '/', name: 'Home', component: Home },
  { path: '/about', name: 'About', component: About }
]

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

export default router

在 App.vue 中使用 <router-link> 创建导航:

vue实现导航栏

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

响应式导航栏实现

使用 Vue 的响应式特性实现移动端菜单:

<template>
  <nav>
    <div class="menu-toggle" @click="toggleMenu">
      <span></span>
      <span></span>
      <span></span>
    </div>
    <ul :class="{ 'active': isActive }">
      <li><router-link to="/">Home</router-link></li>
      <li><router-link to="/about">About</router-link></li>
    </ul>
  </nav>
</template>

<script>
export default {
  data() {
    return {
      isActive: false
    }
  },
  methods: {
    toggleMenu() {
      this.isActive = !this.isActive
    }
  }
}
</script>

<style>
.menu-toggle {
  display: none;
  cursor: pointer;
}

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

导航栏样式优化

添加 CSS 过渡效果:

vue实现导航栏

nav {
  background: #333;
  padding: 1rem;
}

router-link {
  color: white;
  text-decoration: none;
  margin: 0 1rem;
  transition: color 0.3s;
}

router-link:hover {
  color: #42b983;
}

ul {
  list-style: none;
  transition: all 0.3s ease;
}

动态高亮当前路由

在导航组件中添加高亮逻辑:

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'

export default {
  setup() {
    const route = useRoute()
    const currentRoute = computed(() => route.path)

    return { currentRoute }
  }
}
</script>

<template>
  <nav>
    <router-link 
      to="/" 
      :class="{ 'active': currentRoute === '/' }"
    >
      Home
    </router-link>
    <router-link 
      to="/about" 
      :class="{ 'active': currentRoute === '/about' }"
    >
      About
    </router-link>
  </nav>
</template>

<style>
.active {
  color: #42b983;
  font-weight: bold;
}
</style>

嵌套路由导航

实现多级导航结构:

// router/index.js
const routes = [
  {
    path: '/products',
    component: Products,
    children: [
      { path: '', component: ProductList },
      { path: ':id', component: ProductDetail }
    ]
  }
]
<!-- Products.vue -->
<template>
  <div>
    <nav class="sub-nav">
      <router-link to="/products">List</router-link>
      <router-link to="/products/1">Detail</router-link>
    </nav>
    <router-view/>
  </div>
</template>

标签: vue
分享给朋友:

相关文章

vue实现安全免登录

vue实现安全免登录

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

vue实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── K…

实现vue table

实现vue table

Vue 表格实现方法 基础表格实现 使用 Vue 的 v-for 指令可以快速渲染表格数据。在模板中定义表格结构,通过数据绑定动态生成行和列。 <template> <tab…