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

Centos7.5 installs mysql5.7.24 binary package deployment

1. Environmental preparation: Operating system: C...

JS realizes video barrage effect

Use ES6 modular development and observer mode to ...

A colorful cat under Linux

Friends who have used the Linux system must have ...

vue+el-upload realizes dynamic upload of multiple files

vue+el-upload multiple files dynamic upload, for ...

Summary of MySQL database like statement wildcard fuzzy query

MySQL error: Parameter index out of range (1 >...

Implementation code for partial refresh of HTML page

Event response refresh: refresh only when request...

Implementation of Nginx Intranet Standalone Reverse Proxy

Table of contents 1 Nginx Installation 2 Configur...

Detailed explanation of the process of building and running Docker containers

Simply pull the image, create a container and run...

CSS isolation issue in Blazor

1. Environment VS 2019 16.9.0 Preview 1.0 .NET SD...

Ubuntu16.04 builds php5.6 web server environment

Ubuntu 16.04 installs the PHP7.0 environment by d...

MySQL 8.0.12 Simple Installation Tutorial

This article shares the installation tutorial of ...