Introduction to Vue.js
Vue (pronouced like view) is a progressive framework for building UI. Perfectly capable of powering Single-Page Applications when used with modern tooling and supporting libraries
Declarative Rendering
<div id="app">
</div>
<div id="app-2">
<span v-bind:title="message">
Hover your mouse over me for a few seconds
to see my dynamically bound title!
</span>
</div>
var app = new Vue({
el: '#app', // element to id="app"
data: {
message: 'Hello Vue!'
}
})
var app2 = new Vue({
el: '#app-2',
data: {
message: 'You loaded this page on ' + new Date()
}
})
Conditionals and Loops
<div id="app-3">
<p v-if="seen">Now you see me</p>
</div>
<div id="app-4">
<ol>
<li v-for="todo in todos">
</li>
</ol>
</div>
var app3 = new Vue({
el: '#app-3',
data: {
seen: true
}
})
var app4 = new Vue({
el: '#app-4',
data: {
todos: [
{ text: 'Learn JavaScript' },
{ text: 'Learn Vue' },
{ text: 'Build something awesome' }
]
}
})
Handling User Input
<div id="app-5">
<p></p>
<button v-on:click="reverseMessage">Reverse Message</button>
</div>
<div id="app-6">
<p></p>
<input v-model="message">
</div>
var app5 = new Vue({
el: '#app-5',
data: {
message: 'Hello Vue.js!'
},
methods: {
reverseMessage: function () {
this.message = this.message.split('').reverse().join('')
}
}
})
var app6 = new Vue({
el: '#app-6',
data: {
message: 'Hello Vue!'
}
})
Composing with Components
// Define a new component called todo-item
Vue.component('todo-item', {
template: '<li>This is a todo</li>'
})
// added props
Vue.component('todo-item', {
// The todo-item component now accepts a
// "prop", which is like a custom attribute.
// This prop is called todo.
props: ['todo'],
template: '<li></li>'
})
Vue.component('todo-item', {
props: ['todo'],
template: '<li></li>'
})
var app7 = new Vue({
el: '#app-7',
data: {
groceryList: [
{ id: 0, text: 'Vegetables' },
{ id: 1, text: 'Cheese' },
{ id: 2, text: 'Whatever else humans are supposed to eat' }
]
}
})
<ol>
<!-- Create an instance of the todo-item component -->
<todo-item></todo-item>
</ol>
<div id="app-7">
<ol>
<!--
Now we provide each todo-item with the todo object
it's representing, so that its content can be dynamic.
We also need to provide each component with a "key",
which will be explained later.
-->
<todo-item
v-for="item in groceryList"
v-bind:todo="item"
v-bind:key="item.id">
</todo-item>
</ol>
</div>
In a larger application, it is necessary to divide the whole app into components to make development manageable.
<div id="app">
<app-nav></app-nav>
<app-view>
<app-sidebar></app-sidebar>
<app-content></app-content>
</app-view>
</div>
Written on July 14, 2017