Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Vue.js 2 Cookbook

You're reading from   Vue.js 2 Cookbook Build modern, interactive web applications with Vue.js

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781786468093
Length 454 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Andrea Passaglia Andrea Passaglia
Author Profile Icon Andrea Passaglia
Andrea Passaglia
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started with Vue.js 2. Basic Vue.js Features FREE CHAPTER 3. Transitions and Animations 4. All About Components 5. Vue Communicates with the Internet 6. Single Page Applications 7. Unit Testing and End-to-End Testing 8. Organize + Automate + Deploy = Webpack 9. Advanced Vue.js – Directives, Plugins, and Render Functions 10. Large Application Patterns with Vuex 11. Integrating with Other Frameworks

Writing lists

The desire to produce lists almost seems to be an innate part of human nature. There is a deeply satisfying feeling that one obtains by watching a well ordered list marching down the computer screen.

With Vue, we are given the tools to make lists of any kind with a stunning appearance and maximum ease.

Getting Ready

For this recipe we are going to use basic data-binding, if you follow the very first recipe you are already familiar with it.

How to do it...

We are going to build lists in a couple of different ways: with a range of numbers, with an array, and finally with an object.

Range of numbers

To start off with lists, set up your JSFiddle like in the preceding recipe, adding Vue.js as a framework. Select Vue 2.2.1 (or Vue (edge)):

  1. In the JavaScript section, write:
        new Vue({el:'#app'})
  1. And in the HTML write:
        <div id="app"> 
<ul>
<li v-for="n in 4">Hello!</li>
</ul>
</div>

This will result in a list with Hello! written four times. In a few seconds your first list is complete, nice job!

We can write a countdown with this technique--in the HTML, replace the content of the <div> with the following:

<div id="app"> 
<ul>
<li v-for="n in 10">{{11-n}}</li>
<li>launch missile!</li>
</ul>
</div>

Arrays

  1. In the HTML, to achieve the same result, edit the list to reflect the following:
        <ul> 
<li v-for="n in [10,9,8,7,6,5,4,3,2,1]">{{n}}</li>
<li>launch missile!</li>
</ul>

Although this list is identical to the previous one, we shouldn't put literal arrays in HTML markup.

  1. We're better off with a variable that contains the array. Edit the preceding code to match the following:
        <ul> 
<li v-for="n in countdown">{{n}}</li>
<li>launch missile!</li>
</ul>
  1. Then put the array countdown in the JavaScript:
        new Vue({ 
el:'#app',
data: {
countdown: [10,9,8,7,6,5,4,3,2,1]
}
})

Arrays with index notation

When enumerating an array, we also have access to the index, represented by the variable i in the following code:

  1. The HTML becomes:
        <div id="app"> 
<ul>
<li v-for="(animal, i) in animals">
The {{animal}} goes {{sounds[i]}}
</li>
</ul>
</div>
  1. In the code part, write:
        new Vue({ 
el: '#app',
data: {
animals: ['dog', 'cat', 'bird'],
sounds: ['woof', 'meow', 'tweet']
}
})

Objects

The preceding example can be refactored to match animal names and their sounds so that an accidental misalignment of the index will not affect our list.

  1. The HTML becomes:
        <div id="app"> 
<ul>
<li v-for="(sound, name) in animals">
The {{name}} goes {{sound}}
</li>
</ul>
</div>
  1. And we need to create the animals object in the JavaScript:
        new Vue({ 
el: '#app',
data: {
animals: {
dog: 'woof', cat: 'meow', bird: 'tweet'
}
}
})

How it works...

The workings of lists are quite simple; here is a little more explanation on the syntax.

Range of numbers

The variable n is in scope inside the <li> tag. To prove it to yourself, you can quickly build a countdown list as follows:

<ul> 
<li v-for="n in 10">{{11 - n}}</li>
<li>launch missile!</li>
</ul>

We write 11 instead of 10 because enumeration in Vue is 1-indexed; this means that n in 10 will start to count from 1, not from 0 like someone might expect, and go up to 10. If we want our countdown to start from 10, then we have to put 11. The last number will be 10, so we'll have 1 as the last number before the missile is launched.

What v-for="n in 10" does is call enumeration; specifically we are enumerating a range of numbers (1 to 10).

Arrays

Vue allows us to enumerate arrays too. The general syntax is as follows:

v-for="(element, index) in array"

As seen, the index and parenthesis can be omitted if all we want are the array elements.

This form of enumeration is guaranteed to be ordered. In other words, the ordered sequence of elements in the array will be the same you will see on the screen; this is not the case when enumerating objects.

Objects

The syntax is v-for="(value, property)" and if you want you can also squeeze in the index with v-for="(value, property, index)". The latter is not recommended though since, as already said, the order in which properties are enumerated is not fixed. In practice, in most browsers, the order is the same as the insertion order but this is not guaranteed.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime