Vue+Element UI realizes the encapsulation of drop-down menu

Vue+Element UI realizes the encapsulation of drop-down menu

This article example shares the specific code of Vue+Element UI to implement the drop-down menu encapsulation for your reference. The specific content is as follows

1. Rendering

I'll post a rendering first. The menu items are not styled, and there is no border shown in the picture (the border is the border of the outer container). You can modify the styles of the others as needed.

2. Component packaging

The component encapsulation uses CSS animation, positioning, and the drop-down menu component el-dropdown provided by Element UI. The code is as follows

<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>
    <!-- Drop-down menu -->
    <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>Golden Cake</el-dropdown-item>
        <el-dropdown-item>Lion head</el-dropdown-item>
        <el-dropdown-item>Snail Rice Noodles</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: {
    //Parent icon click event clickFire() {
      //Parameter 1: custom component event, which can be called in the parent component to trigger the value transfer of parent-child components; Parameter 2: data passed to the parent component [can be in array form]
      this.$emit('clickItem', this.$data.item)
    },
    //Drop-down menu click event 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;
}
// Innermost layer.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%);
  }
}

// Outermost layer.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);
  }
}
//Drop-down menu.dropMenu {
  margin-top: 5px;
  // background-color: yellowgreen;
  color: #fff;
  //Title item.el-dropdown-link {
    cursor: pointer;
  }
  //Menu sub-item.el-dropdown-menu__item {
    color: red !important;
  }
  .dropMenu-item {
    background-color: rosybrown;
  }
}
</style>

3. Example of use in parent component

<template>
    <!-- Functional module: Use subcomponents-pay attention to the custom events clickItem and 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 subcomponentimport RingItem from '../Controls/RingItem'
export default {
  components:
  //2-Register component RingItem,
  },
  data() {
    return {
      //Function module icon resources funcItems: [
        {
          name: 'System Management',
          url: require('../../../static/imgs/menus/management.png'),
          menus: ['Details Management', 'About Us'],
        },
      ],
    }
  },
  methods: {
    /**
     * RingItem subcomponent click event: value is the value passed by emit in the subcomponent*/
    clickRingItem(value) {
      //Judge the name attribute value of the subcomponent and implement the corresponding business logic switch (value.name) {
        case 'system management': {
          console.log('system management')
          //Execute page jump-Management Center (depending on your needs, add business logic)
          //this.$router.push({ path: '/admincenter' })
          break
        }
      }
    },
    /**
     * RingItem subcomponent: drop-down menu click event (value is the value passed by emit in the subcomponent)
     */
    handleCommandDropMenu(value) {
      console.log(value)
       switch (value.name) {
        case 'Details Management': {
          console.log('system management')
          //Execute page jump-Management Center (depending on your needs, add business logic)
          //this.$router.push({ path: '/admincenter' })
          break
        }
         case 'About us': {
          console.log('system management')
          //Execute page jump-Management Center (depending on your needs, add business logic)
          //this.$router.push({ path: '/admincenter' })
          break
        }
      }
    },
  },
}
</script>
<style lang="less" scoped>
//Style adjustment</style>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Sample code for implementing the history tag menu using vue+elementui+vuex+sessionStorage
  • Vue.js uses Element-ui to implement the navigation menu
  • Vue uses element-ui to implement menu navigation
  • Vue+element-ui adds a custom right-click menu method example
  • Vue + Element UI implements the menu function implementation code of the permission management system

<<:  Steps for Django to connect to local MySQL database (pycharm)

>>:  Docker FAQ

Recommend

Detailed explanation of MySQL date addition and subtraction functions

1. addtime() Add the specified number of seconds ...

Docker commands are implemented so that ordinary users can execute them

After installing docker, there will usually be a ...

Implementation ideas for docker registry image synchronization

Intro Previously, our docker images were stored i...

HTML is actually the application of learning several important tags

After the article "This Will Be a Revolution&...

Seven ways to implement array deduplication in JS

Table of contents 1. Using Set()+Array.from() 2. ...

Front-end state management (Part 1)

Table of contents 1. What is front-end state mana...

Detailed explanation of the basic use of Apache POI

Table of contents Basic Introduction Getting Star...

Diagram of the process of implementing direction proxy through nginx

This article mainly introduces the process of imp...

Pure CSS drop-down menu

Achieve results Implementation Code html <div ...

Methods and steps to access Baidu Maps API with JavaScript

Table of contents 1. Baidu Map API Access 2. Usin...

Tutorial on how to deploy LNMP and enable HTTPS service

What is LNMP: Linux+Nginx+Mysql+(php-fpm,php-mysq...

Analysis of MySQL Aborted connection warning log

Preface: Sometimes, the session connected to MySQ...

Analysis of the principle of Vue nextTick

Table of contents Event Loop miscroTask (microtas...

Implementation and usage scenarios of JS anti-shake throttling function

Table of contents 1. What is Function Anti-shake?...

Detailed introduction to JS basic concepts

Table of contents 1. Characteristics of JS 1.1 Mu...