This tutorial will introduce you to Vue.js, a progressive JavaScript framework used to develop interactive web interfaces. It's a simple yet powerful library for building cool web applications.
Familiarity with HTML, CSS, and basic JavaScript would be beneficial. No prior knowledge of Vue.js is required.
Vue.js is a framework for creating user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. The core library focuses on the view layer only and is easy to pick up and integrate with other libraries.
Every Vue application starts by creating a new Vue instance with the Vue function:
var vm = new Vue({
// options
})
Here, the vm
variable represents the Vue instance.
Vue.js uses a template system. This means you can use plain HTML templates and Vue.js will replace variables with actual values, based on the Vue instance data.
var vm = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
In the HTML, you can then reference this message variable like so:
<div id="app">
{{ message }}
</div>
Here, {{ message }}
is a placeholder for the actual value of the message
data property from the Vue instance.
// Create a new Vue instance
var vm = new Vue({
el: '#example',
data: {
message: 'Hello Vue!'
}
})
In this example, el: '#example'
tells Vue to bind this instance to the element in the DOM with the id of 'example'. data: { message: 'Hello Vue!' }
is creating a new data property called 'message' with the value 'Hello Vue!'.
The corresponding HTML would be:
<div id="example">
{{ message }}
</div>
The expected output in the browser would be: 'Hello Vue!'.
Directives are special attributes with the v-
prefix. Directive attribute values are expected to be a single JavaScript expression (with the exception for v-for
). Here's an example:
var vm = new Vue({
el: '#demo',
data: {
seen: true
}
})
In the HTML, we can use the v-if
directive to conditionally render elements:
<div id="demo">
<p v-if="seen">Now you see me</p>
</div>
If the seen
data property is true
, the paragraph will be displayed. If it's false
, it won't be.
In this tutorial, you've learned the basics of Vue.js, how to create a new Vue instance, and how to use Vue's template syntax to create dynamic views. You've also been introduced to Vue directives.
Next steps for your learning could include learning about Vue components, Vue Router for creating single-page applications, and Vuex for state management. You can find more resources on the official Vue.js documentation.
Create a Vue instance that binds to an element with the id 'practice1', with a data property 'message' that has the value 'This is a practice exercise'.
Expand on Exercise 1 by adding a 'seen' data property with a value of true
. In your HTML, use a v-if
directive to conditionally render a paragraph element that says 'You can see me!'.
Exercise 1
var vm = new Vue({
el: '#practice1',
data: {
message: 'This is a practice exercise'
}
})
HTML:
<div id="practice1">
{{ message }}
</div>
Exercise 2
var vm = new Vue({
el: '#practice2',
data: {
message: 'This is a practice exercise',
seen: true
}
})
HTML:
<div id="practice2">
<p v-if="seen">{{ message }}</p>
</div>
Continue practicing by creating more Vue instances with different data properties and playing around with Vue directives like v-for
for rendering lists.