SoFunction
Updated on 2025-04-05

Vue3 uses custom instructions to implement content control

Preface

When developing complex front-end applications, you often encounter situations where you need to display or hide certain content on the page based on the user's permissions. As a progressive JavaScript framework, Vue3 provides powerful custom instruction functions, making permission control both simple and efficient. This article will introduce in detail how to use custom instructions in Vue3 to determine whether the content is displayed to meet the interface display needs under different user permissions.

Basic preparation

First, make sure you already have a Vue3 project running.

I will start with the most basic custom instructions and gradually deepen into the implementation of permission control.

First, make sure you already have a Vue3 project running.

I will start with the most basic custom instructions and gradually deepen into the implementation of permission control.

User permission list:["model:info:add","model:info:delete"], which is a common permission representation method that contains a permission description for model operations.

Create custom directives

Vue3 allows us to pass the application instancedirectiveMethod or componentdirectivesOption to register custom directives. For global use, we willOr similar entry file to register a name calledv-permissioncustom directives.

import { createApp } from 'vue'
import App from './'
const app = createApp(App)
('permission', {mounted(el, binding) {
// Called when the directive is bound to an element for the first time// This will fill in the permission judgment logic  }
})
('#app')

Implement permission judgment logic

Next, we need to implement permission judgment logic in custom instructions. Suppose we already have a global state management or similar mechanism to store the permission list of the current user. Here it is simplified from a simulated oneImport into the file.

 // 
export const userPermissions = ["model:info:add","model:info:delete"];

Use these permissions in custom directives:

import { createApp } from "vue";
import App from "./";
import { userPermissions } from "./auth";
const app = createApp(App);
("permission", {
  mounted(el, binding) {
    const { value } = binding;
    if (value && !(value)) {
      (el);
    }
  },
});
("#app");

In the above code, we first bind the value from () Obtained the permission identifier that needs to be checked. Then, check whether this identifier is included in the user's permission list (userPermissions) to decide whether to remove the corresponding element.

Use custom directives

Now, we have completed the custom commandv-permissionBasic implementation. Next, it is very simple to use it in the Vue component to control whether the content is displayed.

Suppose there is a button that only users with the permission to add model ("model:model:add") can see:

<template>
  <button v-permission="'model:info:add'"Add a model</button
</template>

If the current user does not have the permission "model:model:add", the button will not be rendered into the DOM.

Extended custom directives

Although the above implementations can meet basic needs, it may also be necessary to deal with various situations more flexibly in practical applications. For example, we might want to hide rather than remove elements when there is no permission, or provide a callback function to execute when the check fails.

These can all be achieved by extending custom directives. For example, modify the directive to support more options:

("permission", {
  mounted(el, binding) {
    const { value, arg, modifiers } = binding;
    let hasPermission = true;
    if (typeof value === "string") {
      hasPermission = (value);
    } else if (typeof value === "function") {
      hasPermission = value(userPermissions);
    }
    if (!hasPermission) {
      if () {
        (el);
      } else {
         = "none";
      }
    }
  },
});

The above code adds support for function-form binding values ​​and modifiers. This allows more flexible control of the display and hiding strategies of elements.

in conclusion

Using Vue3's custom instruction function for permission management can not only improve the readability and maintainability of the code, but also flexibly adapt to various complex scenarios. Through the methods introduced in this article, you can easily implement a function that controls content display based on permissions. As project requirements continue to change and upgrade, you can further expand and optimize custom instructions to make them more powerful and easy to use.

The above is the detailed content of Vue3's implementation method of using custom instructions for content control. For more information about Vue3's content control, please pay attention to my other related articles!