Sales

ما هو التوجيه المخصص في Vue.JS وكيف يتم إنشاؤه؟

Emir Eskici

Emir Eskici

PlusClouds Author

Cloud & SaaS

What is Vue.JS Custom Directive and How to Create It

What is Vue.js Custom Directive and How to Create It

Directives are code snippets that allow selected (DOM) or components or elements inserted to act according to their definition, written with the syntax rule v-directiveName (syntax). There are certain built-in directives within Vue.js. With these predefined capable directives, various processes are carried out much more practically. Let's say these directives do not meet our needs, so what will we do? There is also a practical solution to that with Vue.js Custom Directive.

If the directives provided by Vue.js are insufficient or do not meet our requirements, Vue.js allows us to create our own directives. These are called custom directives. Let's explore together how to create a custom directive in Vue.js.

When creating any custom directive, we need to define this directive globally in main.js to call and use it everywhere. The definition looks like this:

Description

Five different methods are needed for the directive to function, and these are called Directive Hooks. Directive Hooks are methods that are triggered in the lifecycle of the directive, and the most commonly used one is the bind (el, binding) method seen in the example above, which holds some parameters; now let's take a look at the parameters that this method takes.

el: This parameter allows us to access the element where the directive is used. (intervention to the DOM)
binding: This parameter holds and shows us which argument the specified directive takes and what type of data is being sent. (properties)

How Can We Send Data to the Directive We Created?

We may want to send any data to the directive that we defined in main.js within our app.vue file; after all, we would not want to use a directive that only has a single value constantly. So, how will we do this?

Description

How Will We Use It?

The answer is very simple. There is no difference from the directives normally used. You can examine below...

Description

Result

Description

Thank you for reading, see you in our next blog with new information.

We will be sharing a great application next week, stay tuned

#There is no text provided for translation. Please provide the text you would like to be translated into English.

Frequently Asked Questions

What is a Vue.js custom directive and when should I use one?

A Vue.js custom directive is a user-created directive that you can use when the built-in directives do not meet your requirements. They are called custom directives, and when creating one you define it globally in main.js to call and use it everywhere.

How do I register a Vue.js custom directive globally in Vue.js?

Custom directives must be defined globally in main.js to call and use it everywhere. This is how you enable the directive across the app.

What are Directive Hooks in Vue.js and which one is most commonly used?

Five different methods are needed for the directive to function, and these are called Directive Hooks. The most commonly used one is the bind (el, binding) method.

What does the el parameter do in a Vue.js directive?

This parameter allows us to access the element where the directive is used. It provides intervention to the DOM.

What does the binding parameter contain in a Vue.js directive?

This parameter holds and shows us which argument the specified directive takes and what type of data is being sent. (properties)

How can we send data to the Vue.js custom directive we created?

We may want to send any data to the directive that we defined in main.js within our app.vue file. The binding parameter shows what data is sent.

Is using a custom directive different from using built-in directives in Vue.js?

There is no difference from the directives normally used. This means the concept is consistent with how built-in directives are used.