vuejs-sintaxis-basica

Basic Syntax of Vue.js

  • 4 min

In this tutorial, we are going to see the basic syntax of Vue.js. Something that, logically, we will use constantly throughout the rest of the course.

Vue.js syntax is based on combining HTML, CSS, and JavaScript in a declarative and reactive way. Undoubtedly, this is one of the reasons for Vue.js’s success.

It basically focuses on letting us write code easily while Vue.js takes care of the “heavy” part (reactivity and DOM updates).

But, at the same time, it also follows principles similar to other modern frameworks. So it’s easy to get the hang of it if you’ve seen any of them.

So let’s see the fundamentals of Vue.js syntax 👇.

Vue.js is Component-Based

Like many other technologies, Vue.js is component-based. They are the 🧱 “building blocks” 🧱 with which most modern web applications are built (and non-web ones, and not-so-modern ones 😅).

As such, a component is a reusable block of code that can have its own state, properties, methods, and lifecycle.

In general, they almost always correspond to a UI element, like a button, a list, a card, etc.

Components allow us to organize and structure our application in a modular way, which facilitates development and maintenance of the application, and makes it smell better (this last point is not 100% verified).

Structure of a Vue.js Component

A component in Vue.js is simply a file with the extension .vue. The syntax contains three main sections.

ElementDescription
<template>Defines the HTML structure of the component.
<script>Contains the JavaScript logic of the component.
<style>Defines the CSS styles of the component.

For example, like this:

<template>
  <div>
    <h1>{{ message }}</h1>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const message = ref('Hello, Vue.js 3!');
</script>

<style scoped>
h1 {
  color: blue;
}
</style>
Copied!

Using <template> to Group Elements

In Vue.js, <template> is a special tag that allows grouping multiple elements without the need for a root container.

That is, it’s a “fictitious” grouping, as if we had a <div>. But with the difference that the <template> tag will not appear in the HTML5 code.

<template>
  <h1>{{ title }}</h1>
  <p>{{ description }}</p>
</template>

<script setup>
import { ref } from 'vue';

const title = ref('Welcome to Vue.js 3');
const description = ref('This is an example of multiple elements in <template>.');
</script>
Copied!

In this example, the <template> contains two elements (h1 and p) without needing a container.

We have seen that in a component’s syntax, <template> is used. This is because, under the hood, a component can have only a single HTML node.

But it’s not the only place where we will see <template> used. It will be necessary in other cases where, similarly, we need to group HTML elements.

String Interpolation

String interpolation is a way to display dynamic data within HTML. For this, the double curly brace syntax {{ }} is used.

Vue.js will automatically replace the expressions inside {{ }} with their corresponding values.

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

<script setup>
import { ref } from 'vue';

const message = ref('Hello, Vue.js 3!');
</script>
Copied!

In this example:

  • {{ message }} displays the value of the message variable.
  • {{ 2 + 2 }} displays the result of the expression 4.

Expressions in Interpolations

In addition to displaying simple variables, we can also use JavaScript expressions inside the interpolation braces {{ }}.

These expressions can include mathematical operations, method calls, ternary operators, concatenations, etc.

<template>
  <div>
    <p>{{ message.toUpperCase() }}</p>
	<p>The sum of 2 + 2 is {{ 2 + 2 }}</p>
    <p>{{ isActive ? 'Active' : 'Inactive' }}</p>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const message = ref('Hello, Vue.js 3!');
const isActive = ref(true);
</script>
Copied!

In this example:

  • {{ message.toUpperCase() }} converts the message to uppercase.
  • {{ isActive ? 'Active' : 'Inactive' }} uses a ternary operator to display conditional text.

Although it is possible to use complex expressions in interpolation, it is not recommended to overuse them. In these cases, it is better to use methods or computed properties.

Basic Directives

Directives are special attributes we add to HTML that Vue.js uses to provide additional functionality.

Directives in Vue.js always start with v- and are used to manipulate the DOM declaratively. They also will not appear in the generated HTML code.