DEV Community

Cover image for Mastering Vue 3: A Comprehensive Guide to Building Modern Web Applications < Part 3 />
Hany Taha
Hany Taha

Posted on • Edited on

Mastering Vue 3: A Comprehensive Guide to Building Modern Web Applications < Part 3 />

Content:

  1. Mastering Vue 3 - Part 1 [Introduction and key features]

2. Mastering Vue 3 - Part 2 [Benefits of using Vue 3 for web development]

3. Mastering Vue 3 - Part 3 [Template syntax in Vue 3]

4. Mastering Vue 3 - Part 4 [Reactivity Fundamentals]

5. Mastering Vue 3 - Part 5 [Class and Style Bindings]

6. Mastering Vue 3 - Part 6 [Lifecycle Hooks]

7. Mastering Vue 3 - Part 7 [Understanding components in Vue 3
]

8. Mastering Vue 3 - Part 8 [Installing Vue project and file structure]

9. Mastering Vue 3 - Part 9 [Vue Router in Vue 3]

10. Mastering Vue 3 - Part 10 [Animation in Vue 3]

11. Mastering Vue 3 - Part 11 [State Management with Pinia]

12. Mastering Vue 3 - Part 12 [Teleport in Vue 3]

13. Mastering Vue 3 - Part 13 [Working with API Calls ]

14. Mastering Vue 3 - Part 14 [Forms and Form Validation ]


Template syntax in Vue 3

In Vue 3, the template syntax is used to define the structure and rendering logic of a component's HTML markup. It provides a declarative way to describe how the component should look based on its data and state.
The template syntax in Vue 3 is similar to the previous versions of Vue, but with a few changes and enhancements. Here are some key aspects of the template syntax in Vue 3:

Interpolation
Interpolation allows you to insert dynamic values into the template. It uses double curly braces {{ }} to bind the data to the template.

<template>
    <div>
      <p>{{ message }}</p>
      <p>{{ count }}</p>
    </div>
  </template>

  <script>
  export default {
      data() {
          return {
              message: 'hello world',
              count: 5
          };
      }
  };
  </script>
Enter fullscreen mode Exit fullscreen mode

In this example, the message and count variables are dynamically inserted into the paragraphs.
Note: variables must be defined in the data function.


Directives
Directives are special attributes starting with the v- prefix that provide additional functionality to elements or components. They are used to bind data, control rendering, handle events, and more.

<template>
  <div>
    <p v-if="showMessage">{{ message }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

In this example, the v-if directive conditionally renders the paragraph based on the value of showMessage, and the @click directive attaches an event listener to the button that triggers the increment method.


Conditionals
Vue 3 provides conditional rendering directives to show or hide elements based on certain conditions. The most commonly used directives for conditionals are v-if, v-else, v-else-if, and v-show.

<template>
  <div>
    <p v-if="showMessage">Message is shown</p>
    <p v-else-if="count > 10">Count is greater than 10</p>
    <p v-else>Default message</p>
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

In this example, the paragraph elements are conditionally rendered based on different conditions.


List Rendering
Vue 3 provides the v-for directive for rendering lists or iterating over arrays or objects.

<template>
  <div>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

In this example, the v-for directive iterates over the items array and renders an <li> element for each item.


Event Handling
Vue 3 allows you to handle user-interactions and events using the v-on directive or the shorthand @ symbol.

<template>
  <div>
    <button @click="increment">Increment</button>
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

In this example, the @click directive attaches an event listener to the button, triggering the increment method when clicked.

These are some examples of the template syntax in Vue 3. The template syntax provides a convenient way to define the structure and behavior of the component's HTML markup, making it easy to build dynamic and reactive user interfaces.

Top comments (0)