Vue3菜单展开和收起实现

 更新时间:2024年09月18日 09:49:56   作者:jnfy  
在Vue项目中实现首页布局,包括可收放的左侧菜单和主体内容区,在store中管理菜单状态,通过修改isCollapse状态控制菜单的展开与收起,在home.vue中编写左侧菜单栏的代码和样式,实现一个响应式的用户界面

Vue3菜单展开和收起

先确定我们首页的布局

我们使用一下这种,在src/views/home.vue:

<template>
  <div class="common-layout">
    <el-container>
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-header>Header</el-header>
        <el-main>Main</el-main>
      </el-container>
    </el-container>
  </div>
</template>

先写一下我们左边的菜单

菜单可以收起和展开,所以我们在store/module/common.js 中定义一个 isCollapse状态和修改isCollapse的方法。

import { defineStore } from 'pinia'
export const useCommonStore = defineStore('common', {
  state: () => {
    return {
      token: null,
      isCollapse: false, //默认值
    }
  },
  actions: {
   //改变isCollapse
    updateCollapse() {
      this.isCollapse = !this.isCollapse
    },
  },
  persist: true,
})

这是我要实现的左侧菜单栏代码和样式

//src/views/home.vue
<template>
  <div>
    <el-container>
      <el-aside @mouseover="mouseover" :width="store.isCollapse ? '100px' : '258px'">
        <div class="sidebar">
          <div  class="ga-flex ga-ai-c ga-j-sb  logo-warp p-15">
            <img class="logo m-r-10" src="./../assets/images/galaxy-logo.png" />
            <h2  v-if="!store.isCollapse" class="flex-1">GaUI</h2>
            <a @click="open" v-if="!store.isCollapse">
              <i class="icon-align-left f-20"></i>
            </a>
          </div>
          <div class="p-15">
            <el-menu
              active-text-color="#534686"
              class="el-menu-vertical-demo"
              :collapse="store.isCollapse"
              @open="handleOpen"
              @close="handleClose"
            >
                <el-sub-menu index="1">
                  <template #title>
                    <el-icon><location /></el-icon>
                    <span>Navigator One</span>
                  </template>
                  <el-menu-item index="1-1">item one</el-menu-item>
                  <el-menu-item index="1-2">item two</el-menu-item>
                 
                </el-sub-menu>
             
            </el-menu>
          </div>
        </div>
      </el-aside>
      <el-container>
        <el-header>
          <div>header</div>
        </el-header>
        <el-main>
          Main
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </div>
</template>
<script setup>
import { useCommonStore } from '../store/module/common.js'
const store = useCommonStore()
// 点击展开收起
const open = () => {
  store.updateCollapse()
}
// 如果是收起状态,那么鼠标移入时则展开
const mouseover = () => {
  if (store.isCollapse) {
    store.updateCollapse()
  }
}
const handleOpen = (key, keyPath) => {
  //console.log(key, keyPath)
}
const handleClose = (key, keyPath) => {
  //console.log(key, keyPath)
}
</script>
<style lang="scss" scoped>
.sidebar {
  background-color: #ffffff;
  transition: 0.3s;
  min-height: 100vh;
  .logo-warp {
    padding: 20px 25px;
    box-shadow: -9px 0 20px rgb(89 102 122 / 10%);
    .logo {
      width: 50px;
      height: 50px;
    }
    a{
      &:hover{
        color: #534686;
      }
    }
  }
  .el-sub-menu__title:hover {
    background-color: var(--el-menu-hover-bg-color);
  }
}
.el-aside {
  box-shadow: 0 0 21px 0 rgb(89 102 122 / 10%);
}
.el-menu {
  border-right: none !important;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 228px;
  min-height: 100%;
}
</style>

左侧菜单效果:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue 实现登录界面验证码功能

    Vue 实现登录界面验证码功能

    本文通过实例代码给大家介绍了Vue 实现登录界面 验证码功能,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-01-01
  • Vue2模版编译流程详解

    Vue2模版编译流程详解

    vue中有一张响应式系统的流程图,vue会将模板语法编译成render函数,通过render函数渲染生成Virtual dom,但是官方并没有对模板编译有详细的介绍,这篇文章带大家一起学习下vue的模板编译
    2023-07-07
  • 详解vue为什么要求组件模板只能有一个根元素

    详解vue为什么要求组件模板只能有一个根元素

    这篇文章主要介绍了vue为什么要求组件模板只能有一个根元素,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-07-07
  • vue中常用方法的用法汇总

    vue中常用方法的用法汇总

    Vue.js 是一个用于构建用户界面的渐进式框架,本文主要为大家整理了一些常用的 Vue 方法及其详细说明和代码示例,有需要的小伙伴可以参考一下
    2023-11-11
  • elementui的table列超出隐藏tooltip悬浮显示问题

    elementui的table列超出隐藏tooltip悬浮显示问题

    这篇文章主要介绍了elementui的table列超出隐藏tooltip悬浮显示问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • 10个Vue3中常用的组合式 API用法详解

    10个Vue3中常用的组合式 API用法详解

    通过Vue 3,组合式API增强了我们利用Vue的能力,使我们的代码更具模块性和可读性,本文主要来和大家分享10个常用的Vue3组合式API,希望对大家有所帮助
    2024-01-01
  • vuex之this.$store.dispatch()与this.$store.commit()的区别及说明

    vuex之this.$store.dispatch()与this.$store.commit()的区别及说明

    这篇文章主要介绍了vuex之this.$store.dispatch()与this.$store.commit()的区别及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Vue中router.beforeEach与beforeRouteEnter的区别及说明

    Vue中router.beforeEach与beforeRouteEnter的区别及说明

    这篇文章主要介绍了Vue中router.beforeEach与beforeRouteEnter的区别及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue.js实现备忘录功能的方法

    vue.js实现备忘录功能的方法

    下面小编就为大家带来一篇vue.js实现备忘录功能的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Vue keep-alive实践总结(推荐)

    Vue keep-alive实践总结(推荐)

    本篇文章主要介绍了Vue keep-alive实践总结(推荐),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08

最新评论