Getting started with Vue.js - part three - data binding

Getting started with Vue.js - part three - data binding

Published 30. aug 2019 11:25 by Stein Ove Helset

In the third part of the course we will learn how to bind data from the data object to attributes inside the template.

Binding data to attributes

Binding data to an attribute is something that is very often used in Vue and in this part of the course we will show you how it's one. First we'll show an example that we will use in our app and then we will explain a little bit more.

We want to add a picture to our app and instead of hard-coding it directly into our template we'll add the image as a part of the data object.

Vue Hero - Introduction - Attribute binding

When we have done this we can scroll up to the HTML and add a extra code block to the card-element.

Vue Hero - Introduction - Card element Equivalents
<img :src="image"> will be the same as:
<img src="">

The :src="image" directive is a shorthand for v-bind:src="image". We prefer the short hand with just a colon since it's shorter and more readable.

Mini challenge

The mini challenge of this part is to add another attribute to the image. Try adding a alternative text in the data object and then add it to the image using a directive. This will not be a part of the course so when you're finished you can just remove it.

Share this post


Add comment