The main aim of this tutorial is to provide an in-depth understanding of the Vue instance, the cornerstone of Vue.js applications. We'll examine its properties, methods, and lifecycle hooks.
By the end of this tutorial, you will have a good understanding of how to:
- Create a Vue instance
- Use the Vue instance's properties and methods
- Understand and use Vue lifecycle hooks
Basic knowledge of JavaScript and Vue.js is recommended. Familiarity with HTML and CSS will be helpful but is not required.
A Vue instance is essentially a ViewModel as defined in the Model-View-ViewModel architectural pattern. It serves as the intermediary between the Vue JavaScript framework and the DOM (Document Object Model).
To create a Vue instance, we use the Vue constructor. Here is a simple example:
var vm = new Vue({
// options
})
When a Vue instance is created, it adds all the properties found in its data object to Vue’s reactivity system. This means that when the values of those properties change, the view will "react", updating to match the new values.
Here's how you can define data and methods within a Vue instance:
var vm = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
},
methods: {
reverseMessage: function () {
this.message = this.message.split('').reverse().join('')
}
}
})
Vue instances go through a series of initialization steps when they are created - for instance, they need to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes. Along the way, Vue also runs functions called lifecycle hooks, giving users the opportunity to add their own code at specific stages.
// Create a new Vue instance
var vm = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
In the example above, 'el' is used to specify the element that the Vue instance will be mounted on, 'data' is an object that holds the data for the Vue instance, and 'message' is a property on the 'data' object.
var vm = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
},
methods: {
reverseMessage: function () {
this.message = this.message.split('').reverse().join('')
}
}
})
In this example, we have a 'methods' object that holds the methods for the Vue instance. The 'reverseMessage' method reverses the string in the 'message' property.
var vm = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
},
created: function () {
console.log('message is: ' + this.message)
}
})
In this example, we have a 'created' lifecycle hook, which is a function that will run after the instance is created. It logs the message property to the console.
In this tutorial, we learned how to create a Vue instance, use the Vue instance's properties and methods, and understand and use Vue lifecycle hooks. Next, you may want to learn about Vue components, which are reusable instances with a name.
Create a Vue instance that binds to an element with id 'app', has a data property 'name' and a method that changes the 'name' to 'Vue.js'.
Create a Vue instance that logs 'Vue instance created!' to the console when the Vue instance is created.
Exercise 1
var vm = new Vue({
el: '#app',
data: {
name: 'My Name'
},
methods: {
changeName: function () {
this.name = 'Vue.js'
}
}
})
Exercise 2
var vm = new Vue({
el: '#app',
created: function () {
console.log('Vue instance created!')
}
})