当前位置:首页 > VUE

vue实现左侧导航

2026-03-09 11:58:46VUE

Vue 实现左侧导航的方法

使用 Vue 实现左侧导航通常需要结合 Vue Router 和组件化思想。以下是一个完整的实现方案:

创建导航组件

新建一个 Sidebar.vue 组件作为左侧导航栏:

<template>
  <div class="sidebar">
    <div v-for="item in menuItems" :key="item.path">
      <router-link :to="item.path">{{ item.title }}</router-link>
    </div>
  </div>
</template>

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

<style scoped>
.sidebar {
  width: 200px;
  background-color: #f5f5f5;
  padding: 20px;
  height: 100vh;
}
</style>

配置路由

router/index.js 中配置路由:

vue实现左侧导航

import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'
import Contact from '../views/Contact.vue'

Vue.use(Router)

export default new Router({
  routes: [
    { path: '/home', component: Home },
    { path: '/about', component: About },
    { path: '/contact', component: Contact },
    { path: '*', redirect: '/home' }
  ]
})

在主布局中使用

App.vue 中使用导航组件:

<template>
  <div id="app">
    <Sidebar />
    <router-view />
  </div>
</template>

<script>
import Sidebar from './components/Sidebar.vue'

export default {
  components: {
    Sidebar
  }
}
</script>

<style>
#app {
  display: flex;
}
</style>

添加交互效果

为导航项添加激活状态样式:

vue实现左侧导航

<template>
  <router-link 
    :to="item.path" 
    active-class="active"
    exact-active-class="exact-active"
  >
    {{ item.title }}
  </router-link>
</template>

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

使用嵌套路由

对于多级导航菜单:

// router/index.js
{
  path: '/products',
  component: Products,
  children: [
    { path: 'list', component: ProductList },
    { path: 'detail/:id', component: ProductDetail }
  ]
}
<!-- Sidebar.vue -->
<div v-for="item in menuItems" :key="item.path">
  <router-link v-if="!item.children" :to="item.path">
    {{ item.title }}
  </router-link>
  <div v-else>
    <div>{{ item.title }}</div>
    <router-link 
      v-for="child in item.children" 
      :key="child.path"
      :to="item.path + '/' + child.path"
    >
      {{ child.title }}
    </router-link>
  </div>
</div>

响应式处理

添加响应式处理,在小屏幕下隐藏导航:

<template>
  <div class="sidebar" :class="{ 'hidden': !isVisible }">
    <!-- 导航内容 -->
  </div>
  <button @click="toggleSidebar">切换导航</button>
</template>

<script>
export default {
  data() {
    return {
      isVisible: true
    }
  },
  methods: {
    toggleSidebar() {
      this.isVisible = !this.isVisible
    }
  }
}
</script>

<style>
.sidebar.hidden {
  transform: translateX(-100%);
}
</style>

标签: vue
分享给朋友:

相关文章

vue实现页面截屏

vue实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm insta…

vue路由实现内部切换

vue路由实现内部切换

Vue 路由实现内部切换的方法 Vue Router 是 Vue.js 官方的路由管理器,用于实现单页应用(SPA)的页面切换。以下是几种常见的内部路由切换方式: 使用 <router-lin…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

vue实现ajax

vue实现ajax

Vue 中实现 AJAX 的几种方法 在 Vue 中实现 AJAX 请求可以通过原生 JavaScript 的 XMLHttpRequest、第三方库如 axios 或 fetch API 来完成。以…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue实现复制

vue实现复制

Vue 实现复制功能 在 Vue 中实现复制功能可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 Clipboard API Clipboard API 是现代浏览器提供的一种原生 AP…