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 mysql5.6 master-slave setup and asynchronous issues

Table of contents 1. MySQL master-slave replicati...

Ubuntu Basic Tutorial: apt-get Command

Preface The apt-get command is a package manageme...

Personal opinion: Talk about design

<br />Choose the most practical one to talk ...

Summary of MySQL string interception related functions

This article introduces MySQL string interception...

Detailed explanation of JavaScript progress management

Table of contents Preface question principle test...

A brief analysis of the knowledge points of exporting and importing MySQL data

Often, we may need to export local database data ...

zabbix custom monitoring nginx status implementation process

Table of contents Zabbix custom monitoring nginx ...

A small question about the execution order of SQL in MySQL

I encountered a sql problem at work today, about ...

Solution to ERROR 1054 (42S22) when changing password in MySQL 5.7

I have newly installed MySQL 5.7. When I log in, ...

MySQL example of getting today and yesterday's 0:00 timestamp

As shown below: Yesterday: UNIX_TIMESTAMP(CAST(SY...

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

Gearman + MySQL to achieve persistence operation example

This article uses the gearman+mysql method to imp...

Teach you step by step to develop a brick-breaking game with vue3

Preface I wrote a few examples using vue3, and I ...