当前位置:首页 > VUE

vue实现侧边菜单

2026-01-18 10:09:17VUE

使用Vue实现侧边菜单

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

基于Element UI的侧边菜单

Element UI提供了现成的侧边栏组件,可以直接使用:

vue实现侧边菜单

<template>
  <el-container>
    <el-aside width="200px">
      <el-menu
        default-active="1"
        class="el-menu-vertical-demo"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
        <el-submenu index="1">
          <template slot="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>
    </el-aside>
    <el-main>主内容区域</el-main>
  </el-container>
</template>

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

自定义侧边菜单组件

如果需要完全自定义的侧边菜单,可以创建一个组件:

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

<script>
export default {
  data() {
    return {
      menuItems: [
        { id: 1, title: '首页', path: '/' },
        { id: 2, title: '关于', path: '/about' },
        { id: 3, title: '服务', path: '/services' }
      ]
    }
  }
}
</script>

<style scoped>
.sidebar {
  width: 200px;
  background-color: #f5f5f5;
  height: 100vh;
}
.menu-item {
  padding: 12px 20px;
  border-bottom: 1px solid #ddd;
}
</style>

使用Vue Router实现动态侧边菜单

结合Vue Router可以实现更灵活的侧边菜单:

vue实现侧边菜单

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

<script>
export default {
  computed: {
    filteredRoutes() {
      return this.$router.options.routes.filter(
        route => route.meta && route.meta.showInMenu
      )
    }
  }
}
</script>

在router配置中需要添加meta信息:

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { title: '首页', showInMenu: true }
  },
  {
    path: '/about',
    name: 'About',
    component: About,
    meta: { title: '关于', showInMenu: true }
  }
]

响应式侧边菜单

添加响应式功能,使菜单在小屏幕上可以折叠:

<template>
  <div>
    <button @click="toggleMenu">切换菜单</button>
    <div class="sidebar" :class="{ 'collapsed': isCollapsed }">
      <!-- 菜单内容 -->
    </div>
  </div>
</template>

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

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

以上方法可以根据项目需求选择使用,Element UI适合快速开发,自定义组件提供更大灵活性,结合Vue Router可以实现动态菜单,响应式设计则能适应不同屏幕尺寸。

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

相关文章

vue实现左右菜单联动实现

vue实现左右菜单联动实现

Vue 实现左右菜单联动 使用事件总线通信 在 Vue 中可以通过事件总线实现左右菜单的通信。创建一个中央事件总线实例,让左右菜单通过事件监听和触发实现联动。 // eventBus.js impo…

vue实现侧边抽屉

vue实现侧边抽屉

实现侧边抽屉的基本方法 在Vue中实现侧边抽屉效果,可以通过以下几种方式完成。侧边抽屉通常用于导航菜单、设置面板或其他需要从屏幕边缘滑出的内容。 使用Vue原生组件 通过Vue的条件渲染和CS…

vue菜单tab实现

vue菜单tab实现

实现Vue菜单Tab的步骤 在Vue中实现菜单Tab功能,可以通过动态组件或条件渲染来实现。以下是几种常见的方法: 使用动态组件 动态组件允许根据当前选中的Tab动态切换显示内容。定义一个组件…

vue elementui实现菜单

vue elementui实现菜单

Vue ElementUI 实现菜单 安装 ElementUI 确保项目已安装 Vue 和 ElementUI。若未安装,可以通过以下命令安装: npm install element-ui 在…

vue实现导航菜单

vue实现导航菜单

Vue 实现导航菜单的方法 基础路由导航实现 在 Vue 项目中,使用 vue-router 可以轻松实现导航菜单功能。首先安装并配置路由: npm install vue-router 在 ro…

css制作横向菜单

css制作横向菜单

使用浮动(Float)实现横向菜单 将菜单项的 display 属性设置为 float: left,使列表项横向排列。清除浮动避免影响后续布局。 ul.horizontal-menu { lis…