当前位置:首页 > VUE

vue实现侧导航

2026-03-28 03:38:28VUE

Vue 实现侧边导航的方法

使用 Element UI 的 el-menu 组件

安装 Element UI:

npm install element-ui

在 main.js 中引入:

import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

在组件中使用:

<el-menu
  default-active="1"
  class="el-menu-vertical"
  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>

自定义侧边导航组件

创建 Sidebar.vue 组件:

vue实现侧导航

<template>
  <div class="sidebar">
    <div 
      v-for="item in menuItems" 
      :key="item.path"
      class="menu-item"
      :class="{ active: currentPath === item.path }"
      @click="navigateTo(item.path)">
      {{ item.title }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPath: '/',
      menuItems: [
        { title: '首页', path: '/' },
        { title: '产品', path: '/products' },
        { title: '关于', path: '/about' }
      ]
    }
  },
  methods: {
    navigateTo(path) {
      this.$router.push(path)
      this.currentPath = path
    }
  }
}
</script>

<style>
.sidebar {
  width: 200px;
  background: #2c3e50;
  height: 100vh;
  color: white;
}
.menu-item {
  padding: 15px;
  cursor: pointer;
}
.menu-item:hover {
  background: #34495e;
}
.menu-item.active {
  background: #42b983;
}
</style>

使用 Vue Router 实现导航联动

配置路由:

const routes = [
  { path: '/', component: Home },
  { path: '/products', component: Products },
  { path: '/about', component: About }
]

const router = new VueRouter({
  routes
})

在 Sidebar 组件中监听路由变化:

watch: {
  '$route'(to) {
    this.currentPath = to.path
  }
}

响应式侧边导航实现

添加响应式逻辑:

vue实现侧导航

data() {
  return {
    isCollapsed: false
  }
},
methods: {
  toggleCollapse() {
    this.isCollapsed = !this.isCollapsed
  }
}

样式调整:

.sidebar {
  transition: width 0.3s;
  width: 200px;
}
.sidebar.collapsed {
  width: 64px;
}

嵌套路由侧边导航

配置嵌套路由:

const routes = [
  {
    path: '/dashboard',
    component: Dashboard,
    children: [
      { path: 'overview', component: Overview },
      { path: 'settings', component: Settings }
    ]
  }
]

导航组件调整:

<el-menu-item index="/dashboard/overview">概览</el-menu-item>
<el-menu-item index="/dashboard/settings">设置</el-menu-item>

标签: vue
分享给朋友:

相关文章

vue实现滚动中断

vue实现滚动中断

Vue 实现滚动中断的方法 在 Vue 中实现滚动中断通常涉及监听滚动事件,并在特定条件下阻止默认行为或停止滚动。以下是几种常见方法: 监听滚动事件并阻止默认行为 通过 @scroll 或 @whe…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue轮询实现

vue轮询实现

Vue 轮询实现方法 在 Vue 中实现轮询可以通过以下几种方式: 使用 setInterval data() { return { pollInterval: null }…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue 实现音乐

vue 实现音乐

Vue 实现音乐播放功能 在 Vue 中实现音乐播放功能,可以通过 HTML5 的 <audio> 元素或第三方库如 howler.js 来实现。以下是两种方法的详细步骤: 使用 HTM…

vue实现 页面

vue实现 页面

Vue 实现页面的核心方法 创建 Vue 项目 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 是官方脚手架工具,适合传统项目;Vite 提供更快的现代构建体验。 npm cre…