Vue3's Template Syntax: Directives, Interpolation Syntax, and Other Related Features

When developing applications with Vue3, we usually use templates to define the user interface of the application. Vue3's template syntax extends ordinary HTML and adds some special instructions and interpolation syntax to achieve dynamic rendering and interaction of data. This article will introduce Vue3's template syntax in detail, including directives, interpolation syntax, and other related features.

interpolation syntax

The most basic and commonly used template syntax in Vue3 is the interpolation syntax, which is used to dynamically render data to text content or attributes in HTML. The interpolation syntax uses double curly braces ( { { }}) to wrap expressions, for example:

<div>{
   
   { message }}</div>

In the above code, messageit is a data in the Vue3 instance, which will be dynamically rendered into <div>the element.

In addition to simple text interpolation, Vue3 also supports interpolation in HTML attributes, for example:

<img :src="imageUrl">

The above code is used :as v-bindan abbreviation of the instruction, and imageUrlthe value of the data is bound to srcthe attribute to realize dynamic loading of images.

instruction

Directives are special attributes in Vue3 templates, v-beginning with . They are used to operate on HTML elements and implement some complex logic. Vue3 provides a number of built-in instructions, including commonly used v-if, v-for, v-onand , v-modeletc.

  • v-ifDirectives are used to determine whether to render HTML elements based on conditions, for example:

    <div v-if="isShow">条件为真时显示</div>
    
  • v-forDirectives are used to loop through arrays or objects to generate repeated HTML elements, for example:

    <ul>
      <li v-for="item in items" :key="item.id">{
         
         { item.name }}</li>
    </ul>
    
  • v-onDirectives are used to listen to DOM events and execute corresponding methods, for example:

    <button v-on:click="handleClick">点击触发方法</button>
    
  • v-modelDirectives are used to implement two-way binding between form elements and data in a Vue3 instance, for example:

    <input v-model="message" type="text">
    

    The above code messagebinds the data to the text input box two-way.

In addition, Vue3 also supports custom directives to meet specific needs. Custom directives can be used to directly manipulate the DOM, listen to events, and more.

Computed Properties and Listeners

In addition to interpolation syntax and directives, Vue3 also provides computed properties and listeners for handling data logic in views.

Computed properties are new data derived from existing data, and they are used in templates in the same way as normal data. However, unlike ordinary data, computed attributes will cache the results, and directly return the cached computed results when the dependent data has not changed, improving performance.

Listeners are functions that observe and respond to changes in data. When the monitored data changes, the callback function defined in the listener will be executed.

conditional rendering

Vue3 provides a variety of conditional rendering methods, including v-if, v-else-if, v-elseand v-show.

  • v-ifand v-else-ifare used to determine whether to render elements based on conditions.
  • v-elseIt is used to represent the content that needs to be rendered when the previous v-ifor v-else-ifis not satisfied.
  • v-showIt is used to control the display and hiding of elements according to conditions, and displayis realized by modifying the attributes of elements.

list rendering

List rendering is a function often used in Vue3 templates. v-forThe command can loop through arrays or objects and generate repeated HTML elements. In list rendering, we usually need to set a unique value for each item keyso that Vue3 can identify the identity of each item and update it efficiently.

<ul>
  <li v-for="item in items" :key="item.id">{
   
   { item.name }}</li>
</ul>

In the above code, itemsit is an array, v-forwhich is looped through the instruction and generates corresponding <li>elements.

form input binding

The two-way data binding feature of Vue3 makes form processing more concise and flexible. v-modelTwo-way binding between form input elements and data in a Vue3 instance can be easily achieved through the directive.

<input v-model="message" type="text">

The above code performs messagetwo-way binding between the data and the text input box, and any modification to the input box will be updated to messagethe data synchronously.

Summarize

Vue3 template syntax is an important part of realizing view and data binding. It provides rich functions such as interpolation syntax, instructions, computed properties, and listeners, which can help us quickly build interactive and rich user interfaces. Understanding and familiarity with Vue3 template syntax is essential for developing Vue3 applications.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/131772647
Recommended