Vue+Element UI實現(xiàn)下拉菜單的封裝
本文實例為大家分享了Vue+Element UI實現(xiàn)下拉菜單封裝的具體代碼,供大家參考,具體內(nèi)容如下
1、效果圖
先貼個效果圖,菜單項沒有做樣式美化,圖中顯示的邊框也是沒有的(邊框是外部容器的邊框),其它的根據(jù)需要自己修改一下樣式即可。

2、組件封裝
組件的封裝用到了CSS動畫、定位,以及Element UI提供的下拉菜單組件el-dropdown,代碼如下
<template>
<div class="all" @click="clickFire">
<span class="item-border">
<el-image
class="item"
style="width: 24px; height: 24px"
fit="cover"
:lazy="isLazy"
:src="itemProperty.url"
:title="itemProperty.name"
:placeholder="itemProperty.name"
></el-image>
</span>
<div class="wrap-item"></div>
<!-- 下拉菜單 -->
<el-dropdown class="dropMenu" @command="handleCommand">
<span class="el-dropdown-link" v-text="itemProperty.name"></span>
<el-dropdown-menu slot="dropdown" class="dropMenuitems">
<!-- <el-dropdown-item>黃金糕</el-dropdown-item>
<el-dropdown-item>獅子頭</el-dropdown-item>
<el-dropdown-item>螺螄粉</el-dropdown-item> -->
<el-dropdown-item
class="dropMenu-item"
v-for="(item, index) in itemProperty.menus"
:key="index"
:command="item"
>{{ item }}</el-dropdown-item
>
</el-dropdown-menu>
</el-dropdown>
</div>
</template>
<script>
export default {
props: {
itemProperty: Object,
require: true,
},
data() {
return {
isLazy: true,
item: {
name: 'item',
url: require('../../../static/imgs/menus/warning.png'),
menus: [
'submenu-1',
'submenu-2',
'submenu-3',
'submenu-4',
'submenu-5',
],
},
}
},
mounted() {
this.$data.item = this.$props.itemProperty
// console.log(this.$props.itemProperty)
},
methods: {
//父級圖標點擊事件
clickFire() {
//參數(shù)1:自定義組件事件,在父組件中被調(diào)用才能觸發(fā)父子組件的值傳遞;參數(shù)2:向父組件傳遞的數(shù)據(jù)[可為數(shù)組形式]
this.$emit('clickItem', this.$data.item)
},
//下拉菜單點擊事件
handleCommand(command) {
// console.log(command)
this.$emit('handleCommand', command)
},
},
}
</script>
<style lang="less" scoped>
.all {
// border: 1px solid skyblue;
display: inline-block;
position: relative;
width: 65px;
height: 65px;
// overflow: hidden;
}
// 最內(nèi)層
.item-border {
display: inline-block;
margin: 0 auto;
margin-left: 0px;
margin-top: 10px;
width: 44px;
height: 44px;
border-radius: 50%;
border: 3px solid skyblue;
// background-color: slateblue;
.item {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
// 最外層
.wrap-item {
position: absolute;
top: 0;
left: 0;
display: inline-block;
width: 56px;
height: 56px;
border: 5px dotted transparent;
border-left: 5px dotted #73ffff;
border-left-width: 3px;
border-right-color: #73ffff;
border-top-color: transparent;
border-radius: 50%;
// background-color: burlywood;
animation: circle 3s infinite linear;
}
@keyframes circle {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(-360deg);
}
}
//下拉菜單
.dropMenu {
margin-top: 5px;
// background-color: yellowgreen;
color: #fff;
//標題項
.el-dropdown-link {
cursor: pointer;
}
//菜單子項
.el-dropdown-menu__item {
color: red !important;
}
.dropMenu-item {
background-color: rosybrown;
}
}
</style>
3、父組件中使用舉例
<template>
<!-- 功能模塊:使用子組件-注意自定義事件clickItem與handleCommand -->
<div class="funcModules">
<RingItem
class="ringitem-style"
v-for="(item, index) in funcItems"
:key="index"
:itemProperty="item"
@clickItem="clickRingItem"
@handleCommand="handleCommandDropMenu"
/>
</div>
</template>
<script>
//1-導入子組件
import RingItem from '../Controls/RingItem'
export default {
components: {
//2-注冊組件
RingItem,
},
data() {
return {
//功能模塊圖標資源
funcItems: [
{
name: '系統(tǒng)管理',
url: require('../../../static/imgs/menus/management.png'),
menus: ['細則管理', '關(guān)于我們'],
},
],
}
},
methods: {
/**
* RingItem子組件點擊事件:value是子組件中通過emit傳遞的值
*/
clickRingItem(value) {
//判斷子組件的name屬性值,實現(xiàn)相應(yīng)的業(yè)務(wù)邏輯
switch (value.name) {
case '系統(tǒng)管理': {
console.log('系統(tǒng)管理')
//執(zhí)行頁面跳轉(zhuǎn)-管理中心(看自己的需求,添加業(yè)務(wù)邏輯)
//this.$router.push({ path: '/admincenter' })
break
}
}
},
/**
* RingItem子組件:下拉菜單點擊事件(value是子組件中通過emit傳遞的值)
*/
handleCommandDropMenu(value) {
console.log(value)
switch (value.name) {
case '細則管理': {
console.log('系統(tǒng)管理')
//執(zhí)行頁面跳轉(zhuǎn)-管理中心(看自己的需求,添加業(yè)務(wù)邏輯)
//this.$router.push({ path: '/admincenter' })
break
}
case '關(guān)于我們': {
console.log('系統(tǒng)管理')
//執(zhí)行頁面跳轉(zhuǎn)-管理中心(看自己的需求,添加業(yè)務(wù)邏輯)
//this.$router.push({ path: '/admincenter' })
break
}
}
},
},
}
</script>
<style lang="less" scoped>
//樣式調(diào)整
</style>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實現(xiàn)tab切換的3種方式及切換保持數(shù)據(jù)狀態(tài)
這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)tab切換的3種方式及切換保持數(shù)據(jù)狀態(tài)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
vue使用百度地圖報錯BMap?is?not?defined問題及解決
這篇文章主要介紹了vue使用百度地圖報錯BMap?is?not?defined問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue實現(xiàn)input寬度隨文字長度自適應(yīng)操作
這篇文章主要介紹了Vue實現(xiàn)input寬度隨文字長度自適應(yīng)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
antv完成區(qū)間柱形圖一列多柱配置實現(xiàn)詳解
這篇文章主要為大家介紹了antv完成區(qū)間柱形圖一列多柱配置實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10

