-
Book Overview & Buying
-
Table Of Contents
Front-End Development Projects with Vue.js
By :
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.
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.
Exercise 1.02 folder and run the following commands in order:> cd Exercise1.02/ > code . > yarn > yarn serve
Go to https://localhost:8080.
Exercise1-02.vue component, let's add data within the <script> 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 {{ 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>You should see an output like the following screenshot:

Figure 1.7: Save the file—you should now have an uppercased 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: Final Exercise 1.02 output
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.
Change the font size
Change margin width
Change background colour