当前位置:首页 > VUE

vue实现左侧导航

2026-01-17 06:51:02VUE

Vue 实现左侧导航的方法

使用 Element UI 的导航组件

Element UI 提供了现成的导航组件,适合快速搭建左侧导航栏。安装 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-menu-item index="1">
      <i class="el-icon-menu"></i>
      <span>首页</span>
    </el-menu-item>
    <el-submenu index="2">
      <template #title>
        <i class="el-icon-location"></i>
        <span>导航一</span>
      </template>
      <el-menu-item index="2-1">选项1</el-menu-item>
      <el-menu-item index="2-2">选项2</el-menu-item>
    </el-submenu>
  </el-menu>
</template>

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

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

自定义 Vue 导航组件

如果需要更灵活的导航栏,可以手动实现一个基于 Vue 的导航组件。通过 v-for 动态渲染导航项,结合路由实现跳转。

<template>
  <div class="nav-container">
    <div 
      v-for="item in navItems" 
      :key="item.path"
      class="nav-item"
      :class="{ 'active': currentRoute === item.path }"
      @click="navigateTo(item.path)">
      {{ item.name }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      navItems: [
        { name: '首页', path: '/' },
        { name: '关于', path: '/about' },
        { name: '联系', path: '/contact' }
      ],
      currentRoute: '/'
    };
  },
  methods: {
    navigateTo(path) {
      this.currentRoute = path;
      this.$router.push(path);
    }
  }
};
</script>

<style>
.nav-container {
  width: 200px;
  background-color: #2c3e50;
  color: white;
  height: 100vh;
}
.nav-item {
  padding: 12px 20px;
  cursor: pointer;
}
.nav-item:hover {
  background-color: #34495e;
}
.active {
  background-color: #16a085;
}
</style>

结合 Vue Router 实现导航

Vue Router 可以与导航组件结合,实现路由跳转和高亮当前路由。通过 router-link 或编程式导航完成跳转。

<template>
  <div class="sidebar">
    <router-link 
      v-for="route in routes" 
      :key="route.path"
      :to="route.path"
      class="nav-link"
      active-class="active">
      {{ route.name }}
    </router-link>
  </div>
</template>

<script>
export default {
  data() {
    return {
      routes: [
        { name: '仪表盘', path: '/dashboard' },
        { name: '用户管理', path: '/users' },
        { name: '设置', path: '/settings' }
      ]
    };
  }
};
</script>

<style>
.sidebar {
  width: 220px;
  background: #f5f5f5;
  padding: 20px;
}
.nav-link {
  display: block;
  padding: 10px;
  color: #333;
  text-decoration: none;
}
.active {
  color: #42b983;
  font-weight: bold;
}
</style>

响应式左侧导航

为了实现响应式效果,可以通过 CSS 媒体查询或 Vue 的状态管理控制导航栏的显示与隐藏。

vue实现左侧导航

<template>
  <div class="nav-wrapper" :class="{ 'collapsed': isCollapsed }">
    <button @click="toggleCollapse">切换</button>
    <div class="nav-content">
      <!-- 导航内容 -->
    </div>
  </div>
</template>

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

<style>
.nav-wrapper {
  width: 250px;
  transition: width 0.3s;
}
.collapsed {
  width: 60px;
}
@media (max-width: 768px) {
  .nav-wrapper {
    width: 100%;
  }
}
</style>

标签: vue
分享给朋友:

相关文章

vue实现多个tab

vue实现多个tab

Vue 实现多个 Tab 的方法 使用动态组件切换 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现多个…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue实现盖章

vue实现盖章

Vue 实现盖章功能 在Vue中实现盖章功能通常涉及前端UI交互,通过Canvas或SVG绘制印章,并允许用户拖拽或点击放置印章。以下是具体实现方法: 使用Canvas绘制印章 通过HTML5的Ca…

vue实现切换

vue实现切换

Vue 实现切换功能的方法 在 Vue 中实现切换功能可以通过多种方式实现,以下是一些常见的方法: 使用 v-if 和 v-else 指令 通过条件渲染实现切换功能,适用于简单的显示/隐藏场景。…

vue 实现拖动

vue 实现拖动

Vue 实现拖动的几种方法 在Vue中实现拖动功能可以通过多种方式,包括原生HTML5的拖放API、第三方库如vuedraggable等。以下是几种常见的实现方法: 使用HTML5拖放API HTM…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…