当前位置:首页 > VUE

侧边菜单vue实现

2026-02-20 04:11:34VUE

侧边菜单的 Vue 实现方法

使用 Vue 实现侧边菜单可以通过多种方式完成,以下是几种常见的实现方法:

使用 Vue Router 和动态组件

通过 Vue Router 可以实现侧边菜单的导航功能,结合动态组件可以灵活切换内容。

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

<script>
export default {
  data() {
    return {
      menuItems: [
        { path: '/home', title: 'Home' },
        { path: '/about', title: 'About' },
        { path: '/contact', title: 'Contact' }
      ]
    }
  }
}
</script>

<style>
.sidebar {
  width: 200px;
  background-color: #f5f5f5;
  height: 100vh;
  position: fixed;
  left: 0;
  top: 0;
}
</style>

使用第三方组件库

许多流行的 Vue UI 组件库提供了现成的侧边菜单组件,例如 Element UI、Ant Design Vue 等。

<template>
  <el-menu
    default-active="1"
    class="el-menu-vertical"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b">
    <el-menu-item index="1">
      <i class="el-icon-menu"></i>
      <span>首页</span>
    </el-menu-item>
    <el-menu-item index="2">
      <i class="el-icon-document"></i>
      <span>文档</span>
    </el-menu-item>
  </el-menu>
</template>

<script>
import { ElMenu, ElMenuItem } from 'element-plus'

export default {
  components: {
    ElMenu,
    ElMenuItem
  }
}
</script>

实现可折叠侧边菜单

通过 Vue 的状态管理可以实现侧边菜单的折叠功能。

<template>
  <div class="sidebar" :class="{ 'collapsed': isCollapsed }">
    <button @click="toggleCollapse">Toggle</button>
    <ul>
      <li v-for="item in menuItems" :key="item.path">
        <router-link :to="item.path">
          <span v-if="!isCollapsed">{{ item.title }}</span>
        </router-link>
      </li>
    </ul>
  </div>
</template>

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

<style>
.sidebar {
  width: 200px;
  transition: width 0.3s;
}
.sidebar.collapsed {
  width: 50px;
}
</style>

响应式侧边菜单

结合媒体查询和 Vue 的状态管理,可以实现响应式的侧边菜单。

侧边菜单vue实现

<template>
  <div class="sidebar" :class="{ 'mobile-hidden': !showMobileMenu }">
    <!-- 菜单内容 -->
  </div>
</template>

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

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

以上方法可以根据具体需求选择使用,或者结合多种方法实现更复杂的功能。

标签: 侧边菜单
分享给朋友:

相关文章

vue实现下拉菜单

vue实现下拉菜单

实现下拉菜单的基本思路 使用Vue实现下拉菜单可以通过动态绑定v-show或v-if控制菜单显示隐藏,结合事件监听(如@click或@mouseenter)触发状态切换。以下是两种常见实现方式: 方…

vue实现菜单管理

vue实现菜单管理

Vue 实现菜单管理方案 方案一:基于路由的动态菜单 通过 Vue Router 的路由配置自动生成菜单结构,适用于权限管理系统。在路由元信息(meta)中添加菜单相关配置: const route…

vue实现菜单联动

vue实现菜单联动

实现菜单联动的基本思路 在Vue中实现菜单联动通常涉及父子组件通信或状态管理。核心是通过数据绑定和事件监听,确保一个菜单的选择能触发另一个菜单的更新。 数据准备与结构设计 定义菜单数据时,建议使用嵌…

vue实现悬浮菜单

vue实现悬浮菜单

实现悬浮菜单的基本思路 使用Vue实现悬浮菜单通常需要结合CSS定位和Vue的事件监听。核心是通过v-show或v-if控制菜单显隐,通过鼠标事件触发状态变化。 HTML结构示例 <templ…

vue实现菜单关闭

vue实现菜单关闭

Vue 实现菜单关闭功能 在 Vue 中实现菜单关闭功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-if 或 v-show 控制显示 通过数据绑定控制菜单的显示与隐藏。v-if 会完全移…

vue实现变色菜单

vue实现变色菜单

实现变色菜单的方法 在Vue中实现变色菜单可以通过多种方式完成,以下是几种常见的实现方法: 动态绑定class 通过Vue的v-bind:class或简写:class动态绑定CSS类,根据条件切换不…