(Food + Drinks + Venue)
The Essentials of Vue.js
December 6th, 2017
After hand-rolling many [UI prototypes] with vanilla JavaScript and using Angular 1 for a few, I wanted something that captured the declarative nature of Angular's data binding, but with a simpler, more approachable API.
Credit - xkcd: Standards (https://xkcd.com/927/)
Easy to get something up and running pretty quickly
Most of what was happening seemed more like magic
Larger footprint in web performance
Developed and maintained by Google
More opinionated on how you should build your app
Great performance due to the use of the virtual DOM
High learning curve even just to get started
Unfriendly to non-JavaScript developers (i.e., JSX)
Developed by Facebook and recently granted an MIT license
High pace of development resulting in unexpected bugs
You got a lot better at vanilla JavaScript very quickly
Takes the best of both worlds and brings them together
Great performance that is on par if not better than React.js
Flexible and accommodating to how you prefer to build apps
An open-source framework with no corporate influence
Smaller team and community compared to React / Angular
It does not alienate non-JavaScript developers
const app = new Vue()
const adventure = new Trip()
const adventure = new Trip({
destination: 'Diagon Alley',
luggage: {
name: 'Mr. B. Hong',
house: 'Ravenclaw',
wand: '12.5" larch wood w/ Dragon heartstring core',
wallet: {
galleons: 12,
sickles: 38,
knuts: 274
}
},
thingsToDo: {
visitGringotts,
visitDiagonAlley,
drinkButterbeer,
eatAtTheLeakyCauldron
}
})
<!-- Start with an HTML element that will serve as
the app's destination -->
<div id="diagon-alley"></div>
// This is the most important configuration
// Mistype this one and nothing will ever happen 😱
const adventure = new Vue({
el: '#diagon-alley'
})
// This where all your "luggage" items are stored
const adventure = new Vue({
el: '#diagon-alley',
data: {
name: 'Mr. B. Hong',
house: 'Ravenclaw',
wand: '12.5" larch wood w/ Dragon heartstring core',
wallet: {
galleons: 12,
sickles: 38,
knuts: 274
}
}
})
// This where all your instructions are stored
const adventure = new Vue({
el: '#diagon-alley',
data: {
name: 'Mr. B. Hong',
house: 'Ravenclaw',
wand: '12.5" larch wood w/ Dragon heartstring core',
wallet: {
galleons: 12,
sickles: 38,
knuts: 274
}
},
methods: {
visitGringotts() { ... },
visitDiagonAlley() { ... },
drinkButterbeer() { ... },
eatAtTheLeakyCauldron() { ... }
}
})
They are Vue specific methods that allow you to accomplish common goals without worrying how it is implemented.
It ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles."
The element is always rendered regardless of initial condition, with CSS-based toggling.
Allows us to "render a list of items based on an array [or object]."
<!-- Given the data model on the right -->
<ul>
<li v-for="house in Hogwarts">
{{ house }}
</li>
</ul>
<!-- It will render to -->
<ul>
<li>Ravenclaw</li>
<li>Hufflepuff</li>
<li>Slytherin</li>
<li>Griffindor</li>
</ul>
// Given this sample Vue instance
new Vue({
...
data: {
Hogwarts: [
'Ravenclaw',
'Hufflepuff',
'Slytherin',
'Gryffindor'
]
}
})
Allow us to manipulate HTML attributes with dynamic data
<!-- The long form -->
<div v-bind:class="{ active: isActive }">...</div>
<!-- The common shortcut -->
<div :class="{ active: isActive }">...</div>
<!-- If isActive is true, it will render -->
<div class="active">...</div>
Allow us to attach methods to common events
<!-- The long form -->
<div v-on:click="alert('Alohomora!')">...</div>
<!-- The common shortcut -->
<div @click="alert('Alohomora!')">...</div>
Allows us to use two-way data binding
<input v-model="name" type="text" id="name" />
<script>
new Vue({
...
data: {
name: 'Sirius Black'
}
})
</script>
Final Coding Exercise
Final Coding Exercise
Gives me what I want when I need it, and then it gets out of my way.