Vue+Element UI實(shí)現(xiàn)下拉菜單的封裝
本文實(shí)例為大家分享了Vue+Element UI實(shí)現(xiàn)下拉菜單封裝的具體代碼,供大家參考,具體內(nèi)容如下
1、效果圖
先貼個(gè)效果圖,菜單項(xiàng)沒有做樣式美化,圖中顯示的邊框也是沒有的(邊框是外部容器的邊框),其它的根據(jù)需要自己修改一下樣式即可。
2、組件封裝
組件的封裝用到了CSS動(dòng)畫、定位,以及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: { //父級(jí)圖標(biāo)點(diǎn)擊事件 clickFire() { //參數(shù)1:自定義組件事件,在父組件中被調(diào)用才能觸發(fā)父子組件的值傳遞;參數(shù)2:向父組件傳遞的數(shù)據(jù)[可為數(shù)組形式] this.$emit('clickItem', this.$data.item) }, //下拉菜單點(diǎn)擊事件 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; //標(biāo)題項(xiàng) .el-dropdown-link { cursor: pointer; } //菜單子項(xiàng) .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-導(dǎo)入子組件 import RingItem from '../Controls/RingItem' export default { components: { //2-注冊(cè)組件 RingItem, }, data() { return { //功能模塊圖標(biāo)資源 funcItems: [ { name: '系統(tǒng)管理', url: require('../../../static/imgs/menus/management.png'), menus: ['細(xì)則管理', '關(guān)于我們'], }, ], } }, methods: { /** * RingItem子組件點(diǎn)擊事件:value是子組件中通過emit傳遞的值 */ clickRingItem(value) { //判斷子組件的name屬性值,實(shí)現(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子組件:下拉菜單點(diǎn)擊事件(value是子組件中通過emit傳遞的值) */ handleCommandDropMenu(value) { console.log(value) switch (value.name) { case '細(xì)則管理': { 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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)tab切換的3種方式及切換保持?jǐn)?shù)據(jù)狀態(tài)
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)tab切換的3種方式及切換保持?jǐn)?shù)據(jù)狀態(tài)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05vue3 表單搜索內(nèi)容回顯到地址欄的實(shí)例代碼
這篇文章主要介紹了vue3 表單搜索內(nèi)容回顯到地址欄的實(shí)例代碼,地址欄輸入內(nèi)容回顯到form表單,同理表單輸入內(nèi)容也要回顯到地址欄中,本文結(jié)合實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09vue使用百度地圖報(bào)錯(cuò)BMap?is?not?defined問題及解決
這篇文章主要介紹了vue使用百度地圖報(bào)錯(cuò)BMap?is?not?defined問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue實(shí)現(xiàn)input寬度隨文字長度自適應(yīng)操作
這篇文章主要介紹了Vue實(shí)現(xiàn)input寬度隨文字長度自適應(yīng)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07antv完成區(qū)間柱形圖一列多柱配置實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了antv完成區(qū)間柱形圖一列多柱配置實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10基于Vue3創(chuàng)建一個(gè)簡單的倒計(jì)時(shí)組件
這篇文章主要給大家介紹了基于Vue3創(chuàng)建一個(gè)簡單的倒計(jì)時(shí)組件的代碼示例,文中通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11