Vue button permission control introduction

Vue button permission control introduction

Preface:

In daily projects, you may need to determine the current user's operating permissions based on the data returned by the background interface. The delete button must be displayed when there is delete permission. Without this permission, the button will not be displayed or deleted. By searching for information, this function is realized through vuex.

1. Steps

1. Define buttom permissions

Create buttomPermission in state to save the permission data returned by the background interface.

setPermission is used to accept data and pass the page permission management into the buttomPermission object.

Using vuex:

Vue.use(Vuex)

Create a vue instance const store = new Vuex.Store({
    state: {
        buttomPermission: {}
    },
    mutations:
        setPermission(state, permission) {
            state.buttomPermission = permission
        }
    }
})
export default store


2. Define the store

import store from './store/index.js'

new Vue({
    store,
    el: '#app',
    render: h => h(App)
})


3. Create permission instructions

Create a new directives folder and create a permission.js file.

The inserted function is used here to check whether the element has permission when the bound element is inserted into the parent node.

inserted( el, bindings, vnode ) { }


4. Use the permission directive

Introduce and define the permission directive on the button page, write the directive in buttom , and bind the corresponding value in the directive.

 <button v-permission="'add'">Add</button>
import permission from './directives/permission'
directives: {permission,},


5. Delete unauthorized data

In the permission instruction, get value value of the button binding through bindings , then find it in the buttomPermission object, and then determine whether there is permission. If there is no permission, delete the node.

inserted(el, bindings, vnode) {
        let btnPermissionValue = bindings.value;
        let boolean =vnode.context.$store.state.buttomPermission[btnPermissionValue];
        !boolean && el.parentNode.removeChild(el);
    }


6. Passing in state management data

Pass the state management data to the permission management through the setPermission method

let permissions = {}
this.$store.commit("setPermission", permissions);


II. Overview

In general, a buttomPermission permission status object is defined through vuex , and then a permissions directive is created. The permissions directive is used for each buttom button and the value of the specific meaning of the button is bound. Then in the permission.js file, get the current value and get whether the current button has permission from buttomPermission . If not, delete the current button node directly.

This is the end of this article about vue button permission control. For more relevant vue button permission control content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of Vue dynamic routing and Springsecurity button-level permission control
  • Detailed explanation of VUE front-end button permission control
  • Detailed explanation of the idea of ​​implementing button-level permission control based on Vue custom instructions

<<:  Comprehensive understanding of HTML Form elements

>>:  Flame animation implemented with CSS3

Recommend

A detailed analysis and processing of MySQL alarms

Recently, a service has an alarm, which has made ...

A detailed introduction to the basics of Linux scripting

Table of contents 1. Script vim environment 2. Ho...

Introduction to container of() function in Linux kernel programming

Preface In Linux kernel programming, you will oft...

Linux kernel device driver advanced character device driver notes

/****************** * Advanced character device d...

How does MySQL connect to the corresponding client process?

question For a given MySQL connection, how can we...

Access the MySQL database by entering the DOS window through cmd under Windows

1. Press win + R and type cmd to enter the DOS wi...

iframe multi-layer nesting, unlimited nesting, highly adaptive solution

There are three pages A, B, and C. Page A contains...

MySQL5.7.21 decompressed version installation detailed tutorial diagram

Since I often install the system, I have to reins...