当前位置:首页 > VUE

vue实现左侧导航

2026-02-17 23:19:01VUE

Vue 实现左侧导航

使用 Element UI 的 Menu 组件

Element UI 提供了 el-menu 组件,可以快速实现左侧导航。以下是一个基本示例:

<template>
  <el-menu
    default-active="1"
    class="el-menu-vertical"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b">
    <el-submenu index="1">
      <template #title>
        <i class="el-icon-location"></i>
        <span>导航一</span>
      </template>
      <el-menu-item index="1-1">选项1</el-menu-item>
      <el-menu-item index="1-2">选项2</el-menu-item>
    </el-submenu>
    <el-menu-item index="2">
      <i class="el-icon-menu"></i>
      <span>导航二</span>
    </el-menu-item>
  </el-menu>
</template>

<script>
export default {
  name: 'SideNav'
}
</script>

<style>
.el-menu-vertical {
  height: 100vh;
}
</style>

使用 Vue Router 实现导航跳转

结合 Vue Router,可以为导航菜单添加路由功能:

<template>
  <el-menu
    :router="true"
    default-active="/"
    class="el-menu-vertical"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b">
    <el-menu-item index="/">
      <i class="el-icon-house"></i>
      <span>首页</span>
    </el-menu-item>
    <el-menu-item index="/about">
      <i class="el-icon-document"></i>
      <span>关于</span>
    </el-menu-item>
  </el-menu>
</template>

自定义导航组件

如果需要完全自定义导航,可以使用 Vue 的动态组件和样式:

<template>
  <div class="sidebar">
    <div 
      v-for="item in navItems" 
      :key="item.path"
      class="nav-item"
      :class="{ 'active': currentPath === item.path }"
      @click="navigateTo(item.path)">
      <i :class="item.icon"></i>
      <span>{{ item.title }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPath: '/',
      navItems: [
        { path: '/', title: '首页', icon: 'icon-home' },
        { path: '/about', title: '关于', icon: 'icon-info' }
      ]
    }
  },
  methods: {
    navigateTo(path) {
      this.currentPath = path
      this.$router.push(path)
    }
  }
}
</script>

<style>
.sidebar {
  width: 200px;
  height: 100vh;
  background: #2c3e50;
  color: white;
}
.nav-item {
  padding: 12px 20px;
  cursor: pointer;
}
.nav-item:hover {
  background: #34495e;
}
.active {
  background: #34495e;
  border-left: 4px solid #42b983;
}
</style>

响应式导航

添加响应式功能,在小屏幕下隐藏导航或显示汉堡菜单:

<template>
  <div>
    <button @click="toggleSidebar" class="hamburger">
      ☰
    </button>
    <div class="sidebar" :class="{ 'collapsed': isCollapsed }">
      <!-- 导航内容 -->
    </div>
  </div>
</template>

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

<style>
.hamburger {
  display: none;
}
@media (max-width: 768px) {
  .hamburger {
    display: block;
  }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }
  .sidebar.collapsed {
    transform: translateX(0);
  }
}
</style>

嵌套路由导航

对于多级导航,可以使用嵌套路由和递归组件:

vue实现左侧导航

<template>
  <div>
    <div v-for="item in navItems" :key="item.path">
      <div @click="toggleExpand(item)">
        {{ item.title }}
        <span v-if="item.children">{{ isExpanded(item) ? '−' : '+' }}</span>
      </div>
      <div v-if="item.children && isExpanded(item)" class="submenu">
        <NavItem :items="item.children" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'NavItem',
  props: ['items'],
  data() {
    return {
      expandedItems: []
    }
  },
  methods: {
    isExpanded(item) {
      return this.expandedItems.includes(item.path)
    },
    toggleExpand(item) {
      if (item.children) {
        const index = this.expandedItems.indexOf(item.path)
        if (index > -1) {
          this.expandedItems.splice(index, 1)
        } else {
          this.expandedItems.push(item.path)
        }
      } else {
        this.$router.push(item.path)
      }
    }
  }
}
</script>

标签: vue
分享给朋友:

相关文章

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件可…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue实现vnc

vue实现vnc

Vue 实现 VNC 客户端 在 Vue 项目中实现 VNC 客户端功能,可以通过集成现有的 VNC 客户端库或组件完成。以下是具体实现方法: 安装依赖库 使用 noVNC 或其他 VNC 客户端…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…