vue3 element-plus二次封装组件系列之伸缩菜单制作

 更新时间:2023年01月16日 15:49:42   作者:https://blog.csdn.net/weixin_44832362/article/details/128170502  
这篇文章主要介绍了vue3 element-plus二次封装组件系列之伸缩菜单制作,是基于vue3 vite element-plus搭建的,值的注意的时候,里面的图标组件是经过处理的,结合实例代码介绍的非常详细,需要的朋友可以参考下

1、效果

折叠效果--只剩图标

展开效果--有图标有文字

 2、主要逻辑代码

home.vue--主页代码

<template>
  <div class="common-layout">
    <el-container>
      <!-- 侧边栏菜单 -->
      <el-aside width="auto">
        <nav-menu :collpase="state.isCollapse"/>
      </el-aside>
      <el-container>
        <el-header>
          <nav-header v-model:collpase="state.isCollapse" />
        </el-header>
        <el-main>Main</el-main>
      </el-container>
    </el-container>
  </div>
</template>
 
<script setup lang='ts'>
import {ref, reactive } from 'vue'
import NavMenu from '@/components/navMenu/index.vue'
import NavHeader from '@/components/navHeader/index.vue'
 
const state = reactive({
  // 控制折叠与展开
  isCollapse: false
})
 
</script>
<style scoped lang="scss">
  .common-layout {
    width: 100%;
    height: 100%;
    .el-container {
      height: 100%;
      // 就是不折叠的时候宽度是200px,折叠的时候宽度自适应
    }
  }
  svg {
    width: 1em;
    height: 1em;
    margin-right: 5px;
  }
</style>

nav-menu组件,侧边菜单组件代码 

<template>
  <el-menu
    default-active="2"
    class="el-menu-vertical-demo"
    :collapse="props.collpase"
  >
    <el-menu-item index="1">
      <el-icon-menu />
      <template #title>图标选择器</template>
    </el-menu-item>
    <el-menu-item index="2">
      <el-icon-aim />
      <template #title>省市区组件</template>
    </el-menu-item>
    <el-menu-item index="3">
      <el-icon-star />
      <template #title>待定</template>
    </el-menu-item>
  </el-menu>
</template>
 
<script setup lang='ts'>
const props = defineProps<{
  collpase: boolean
}>()
</script>
<style scoped lang="scss">
  svg {
    width: 1em;
    height: 1em;
    margin-right: 5px;
  }
  // 记得要有这个,控制侧边菜单宽度,意思是折叠的时候,宽度自适应,不着折叠的时候宽度为200px
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-menu {
    height: 100%;
  }
</style>

nav-header组件的代码

<template>
  <div class="collapse" @click="handleCollapse">
    <el-icon-expand v-if="props.collpase"></el-icon-expand>
    <el-icon-fold v-else></el-icon-fold>
  </div>
</template>
 
<script setup lang='ts'>
const  props = defineProps<{
  collpase: boolean
}>()
const emits = defineEmits<{
  // 这样写,父组件通过v-model传值进来,父组件那边就不用在定义事件改变这里传过去的值了
  // update:collpase 就会自动改变v-model传过来的值了
  (e:'update:collpase', value:boolean):void
}>()
 
const handleCollapse = ()=>{
  emits('update:collpase', !props.collpase)
}
 
</script>
<style scoped lang="scss">
.collapse {
  width: 2em;
  svg {
    width: 2em;
    height: 2em;
  }
}
</style>

完整代码地址

以上是基于vue3 vite element-plus搭建的,值的注意的时候,里面的图标组件是经过处理的,所以使用起来,回和常规使用不一样,将el-icon-xx替换为常规的element-plus图标组件使用方式即可

相关文章

  • Vue3 appear 失效的问题及如何使用 appear

    Vue3 appear 失效的问题及如何使用 appear

    appear 是一个在元素默认被显示的情况下 调用进入动画效果,使得元素在这种初次渲染情况下 执行进入动画的属性,最近在学习vue3的动画时遇到appear无法生效的问题,本文给大家详细讲解,感兴趣的朋友一起看看吧
    2023-10-10
  • 了解VUE的render函数的使用

    了解VUE的render函数的使用

    本篇文章主要介绍了了解VUE的render函数的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Vue中如何把hash模式改为history模式

    Vue中如何把hash模式改为history模式

    这篇文章主要介绍了Vue中如何把hash模式改为history模式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • Vue极简生成器 Vuepress的实现

    Vue极简生成器 Vuepress的实现

    本文主要介绍了Vue极简生成器 Vuepress的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧<BR>
    2022-06-06
  • 使用Element时默认勾选表格toggleRowSelection方式

    使用Element时默认勾选表格toggleRowSelection方式

    这篇文章主要介绍了使用Element时默认勾选表格toggleRowSelection方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue-cli打包后本地运行dist文件中的index.html操作

    vue-cli打包后本地运行dist文件中的index.html操作

    这篇文章主要介绍了vue-cli打包后本地运行dist文件中的index.html操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • vue动态设置页面title的方法实例

    vue动态设置页面title的方法实例

    这篇文章主要给大家介绍了关于vue动态设置页面title的相关资料,文中通过实例代码结束的非常详细,对大家学习或者使用Vue具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-08-08
  • 详解如何优雅运用Vue中的KeepAlive组件

    详解如何优雅运用Vue中的KeepAlive组件

    在Vue中,KeepAlive组件是一种特殊的组件,用于缓存已经渲染过的组件实例,本文主要为大家详细介绍了KeepAlive组件的用法,需要的小伙伴可以参考下
    2023-09-09
  • VueCli4项目配置反向代理proxy的方法步骤

    VueCli4项目配置反向代理proxy的方法步骤

    这篇文章主要介绍了VueCli4项目配置反向代理proxy的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 详解Vue-cli 创建的项目如何跨域请求

    详解Vue-cli 创建的项目如何跨域请求

    本篇文章主要介绍了详解Vue-cli 创建的项目如何跨域请求 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05

最新评论