What are directives in Vue template syntax?

First of all, a directive is a special attribute that starts with "v-" and is used to add some behavior in a Vue template. They work like magic and make your template come alive.

OK, let's get started!

v-if directive
The v-if directive is one of the most commonly used directives in Vue. It can add or remove elements based on the value of an expression.

For example, if you have a variable showMessage, you can use the v-if directive like this:

<div v-if="showMessage">Hello World!</div>

When showMessage is true,

The element will be displayed; when showMessage is false, it will be removed from the DOM.

v-else directive
The v-else directive is used with the v-if directive to add an "else" block.

For example:

<div v-if="showMessage">Hello World!</div>  
<div v-else>Goodbye World!</div>

When showMessage is true, the first

element will be displayed; when showMessage is false, the second
The element will be displayed.

v-for directive
The v-for directive is used to loop through an array or object. It can be used like this:

<ul>  
  <li v-for="(item, index) in items" :key="index">  
    {
   
   { item }}  
  </li>  
</ul>

In this example, we loop through each element in the items array and render them as an unordered list. :key="index" is required and is used to help Vue track the identity of each element.

v-bind directive
The v-bind directive is used to dynamically bind properties to the data of a Vue instance. You can use it to bind any attribute, such as class, style, href, etc.

For example:

<img v-bind:src="imageUrl" />

In this example, we bind the element's src attribute to the imageUrl data in the Vue instance. This way, when the value of imageUrl changes, the source of the image is updated accordingly.

The v-on directive
The v-on directive is used to add event listeners to elements. You can use it to call methods or expressions in the Vue instance.

For example:

<button v-on:click="handleClick">Click me!</button>

In this example, we added a click event listener to the button, and when the button is clicked, it will call the handleClick method in the Vue instance. You can also use the short form @click instead of v-on:click.

v-model directive
The v-model directive is used to create a two-way binding between a form element and the data of a Vue instance. You can use it to create interfaces for form input.

For example:

<input type="text" v-model="message" /> <p>{
   
   { message }}</p>

In this example, we create a text input box and a paragraph element. When the user enters text in the input box, the message data in the Vue instance will automatically update, and the paragraph element will update accordingly. This is a very convenient way to handle form input.

v-show directive
The v-show directive is used to toggle the display or hiding of elements based on the value of an expression. Unlike the v-if directive, v-show will simply toggle the element's CSS attribute display.

For example:

<p v-show="isShow">Hello World!</p>

When isShow is true, <p>the element will be shown; when isShow is false, it will be hidden.

v-pre directive
The v-pre directive is used to skip all compilation of an element or its sub-elements. It can be used to debug template structures during development.

For example:

<div v-pre>  
  <p>Hello World!</p>  
  {
   
   { message }}  
</div>

In this example, neither <div>the element nor its children <p>will be compiled by the Vue compiler. If you try to add any Vue directives or expressions inside <p>the element or { { message }}, they will not be processed.

v-once directive
The v-once directive is used to compile an element or its sub-elements once and not recompile them during subsequent rendering processes. It can be used to improve performance, especially when the template contains a lot of static content.

For example:

<div v-once>  
  <p>Hello World!</p>  
  {
   
   { message }}  
</div>

In this example, <div>both the element and its children <p>will be compiled only once. Even if the value of the message changes, they will not be recompiled.

v-bind:class directive
The v-bind:class directive is used to dynamically bind one or more class names to the data of a Vue instance. It accepts an object as a parameter. The attribute name of the object is the name of the class to be bound. The value of the attribute is a Boolean expression indicating whether to apply this class.

For example:

<div v-bind:class="{ active: isActive, error: hasError }">Hello World!</div>

In this example, when isActive is true, the active class will be applied; when hasError is true, the error class will be applied. You can bind multiple class names to a directive object.

v-bind:style directive
The v-bind:style directive is used to dynamically bind one or more styles to an element. It accepts an object as a parameter. The property name of the object is the name of the style property to be bound, and the value of the property is the value of the style.

For example:

<div v-bind:style="{ color: textColor, fontSize: fontSize + 'px' }">Hello World!</div>

In this example, the value of textColor will be bound to the element as the value of the color style attribute, and the value of fontSize will be bound to the element as the value of the fontSize style attribute. You can bind multiple styles to a directive object.

Guess you like

Origin blog.csdn.net/2301_77795034/article/details/131154239
Recommended