Background requirements: The ERP system needs to add a "button permission control" function, and the control granularity of permissions should be extended to the button level. expected There are two interactive modes for button permission control: "invisible" and "visible but not clickable". Invisible The invisible interaction method is relatively simple. We can use v-if to control whether it is displayed. You can use v-show, but it is not safe enough. After all, v-show only changes the style to display: none, and it still exists in the real DOM rendering, so it is recommended to use v-if to control the invisible. Visible but not clickable"You can see, but you can't."
The final product requirement chose "visible but not clickable", probably because invisible was considered too simple. (¬_¬) Idea Exploration
Practice plan Finally, we chose the instruction method to expand at the lowest cost and avoid changing the existing business code logic.
Please see below for the specific implementation plan: Permission entry: Vuex control, global use//After the user logs in, get the user's permission CODE code and store it in the store this.$store.commit('SET_AUTH_CODE', authCodeList); SET_AUTH_CODE: (state, acthCode) => { if (acthCode) { state.autoCodeList = acthCode; } setStore({ name: 'autoCodeList', content: state.autoCodeList || [], }); } Defining permission instructionsconst disableClickFn = (event) => { event && event.stopImmediatePropagation(); } export const hasPermission = () => { Vue.directive('permission', { bind(el, binding) { let disable = true; if (autoCodeList.length && autoCodeList.includes(binding.value)) { disable = false; } if (disable) { el.classList.add('permission-disabled'); el.setAttribute('disabled', 'disabled'); el.addEventListener('click', disableClickFn, true); } }, unbind(el) { el.removeEventListener('click', disableClickFn); } }); };
If multiple event listeners are attached to the same event type on the same element, they will be called in the order in which they were added when the event is triggered. If you call stopImmediatePropagation() in one of the event listeners, the remaining event listeners will not be called. MSDN - stopImmediatePropagation Add disabled CSS styles.permission-disabled { position: relative; cursor: not-allowed !important; pointer-events: none; // Prevent elements from receiving mouse events border:none; background-image: none; &::after { content: ''; position: absolute; bottom: 0; left: 0px; right: 0px; height: 100%; z-index: 9; background: rgba(255, 255, 255, 0.5); } } A relatively unfamiliar CSS property, pointer-events, is used here. The CSS3 pointer-events property specifies under what circumstances (if any) a particular graphic element can become the target of a mouse event. For more usage reference: MSDN - pointer-events The use of pointer-events here is just an auxiliary function. It does not necessarily mean that the event listener on the element will never be triggered. If the descendant element has specified pointer-events and allows to become the event target, the parent element event can be triggered. And relying solely on CSS properties to control not clicking is still risky, so it is only used for auxiliary purposes here. Global "permission judgment" tool functionimport { getStore, } from '@/util/store'; const autoCodeList = getStore({ name: 'autoCodeList', }) || []; export function hasPermission(authName) { return !(autoCodeList.length > 0 && autoCodeList.includes(authName)); } Specific use// Command method (oms/order/save here corresponds to the CODE permission code when the user logs in) <el-button v-permission="'oms:order:save'">Save</el-button> // Function method <el-button :disabled="hasPermission('oms:order:save')"></el-button> This is the end of this article about the implementation of Vue-based Element button permissions. For more relevant Element button permissions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Docker uses busybox to create a base image
1. Background I recently made a website, uidea, w...
Install fastdfs on Docker Mount directory -v /e/f...
DTD is a set of grammatical rules for markup. It i...
Next, I will install Java+Tomcat on Centos7. Ther...
1. What is master-slave replication? Master-slave...
Preface MySQL officially refers to prepare, execu...
Nginx (engine x) is a high-performance HTTP and r...
In actual projects, the up and down scroll bars a...
When it comes to pictures, the first thing we thi...
Installation Steps 1. Install Redis Download the ...
But recently I found that using this method will c...
1. Download First of all, I would like to recomme...
I have searched various major websites and tested...
[LeetCode] 185. Department Top Three Salaries The...
Method 1: Download Pycharm and install Download a...