当前位置:首页 > 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实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

实现 vue ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project c…

vue 实现音乐

vue 实现音乐

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

vue实现单击

vue实现单击

Vue 实现单击事件的方法 在 Vue 中实现单击事件可以通过 v-on 指令或简写的 @ 符号绑定事件处理器。以下是几种常见的实现方式: 基础用法 通过 v-on:click 或 @click 绑…

vue递归实现

vue递归实现

vue递归实现方法 在Vue中实现递归组件通常用于树形结构、嵌套菜单等场景。以下是具体实现方式: 使用组件name属性递归 定义一个组件时,通过name属性让组件可以在自身模板中调用自己: <…