So far we learned the basics of Vue and how to set up a nice workflow with Parcel. But what if we want to make our app look nice?
Tell you what — why don’t we make our counter appear green when it’s over 1, red when it’s below 0 and grey when it’s 0. The logic should look like this;
How would you do this?
We start with the
I prepared 3 classes —
.grey — each corresponding to their appropriate color.
Notice I’m passing the
grey class to our
Well, it works — it’s grey like we told the sum to be. But it’s not turning green when it’s over 0 and it’s not turning red when it’s below 0.
We need to hook it up with Vue bindings!
Can you figure out how to do it? Remember our
v-bind or the shorthand; this lets us write logic inside HTML.
Pay close attention to the class — it’s no regular class, it’s a Vue binding — which means we can insert logic and conditionals.
Applying conditionals for styles is straightforward. Left side is the class name and on the right side is when that class is applied/conditional.
Cool! There’s just one thing bothering me — the inline logic looks bad if there’s more than 1 conditional.
Let’s refactor the class conditionals to a method — more specifically to a computed property.
Putting too much logic in your templates can make them bloated and hard to maintain. That’s why for any complex logic, you should use a computed property.
Computed properties are cached based on their dependencies. A computed property will only re-evaluate when some of its dependencies have changed.
Creating our first computed property
There we go! We have our first computed property — much easier to read and we get more performance since computed properties are cached and only compute when they need to — when the conditionals are met in this case.
Great job, we have covered most basic building blocks for Vue by now! See how intuitive Vue is? I love how there’s literally no plumbing involved.
When we’re building single page applications, we often have components which communicate with each other. The most common pattern we come across is the parent-children relationship.
Here’s an example parent to children component hierarchy;
We pass props from parent to children and emit events from child to parent.
Every modern app has so-called “container” components. In a nutshell, container components handle the logic and “components” handle the views (markup).
Here’s an in-depth explanation of the concept; it applies to all modern frontend frameworks.
Let’s refactor our counter by moving the counter to a separate and reusable component.
As you noticed by now, there’s a new element in our template. It’s called
When the component renders, the
slot element will be replaced by the content passed to it.
Header.vue to our
Counter.vue like so;
Now we have access to the
Header component which renders the title passed to it.
If we check the browser you might notice something interesting.
Everything changes color! What if we only want the number to change based on the sum.
Named slots are a way we can control which content goes where.
We can give a name to our slot element like so;
named slots; notice the name attribute on the slot
Parent renders regular HTML elements with a special attribute;
Named slots notice we pass the class to the sum only
Tada! That’s all there is to slots!
slot element is a placeholder for future content. A named slot is when we have more than 1 slot per component. The whole concept behind slots is to make code reusable as much as possible.
There we go!
Here’s the source code
Here’s my twitter, you might find more useful stuff there!
Thanks for reading!