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

How to export mysql table structure to excel

The requirements are as follows Export the table ...

Practical solution for Prometheus container deployment

environment Hostname IP address Serve Prometheus ...

How to use Vue to implement CSS transitions and animations

Table of contents 1. The difference between trans...

How to perfectly implement the grid layout with intervals on the page

Typical layout examples As shown in the above pic...

Modularity in Node.js, npm package manager explained

Table of contents The basic concept of modularity...

Analyzing Linux high-performance network IO and Reactor model

Table of contents 1. Introduction to basic concep...

7 ways to vertically center elements with CSS

【1】Know the width and height of the centered elem...

A simple way to change the password in MySQL 5.7

This is an official screenshot. After MySQL 5.7 i...

How to solve the problem that Seata cannot use MySQL 8 version

Possible reasons: The main reason why Seata does ...

Use of provide and inject in Vue3

1. Explanation of provide and inject Provide and ...

Detailed explanation of common operations of Docker images and containers

Image Accelerator Sometimes it is difficult to pu...

Implementation of Portals and Error Boundary Handling in React

Table of contents Portals Error Boundary Handling...

MySQL database constraints and data table design principles

Table of contents 1. Database constraints 1.1 Int...