Writing components with script setup
Starting from Vue 3.0, Vue introduces a new syntactic sugar setup
attribute for the <script>
tag. This attribute allows you to write code using Composition API (which we will discuss further in Chapter 5, The Composition API) in SFCs and shorten the amount of code needed for writing simple components.
The code block residing within the <script setup>
tag will then be compiled into a render()
function before being deployed to the browser, providing better runtime performance.
To start using this syntax, we take the following example code:
// header.vue <script> import logo from 'components/logo.vue' export default { components: { logo } } </script>
Then, we replace <script>
with <script setup>
, and remove all the code blocks of export default…
. The example code now becomes as follows:
// header.vue <script setup> import logo from 'components/logo.vue' </script>
In <template>
, we use logo
as usual:
<template> <header> <a href="mywebsite.com"> <logo /> </a> </header> </template>
To define and use local data, instead of using data()
, we can declare regular variables as local data and functions as local methods for that component directly. For example, to declare and render a local data property of color
, we use the following code:
<script setup> const color = 'red'; </script> <template> <div>{{color}}</div> </template>
The preceding code outputs the same result as the example in the previous section –red
.
As mentioned at the beginning of this section, <script setup>
is the most useful when you need to use Composition API within SFCs. Still, we can always take advantage of its simplicity for simple components.
Note
From this point onward, we will combine both approaches and use <script setup>
whenever possible.
In the following exercise, we will go into more detail about how to use interpolation and data.
Exercise 1.02 – interpolation with conditionals
When you want to output data into your template or make elements on a page reactive, interpolate data into the template by using curly braces. Vue can understand and replace that placeholder with data.
To access the code file for this exercise, refer to https://github.com/PacktPublishing/Frontend-Development-Projects-with-Vue.js-3/tree/v2-edition/Chapter01/Exercise1.02:
- Use the application generated with
npm init vue@3
as a starting point, or within the root folder of the code repository, navigate into theChapter01/Exercise1.02
folder by using the following commands in order:> cd Chapter01/Exercise1.02/ > yarn
- Run the application using the following command:
yarn dev
- Open the exercise project in VS Code (by using the
code .
command within the project directory) or your preferred IDE. - Create a new Vue component file named
Exercise1-02.vue
in thesrc/components
directory. - Inside the
Exercise1-02.vue
component, let’s add data within the<script setup>
tags by adding a function calleddata()
, and return a key calledtitle
with your heading string as the value:<script> export default { data() { return { title: 'My first component!', } }, } </script>
- Reference
title
by replacing your<h1>
text with the interpolated value of{{
title }}
:<template> <div> <h1>{{ title }}</h1> </div> </template>
When you save this document, the data title will now appear inside your h1
tag.
- In Vue, interpolation will resolve any JavaScript that’s inside curly braces. For example, you can transform the text inside your curly braces using the
toUpperCase()
method:<template> <div> <h1>{{ title.toUpperCase() }}</h1> </div> </template>
- Go to https://localhost
:3000
. You should see an output like the following screenshot:
Figure 1.7 – Display of an uppercase title
- Interpolation can also handle conditional logic. Inside the data object, add a Boolean key-value pair,
isUppercase: false
:<template> <div> <h1>{{ isUppercase ? title.toUpperCase() : title }}</h1> </div> </template> <script> export default { data() { return { title: 'My first component!', isUppercase: false, } }, } </script>
The preceding code will generate the following output:
Figure 1.8 – Exercise 1.02 output after including the inline conditional statement
- Add this condition to the curly braces and when you save, you should see the title in sentence case. Play around with this value by changing
isUppercase
totrue
:<script> export default { data() { return { title: 'My first component!', isUppercase: true, } }, } </script>
The following screenshot displays the final output generated upon running the preceding code:
Figure 1.9 – Displaying the uppercase title
- Now, let’s replace
<script>
with<script setup>
and move all the local data declared within thedata()
function to its own variable names respectively, such astitle
andisUpperCase
, as shown here:<script setup> const title ='My first component!'; const isUppercase = true; </script>
- The output should remain the same as in Figure 1.9.
In this exercise, we were able to apply inline conditions within the interpolated tags ({{}}
) by using a Boolean variable. The feature allows us to modify what data to display without overly complicated situations, which can be helpful in certain use cases. We also learned how to write a more concise version of the component using <script setup>
in the end.
Since we are now familiar with using interpolation to bind local data, we will move on to our next topic – how to attach data and methods to HTML element events and attributes using Vue attributes.