Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the Global right-click menu with vue

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to customize the global right-click menu in vue". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The effect is shown in the figure:

Note:

You need to find the components of the overall layout of the page in the project, such as the project uses Home.vue as the public layout of the entire project, and set the positioning of the root element to relative so that the right-click menu is positioned relative to it.

The right-click menu in this paper is mainly implemented by vuex.

1. Define the global state in vuex to manage the right-click menu

Export default {/ * menuContent format: * [* {* name: 'create a file', / / operation name * method: 'createDirectory', / / the method in the corresponding component to be executed * disabled: true / / is disabled or not Can be adjusted according to the specific logic of the page *} *] * the overall right-click menu adopts absolute positioning So the representatives of clientX and clientY are left and top location * / state: {menuContent: [], / / right-click menu content clientX:', / / left clientY:', / / top displayContextMenu: false / / whether to display right-click menu}, mutations: {SET_CONTEXT_MENU: (state) Payload) = > {state.menuContent = payload.menuContent State.clientX = payload.clientX; state.clientY = payload.clientY; state.displayContextMenu = payload.displayContextMenu;},}}

Second, define the common component ContextMenu.vue

{{item.name}} import {mapState} from 'vuex';import bus from' @ / views/common/bus' Export default {name: 'ContextMenu', data () {return {}}, computed: {... mapState ([' contextMenu'])}, methods: {emitEvent (type) {bus.$emit ('operateDirectory', type)} .context-menu {position: absolute; background: # FFF Color: # 34495E; min-width: 100px; box-shadow: 0px 2px 4px 0px rgba (0,0,0,0.2); border-radius: 2px; cursor: pointer; z-index: 1002; & > ul {text-align: left; padding: 5px 10px; & > li {padding: 3px 4px Font-size: 12px; list-style: none; height: 24px; line-height: 24px; &: hover {background: # EDF6FF;}} .hover {color: # 888585 Pointer-events: none;}

Third, use in components

Import {mapState} from 'vuex';//... computed: {. MapState ([' contextMenu'])}, created () {bus.$on ('operateDirectory', (param) = > {/ / right-click menu) should trigger a method with the same name in the component. First, we should determine whether the method exists in this component, and call if (thisparam) {this [param] () }});}, / /... methods: {showContextMenu (event, data) {event.preventDefault () / / default event const menuContent for blocking browsers = {menuContent: [{icon: "el-icon-upload2", name: "run", method: "run",}, {icon: "el-icon-refresh" Name: "edit", method: "editConfig",}, {icon: "el-icon-refresh", name: "add", method: "addCommond",}, {icon: "el-icon-refresh" Name: "delete", method: "deleteConfig",},], clientX: event.clientX, clientY: event.clientY, displayContextMenu: true,} This.$store.commit ("SET_CONTEXT_MENU", menuContent); / / Click the page again, and the right-click menu disappears _ document.onclick = (event) = > {this.$store.commit ("SET_CONTEXT_MENU", {displayContextMenu: false,});} }, run () {/ /...}, editConfig () {/ /...}, addCommond () {/ /...}, deleteConfig () {/ /...}} "how to customize the global right-click menu for vue" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report