Skip to Content
Vue.js 2 Cookbook
book

Vue.js 2 Cookbook

by Andrea Passaglia
April 2017
Intermediate to advanced
454 pages
12h 51m
English
Packt Publishing
Content preview from Vue.js 2 Cookbook

How to do it...

You will build a little game--a bus station simulator!

Whenever a bus--represented by its emoji--leaves the station, all the other buses will drive a little ahead to take its place. Every bus is identified by a number, as you can see from the Vue instance data:

new Vue({   el: '#app',   data: {     buses: [1,2,3,4,5],     nextBus: 6   } })

Whenever a new bus arrives, it will have a progressive number assigned. We want a new bus to leave or go every two seconds. We can achieve this by hooking a timer when our component is mounted on screen. Immediately after data, write the following:

mounted () {   setInterval(() => {     const headOrTail = () => Math.random() > 0.5     if (headOrTail()) {       this.buses.push(this.nextBus)  this.nextBus += ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Vue.js 2

Learning Vue.js 2

Olga Filipova
Pro Vue.js 2

Pro Vue.js 2

Adam Freeman

Publisher Resources

ISBN: 9781786468093Supplemental Content