Data Binding Syntax Using Interpolation
Interpolation is the insertion of something of a different nature into something else. In the Vue.js context, this is where you would use mustache syntax (double curly braces) to define an area where you can inject data into a component's HTMLÂ template.
Consider the following example:
new Vue({ Â Â data() { Â Â Â Â title: 'Vue.js' Â Â }, Â Â template: '<span>Framework: {{ title }}</span>' })
The data property title
is bound to Vue.js reactive data and will update on the fly depending on state changes to the UI and its data. We will go into more depth about how to use interpolation and how to bind it to data properties in the next exercise.
Exercise 1.02: Interpolation with Conditionals
When you want to output data into your template or make elements on a page be reactive, interpolate data into the template by using curly braces. Vue can understand and replace that placeholder with data.
To access the code files for this exercise, refer to https://packt.live/3feLsJ3.
- Open a command-line terminal and navigate into the
Exercise 1.02
folder and run the following commands in order:> cd Exercise1.02/ > code . > yarn > yarn serve
Go to
https://localhost:8080
. - Inside of the
Exercise1-02.vue
component, let's add data within the<script>
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 the data
title
by replacing your<h1>
text with the interpolated value{{ 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 inside curly braces. For example, you can transform your text inside the curly braces using the
toUpperCase()
 method:<template>   <div>     <h1>{{ title.toUpperCase() }}</h1>   </div> </template>
You should see an output like the following screenshot:
- In addition to parsing JavaScript methods, interpolation can 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:
- Add this condition to the curly braces, and when you save you should see the non-uppercased title. Play around with this value by changing
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:
In this exercise, we were able to use inline conditionals inside the interpolated tags (curly braces) by using a Boolean variable. This allows us to modify what data is displayed inside of our component without overly complicated conditions, which can be useful in certain use cases.
We will now learn about how to style components using a variety of methods.