vue3 element-plus二次封裝組件系列之伸縮菜單制作
1、效果
折疊效果--只剩圖標(biāo)

展開效果--有圖標(biāo)有文字

2、主要邏輯代碼
home.vue--主頁代碼
<template>
<div class="common-layout">
<el-container>
<!-- 側(cè)邊欄菜單 -->
<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%;
// 就是不折疊的時候?qū)挾仁?00px,折疊的時候?qū)挾茸赃m應(yīng)
}
}
svg {
width: 1em;
height: 1em;
margin-right: 5px;
}
</style>nav-menu組件,側(cè)邊菜單組件代碼
<template>
<el-menu
default-active="2"
class="el-menu-vertical-demo"
:collapse="props.collpase"
>
<el-menu-item index="1">
<el-icon-menu />
<template #title>圖標(biāo)選擇器</template>
</el-menu-item>
<el-menu-item index="2">
<el-icon-aim />
<template #title>省市區(qū)組件</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;
}
// 記得要有這個,控制側(cè)邊菜單寬度,意思是折疊的時候,寬度自適應(yīng),不著折疊的時候?qū)挾葹?00px
.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傳值進(jìn)來,父組件那邊就不用在定義事件改變這里傳過去的值了
// 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搭建的,值的注意的時候,里面的圖標(biāo)組件是經(jīng)過處理的,所以使用起來,回和常規(guī)使用不一樣,將el-icon-xx替換為常規(guī)的element-plus圖標(biāo)組件使用方式即可
- 去除Element-Plus下拉菜單邊框的實(shí)現(xiàn)步驟
- 詳解vue3+element-plus實(shí)現(xiàn)動態(tài)菜單和動態(tài)路由動態(tài)按鈕(前后端分離)
- vue3+element-plus動態(tài)路由菜單示例代碼
- Vue3 Element-plus el-menu無限級菜單組件封裝過程
- Vue3+Element-Plus?實(shí)現(xiàn)點(diǎn)擊左側(cè)菜單時顯示不同內(nèi)容組件展示在Main區(qū)域功能
- Vue3+Element-Plus實(shí)現(xiàn)左側(cè)菜單折疊與展開功能示例
- vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理功能
- element-plus默認(rèn)菜單打開步驟
相關(guān)文章
vue框架和react框架的區(qū)別以及各自的應(yīng)用場景使用
這篇文章主要介紹了vue框架和react框架的區(qū)別以及各自的應(yīng)用場景使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
關(guān)于SpringBoot與Vue交互跨域問題解決方案
最近在利用springboot+vue整合開發(fā)一個前后端分離的個人博客網(wǎng)站,所以這一篇總結(jié)一下在開發(fā)中遇到的一個問題,關(guān)于解決在使用vue和springboot在開發(fā)前后端分離的項(xiàng)目時,如何解決跨域問題。在這里分別分享兩種方法,分別在前端vue中解決和在后臺springboot中解決。2021-10-10
vue兩組件間值傳遞 $router.push實(shí)現(xiàn)方法
兩組件間傳值,可能包含多種情況,這篇文章主要介紹了vue兩組件間值傳遞 $router.push實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
Vue3源碼分析組件掛載創(chuàng)建虛擬節(jié)點(diǎn)
這篇文章主要為大家介紹了Vue3源碼分析組件掛載創(chuàng)建虛擬節(jié)點(diǎn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
vue使用swiper實(shí)現(xiàn)中間大兩邊小的輪播圖效果
這篇文章主要介紹了vue使用swiper實(shí)現(xiàn)中間大兩邊小的輪播圖效果,本文分步驟通過實(shí)例代碼講解的非常詳細(xì),需要的朋友可以參考下2019-11-11

