当前位置:首页 > VUE

vue实现侧导航

2026-02-10 17:43:22VUE

Vue 实现侧边导航

使用 Vue Router 和动态组件

在 Vue 中实现侧边导航可以通过结合 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: '首页' },
        { path: '/about', title: '关于' },
        { path: '/contact', title: '联系我们' }
      ]
    }
  }
}
</script>

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

.sidebar ul {
  list-style: none;
  padding: 0;
}

.sidebar li {
  padding: 10px 15px;
}

.sidebar a {
  text-decoration: none;
  color: #333;
}

.sidebar a.router-link-exact-active {
  color: #42b983;
  font-weight: bold;
}
</style>

使用 Element UI 的导航组件

如果项目中使用了 Element UI,可以利用其提供的导航组件快速实现:

vue实现侧导航

<template>
  <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-group>
        <el-menu-item index="1-1">选项1</el-menu-item>
        <el-menu-item index="1-2">选项2</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
    <el-menu-item index="2">
      <i class="el-icon-menu"></i>
      <span>导航二</span>
    </el-menu-item>
  </el-menu>
</template>

<script>
export default {
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    }
  }
}
</script>

响应式侧边导航

实现一个可以折叠展开的响应式侧边导航:

<template>
  <div class="sidebar-container" :class="{ 'collapsed': isCollapsed }">
    <div class="toggle-btn" @click="isCollapsed = !isCollapsed">
      {{ isCollapsed ? '>' : '<' }}
    </div>
    <nav>
      <router-link v-for="item in menuItems" :key="item.path" :to="item.path">
        <i :class="item.icon"></i>
        <span v-show="!isCollapsed">{{ item.title }}</span>
      </router-link>
    </nav>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapsed: false,
      menuItems: [
        { path: '/dashboard', title: '仪表盘', icon: 'el-icon-s-home' },
        { path: '/users', title: '用户管理', icon: 'el-icon-user' },
        { path: '/settings', title: '设置', icon: 'el-icon-setting' }
      ]
    }
  }
}
</script>

<style>
.sidebar-container {
  width: 200px;
  height: 100vh;
  background: #304156;
  transition: width 0.3s;
  position: relative;
}

.sidebar-container.collapsed {
  width: 64px;
}

.toggle-btn {
  color: white;
  text-align: center;
  padding: 10px;
  cursor: pointer;
}

nav {
  padding-top: 20px;
}

nav a {
  display: block;
  color: #bfcbd9;
  padding: 12px 20px;
  text-decoration: none;
}

nav a:hover {
  background: #263445;
}

nav a.router-link-exact-active {
  background: #263445;
  color: #409EFF;
}

nav i {
  margin-right: 10px;
}
</style>

使用 Vuex 管理导航状态

对于大型应用,可以使用 Vuex 来管理侧边导航的状态:

vue实现侧导航

// store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    sidebarOpen: true
  },
  mutations: {
    toggleSidebar(state) {
      state.sidebarOpen = !state.sidebarOpen
    }
  }
})

在组件中使用:

<template>
  <div class="sidebar" :class="{ 'collapsed': !sidebarOpen }">
    <button @click="toggleSidebar">切换</button>
    <!-- 导航内容 -->
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  computed: {
    ...mapState(['sidebarOpen'])
  },
  methods: {
    ...mapMutations(['toggleSidebar'])
  }
}
</script>

动态权限控制导航

根据用户权限动态生成导航菜单:

// 在组件中
export default {
  data() {
    return {
      menuItems: []
    }
  },
  created() {
    this.generateMenuBasedOnPermissions()
  },
  methods: {
    generateMenuBasedOnPermissions() {
      const userPermissions = this.$store.getters.permissions

      const allMenuItems = [
        { path: '/dashboard', title: '仪表盘', permission: 'view_dashboard' },
        { path: '/admin', title: '管理', permission: 'admin_access' },
        { path: '/reports', title: '报告', permission: 'view_reports' }
      ]

      this.menuItems = allMenuItems.filter(item => 
        userPermissions.includes(item.permission)
      )
    }
  }
}

标签: vue
分享给朋友:

相关文章

vue实现a

vue实现a

Vue 实现 A 功能的方法 在 Vue 中实现 A 功能可以通过多种方式,具体取决于 A 功能的具体需求。以下是几种常见的实现方法: 使用 Vue 组件 通过创建自定义组件来实现 A 功能。组件可…

vue 实现挂载

vue 实现挂载

Vue 实现挂载的基本方法 在 Vue 中,挂载是将 Vue 实例与 DOM 元素关联的过程。通过挂载,Vue 可以管理 DOM 并实现数据绑定和响应式更新。 创建 Vue 实例并挂载 const…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul&g…

vue实现rtc

vue实现rtc

Vue 实现 RTC 的步骤 安装依赖 在 Vue 项目中安装 WebRTC 相关依赖,如 simple-peer 或 peerjs,用于简化 WebRTC 的实现。 运行以下命令安装 simple-…

vue实现换行

vue实现换行

Vue 中实现换行的几种方法 在 Vue 中实现换行可以通过多种方式,具体取决于使用场景和需求。以下是常见的几种方法: 使用 HTML 的 <br> 标签 在模板中直接使用 <br…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…