-
Book Overview & Buying
-
Table Of Contents
Frontend Development Projects with Vue.js 3 - Second Edition
By :
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.
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:
npm init vue@3 as a starting point, or within the root folder of the code repository, navigate into the Chapter01/Exercise1.02 folder by using the following commands in order:> cd Chapter01/Exercise1.02/ > yarn
yarn dev
code . command within the project directory) or your preferred IDE.Exercise1-02.vue in the src/components directory.Exercise1-02.vue component, let’s add data within the <script setup> tags by adding a function called data(), and return a key called title with your heading string as the value:<script>
export default {
data() {
return {
title: 'My first component!',
}
},
}
</script>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.
toUpperCase() method:<template>
<div>
<h1>{{ title.toUpperCase() }}</h1>
</div>
</template>:3000. You should see an output like the following screenshot:
Figure 1.7 – Display of an uppercase title
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
isUppercase to true:<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
<script> with <script setup> and move all the local data declared within the data() function to its own variable names respectively, such as title and isUpperCase, as shown here:<script setup> const title ='My first component!'; const isUppercase = true; </script>
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.