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>省市區(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;
}
// 記得要有這個,控制側邊菜單寬度,意思是折疊的時候,寬度自適應,不著折疊的時候寬度為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搭建的,值的注意的時候,里面的圖標組件是經過處理的,所以使用起來,回和常規(guī)使用不一樣,將el-icon-xx替換為常規(guī)的element-plus圖標組件使用方式即可
您可能感興趣的文章:
- 去除Element-Plus下拉菜單邊框的實現(xiàn)步驟
- 詳解vue3+element-plus實現(xiàn)動態(tài)菜單和動態(tài)路由動態(tài)按鈕(前后端分離)
- vue3+element-plus動態(tài)路由菜單示例代碼
- Vue3 Element-plus el-menu無限級菜單組件封裝過程
- Vue3+Element-Plus?實現(xiàn)點擊左側菜單時顯示不同內容組件展示在Main區(qū)域功能
- Vue3+Element-Plus實現(xiàn)左側菜單折疊與展開功能示例
- vue3使用element-plus搭建后臺管理系統(tǒng)之菜單管理功能
- element-plus默認菜單打開步驟
相關文章
使用Element時默認勾選表格toggleRowSelection方式
這篇文章主要介紹了使用Element時默認勾選表格toggleRowSelection方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue-cli打包后本地運行dist文件中的index.html操作
這篇文章主要介紹了vue-cli打包后本地運行dist文件中的index.html操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

