2orerz024 Single Fle Components - Real World Vus 3 | Vue Mastery
Single File Components
Now that we've created our project with the Vue CLI, we're ready to start customizing it to build our own
app.
If you're coding along (which | encourage you to do) you'll want to checkout the L3-start branch of
our project repo to grab the starting code (L3 stands for Lesson 3). In that code, | want to bring your
attention to this file I've added:
Eprettierrc.js
module.exports = {
singleQuote: true,
semi: false
Here, I've set up some rules so that Prettier will change any double quotes (" ) to single ones ( ‘ ) and
remove any semicolons ( ; ). ’m not advocating for or against semicolons and double quotes. This is a
simple example of how you might add some Prettier configuration rules to your project. We could do
something similar for ESLint as well. For a more in-depth look at how you can configure ESLint + Prettier
as well as get the most out of VS Code, you can check out this article.
What are these .vue files?
In order to start building our app, we need to get some foundational understanding of how things are
working within the demo app the CLI created for us, including the views directory, which includes two
Home.vue and About.vue
single-file vue fi
‘These are the components that Vue Router loads up when we navigate to the Home and About routes,
respectively.
hitps www: vwemastery.comicoursestreal-warl-vueSsingle-le-components sn2orer2021 Single Fle Components - Real World Vus 3 | Vue Mastery
BMA TNC icig
AeA
In the next lesson, we'll explore the essentials of Vue Router, but for now you just need to understand that
these “view” components are the different views that can be seen (or navigated to) within our app. They
can contain child components that are nested within them, and their children will be displayed in that
view as well. For example, the Home.vue component has a child: HelloWorld.vue, which has a bunch of
template code that is being displayed when we're on the Home route.
Each of these .vue files are single file components, and that's what this lesson is exploring: How are single
file components composed, and how do you use them to create a Vue app?
Anatomy of a Single File Component
When we're talking about Vue apps, we're really talking about a collection of Vue components.
hitps dwn: vwemastery.comfcoursestrea-word-vueiinglefle-companents ane2orer2024 Single Fle Components - Real World Vus 3 | Vue Mastery
‘So what do these single file components look like under the hood?
Atypical vue component has three sections: ,
Now that this file is cleared out, we can add our own code to it. First, let’s add some styles. We'll change
the class name on the div in order to do that.
‘sr¢/components/EventCard.vue
Now the div has the proper styles, including a hover effect. If you're wondering what
that scoped attribute means, that allows us to scope and isolate these styles to just this component.
This way, these styles are specific to this component and won't affect any other part of our application.
You'll see me using scoped styles throughout this course.
Since we want to display information about the event on this EventCard, we need to give it an event to
display. So let’s add thatin the data option of our
Now, inthe wecan display some of that event data with JavaScript expressions, like
so:
sre/components/EventCard.vue
hitps fiw: vwemastery.comicoursestreal-warld-vueSsngle-le-components me2orerz024 Single Fle Components - Real World Vus 3 | Vue Mastery
@{{ event.time }} on {{ event.date }}
ch4>{{ event.title }}
That's it for the component for now.
In order for this EventCard to be displayed, it needs to be put somewhere that can be routed to, such as
the Home.vue file in our views directory. Just like with the HelloWorld.vue file, we'll need to
import EventCard.vue, register it as a child component, and then we can use it in the template.
Osre/views/Home.vue
Now, we should be seeing our EventCard showing up in the browser when we're on the Home view.
hitps dwn: vwemastery.comfcoursestreal-word-vueiinglefle-companents ane‘Single Fle Components
Home | About
@ 1200 on January 28, 2022
Cat Adoption Day
Refactoring for a more production-ready use case
We're making great progress, but remember we want the EventCard to be displaying in the middle of the
Home page. And, since we'll eventually have a collection of events that we pull in from an API call, we
need to do a bit of refactoring to make this a more production-ready use case.
Our refactoring steps include:
Move events data to parent (Home.vue)
Parent creates EventCard component for each event in its data
Parent feeds each EventCard its own event to display
Parent displays EventCards in a Flexbox container
Let's get started with this refactor.
Move events data to parent
Our first step is to delete out the event data from EventCard. We'll then add an event prop instead,
so that the parent can feed this component an event object to display. We're then left with this code:
Osrc/components/EventCard.vue
hitps:iwwn-vemastory.comfcoursesireal-wort-vue/single- one2osa2021 Single Fie Components - Real Word We 3 | ue Mastery
@{{ event.time }} on {{ event.date }}
ch4>{{ event.title }}
Now that EventCard is set up to receive an event, we can add the events data to the
parent, Home.vue.
‘src/views/Home.vue
Parent creates EventCard components
Now that Home.vue has the events data, we can use that data to create a new EventCard for each of
the event objects that are in that data, using the v-for directive.
r¢/views/Home.vue
reven
Notice how we're binding the event's id tothe :key attribute. This gives Vue,js a way to identify and
can keep track of each unique EventCard.
Parent feeds each EventCard its own event
Additionally, as we iterate over the events array to create a new EventCard for each event object,
we're passing in that event object into anew :event prop we've added to the EventCard. This
way, each EventCard has all of the data it needs to display its own event info.
Parent displays EventCards in a Flexbox container
If we check this out in the browser, it’s working. We've created an EventCard for each of
the events in Home.vue's data.
hitps www: vwemastery.comicoursestreal-warl-vueSsingle-le-components rane2o1er2021 ‘Single Fle Components
Home | About
12:00 on January 28, 2022
Cat Adoption Day
10:00 on March 14, 2022
Community Gardening
11:00 on July 22, 2022
Beach Cleanup
Finally, we just need to put these events in a Flexbox container to get things looking how we want. Let's
head into the Home.vue file and change the class name of the div that our EventCard is nested
within, and add some Flexbox styles.
Cysre/views/Home.vue
Events For Good
event in events" :key="event.id"
Now, our EventCards will be displayed within a center-aligned column.
hitps www: vwemastery.comicoursestreal-warl-vueSsingle-le-components sane2orerz024 Single Fle Components - Real World Vus 3 | Vue Mastery
What about Global Styles?
So far, we've discussed scoped styles and how the scoped attribute allows us to add styles that target
the specific component we're concerned about. But what about global styles that we want applied to our
entire app? While there are different ways to achieve this, the simplest way to get started with this is by
heading into the App.vue file. Remember: this is the root component of our app.
Notice that there are some styles rules that the CLI set up for usin this component's
‘These are global styles that are applied to the entire app. Here, we could add a new rule. Like so:
Osre/App.vue
Now, any h4 in our app will havea font-size of 2@px . Since our EventCard’s template has an h4,
that element will receive this new global style.
‘src/components/EventCard.vue
@{{ event.time }} on {{ event.date }}
{{ event.title }}
Speaking of global items in our Vue app, what would happen if we added something like an h1 to
our App.vue's template?
‘src/App.vue
Events For Good
"/">Home |
/about" >About
Let’s head into the browser and take a look.
hitps dwn: vwemastery.comfcoursestrea-word-vueiinglefle-companents rene2o1er2024 ‘Single Fle Components
Home | About
Events For Good
212:00 on January 28, 2022
Cat Adoption Day
10:00 on March 14, 2022
Community Gardening
(211:00 on July 22, 2022
We're seeing a few things. First, our Flexbox container is working (RM) and the event titles are now just a
bit bigger ( 2@px ) due to that new global h4 style rule we added. And notice what happens when we
navigate to the About route.
Home | Abalyy
Events For Good
This is an about page
We're still seeing that h1_ displaying “Events For Good”. So this tells us that we can place content in
our App.vue’s template that we want to be displayed globally across every view of our application. This
could be useful for things like a search bar, header, or of course a nav bar like we already have here.
hitps fiw: vwemastery.comicoursestreal-warld-vueSsngle-le-components we2oie02t Single Fle Components Real World We 3 | Vue Masry
But for our use case, we don’t need that title showing up in every view, so we'll place it into
the Home.vue file.
‘src/views/Home.vue
Events For Good
Now that title will only show up one the Home route.
Let’s ReVue
We've covered a lot. We learned what a single file vue component is, how it's composed
(with scoped versus global styles), and how to start using these vue components to build up a Vue
app. In the next lesson, we're going to dive deeper into the essentials of Vue Router to better understand
how to set up app navigation. See you there!
hitps www: vwemastery.comicoursestreal-warl-vueSsingle-le-components rene