Vue.js 3.0 Tutorial

Vue.js 3.0 Component Advanced

Vue.js 3.0 Transitions & Animations

Vue.js 3.0 Reusable & Combinable

Vue.js 3.0 Advanced

Vue.js 3.0 Tools

Vue.js 3.0 Scale

Vue.js 3.0 Accessibility

Vue.js 3.0 Migrating from Vue2

Vue.js 3.0 Contribute Documentation

Vue.js 3.0 API References

Vue.js 3.0 Style Guide

Vue 3.0 custom directive

Custom directives in Vue are a way to reuse code that manipulates the DOM. Vue 3.0 uses a slightly different syntax for custom directives than Vue 2, so be sure you're looking at the correct version of the documentation.

Let's create a custom directive that changes the background color of an element.

Step 1: Create a Vue project.

If you haven't created your Vue 3 project yet, you can do so using the Vue CLI.

vue create my-project

Choose Vue 3 when asked which version of Vue to use.

Step 2: In your main.js (or main.ts if using TypeScript) file, you can define a global custom directive like this:

import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)

app.directive('highlight', {
  beforeMount(el, binding, vnode, prevVnode) {
    el.style.backgroundColor = binding.value;
  }
})

app.mount('#app')

This creates a directive named highlight. This directive changes the background color of the HTML element it is used on, to the color provided as its value. The beforeMount hook is called right before the directive's element is mounted to the DOM.

Step 3: Use your directive in a component.

You can then use your custom directive in any of your Vue components. Here's how you might use it in your App.vue file:

<template>
  <div v-highlight="'lightgreen'">
    This should have a light green background.
  </div>
</template>

<script>
export default {
  name: 'App',
};
</script>

This will apply the highlight directive to the div, resulting in the div having a light green background.

Step 4: Run your application.

You can run your Vue 3 application using the following command:

npm run serve

And there you go! You've created and used a custom directive in Vue 3.0. This is a fairly simple example, but you can create more complex directives as needed by your application.

  1. Creating Custom Directives in Vue 3.0:

    • Description: Custom directives in Vue 3.0 allow you to extend the behavior of HTML elements.
    • Code Example:
      // customDirective.js
      export const customDirective = {
        mounted(el, binding) {
          // Directive logic when element is mounted
          el.style.color = binding.value;
        },
      };
      
  2. Registering Custom Directives in Vue 3.0:

    • Description: Register custom directives globally or locally using the app.directive method.
    • Code Example:
      // main.js
      import { createApp } from 'vue';
      import { customDirective } from './customDirective';
      
      const app = createApp(App);
      app.directive('my-directive', customDirective);
      
  3. Vue 3.0 Custom Directive Example:

    • Description: Use a custom directive in a component's template.
    • Code Example:
      <!-- MyComponent.vue -->
      <template>
        <div v-my-directive="'red'">Colored Text</div>
      </template>
      
      <script>
      export default {
        // Component logic
      };
      </script>
      
  4. Modifiers for Custom Directives in Vue 3.0:

    • Description: Modifiers can be applied to custom directives to enhance their behavior.
    • Code Example:
      <!-- MyComponent.vue -->
      <template>
        <input v-my-directive.trim="someValue">
      </template>
      
  5. Vue 3.0 Directive Arguments:

    • Description: Pass arguments to custom directives for dynamic behavior.
    • Code Example:
      <!-- MyComponent.vue -->
      <template>
        <div v-my-directive:argName="value">Dynamic Argument</div>
      </template>
      
  6. Scoped Custom Directives in Vue 3.0:

    • Description: Apply directives to specific elements using the v-my-directive syntax.
    • Code Example:
      <!-- MyComponent.vue -->
      <template>
        <div v-my-directive="value">Scoped Directive</div>
      </template>
      
  7. Dynamic Custom Directives in Vue 3.0:

    • Description: Dynamically apply or remove directives based on component logic.
    • Code Example:
      <!-- MyComponent.vue -->
      <template>
        <div v-my-directive="condition">Dynamic Directive</div>
      </template>
      
  8. Vue 3.0 Custom Directive and Reactivity:

    • Description: Utilize reactivity in custom directives to respond to changes.
    • Code Example:
      // customDirective.js
      export const customDirective = {
        updated(el, binding) {
          el.innerHTML = binding.value;
        },
      };