Data Properties (Props)
One of the most used terms and reactive elements used when constructing Vue components is data property. These manifest themselves within the data function of a Vue instance:
<template> Â Â Â Â <div>{{color}}</div> </template> <script> Â Â Â Â export default { Â Â Â Â Â Â Â Â data() { Â Â Â Â Â Â Â Â Â Â return { Â Â Â Â Â Â Â Â Â Â Â Â color: 'red' Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â } Â Â Â Â } </script>
You can use data property to essentially store any information you want to use within your Vue templates. When this data property is updated or is changed, it will reactively update in the corresponding template.
Exercise 1.01: Building Your First Component
In this exercise, we are going to build our first component inside of a Vue project. In this context, components are imported using ES6. We will require Node.js and yarn
to be installed. These will be covered in the Preface. By the end of the exercise, you will be able to confidently create new Vue components using Vetur and import them into your project.
To access the code files for this exercise, refer to https://packt.live/35Lhycl.
- Open a command-line terminal and navigate into the
Exercise 1.01
folder and run the following commands in order:> cd Exercise1.01/ > code . > yarn > yarn serve
Go to
https://localhost:8080
.Note
Your app will hot reload when you save new changes, so you can see them instantly.
- In VSCode (which will have opened when you ran the
code .
command), go into thesrc/App.vue
directory and delete everything in that file and save. - In your browser, everything should be blank a clean slate to start working from.
- The three primary components that make up a single-file component are the
<template>
,<script>
, and<style>
blocks. If you installed the Vetur extension from the Preface, writevue
and press Tab to choose the first selection of the prompt. This is the quickest way to set up your default code blocks as displayed in the following screenshot:The following is the code generated after pressing Tab when using Vetur:
// src/App.vue <template> </template> <script> export default { } </script> <style> </style>
- Create another file in the
components
folder calledExercise1-01.vue
and repeat the same step for scaffolding the Vue blocks using Vetur:// src/components/Exercise1-01.vue <template> </template> <script> export default { } </script> <style> </style>
- Within our
Exercise1-01.vue
component, compose a set of<div>
tags, with an<h1>
element and a heading inside the<template>
tags:<template> Â Â <div> Â Â Â Â <h1>My first component!</h1> Â Â </div> </template>
- Inside the
<style>
block, add some styling as follows:<template> Â Â <div> Â Â Â Â <h1>My first component!</h1> Â Â </div> </template> <style> Â Â h1 { Â Â Â Â font-family: 'Avenir', Helvetica, Arial, sans-serif; Â Â Â Â text-align: center; Â Â Â Â color: #2c3e50; Â Â Â Â margin-top: 60px; Â Â } </style>
- Import our component into the
App.vue
by using the ES6import
method and defining the component inside thecomponents
object in the<script>
block. We can now reference this component inside the HTML by using its name incamelCase
orkebab-case
(both will work):<template> Â Â <Exercise /> </template> <script> import Exercise from './components/Exercise1-01' export default { Â Â components: { Â Â Â Â Exercise, Â Â } } </script>
When you press
Save
,https://localhost:8080
should reload and display the following output:
In this exercise, we saw how to structure Vue components using template tags, scaffold basic Vue components using Vetur
, output HTML, and use ES6 syntax to import the Exercise1-01
component into App.vue
.
Note
You can only have one root HTML element inside <template>
tags. Complex components should be wrapped in a containing HTML tag of your choice. <div>
, <article>
, and <section>
are all semantic HTML component wrappers.