vuejs_tutorial (1) (1)
vuejs_tutorial (1) (1)
Vue is a JavaScript framework for building user interfaces. Its core part is focused mainly on
the view layer and it is very easy to understand. The version of Vue that we are going to use
in this tutorial is 2.0.
As Vue is basically built for frontend development, we are going to deal with lot of HTML,
JavaScript and CSS files in the upcoming chapters. To understand the details, let us start with
a simple example.
Example
<html>
<head>
<title>VueJs Introduction</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<div id="intro" style="text-align:center;">
<h1>{{ message }}</h1>
</div>
<script type="text/javascript">
var vue_det = new Vue({
el: '#intro',
data: {
message: 'My first VueJS Task'
}
});
</script>
</body>
</html>
16
VueJS
Output
This is the first app we have created using VueJS. As seen in the above code, we have included
vue.js at the start of the .html file.
There is a div which is added in the body that prints “My first VueJS Task” in the browser.
We have also added a message in a interpolation, i.e. {{}}. This interacts with VueJS and
prints the data in the browser. To get the value of the message in the DOM, we are creating
an instance of vuejs as follows:
In the above code snippet, we are calling Vue instance, which takes the id of the DOM element
i.e. e1:’#intro’, it is the id of the div. There is data with the message which is assigned the
value ‘My first VueJS Task’. VueJS interacts with DOM and changes the value in the DOM
{{message}} with ’My first VueJS Task’.
17
VueJS
If we happen to change the value of the message in the console, the same will be reflected
in the browser. For example:
Console Details
In the above console, we have printed the vue_det object, which is an instance of Vue. We
are updating the message with “VueJs is interesting” and the same is changed in the
browser immediately as seen in the above screenshot.
This is just a basic example showing the linking of VueJS with DOM, and how we can
manipulate it. In the next few chapters, we will learn about directives, components,
conditional loops, etc.
18
4. VueJS – Instances VueJS
To start with VueJS, we need to create the instance of Vue, which is called the root Vue
Instance.
Syntax
var app = new Vue({
// options
})
Let us look at an example to understand what needs to be part of the Vue constructor.
<html>
<head>
<title>VueJs Instance</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<div id="vue_det">
<h1>Firstname : {{firstname}}</h1>
<h1>Lastname : {{lastname}}</h1>
<h1>{{mydetails()}}</h1>
</div>
<script type="text/javascript" src="js/vue_instance.js"></script>
</body>
</html>
19
VueJS
vue_instance.js
For Vue, there is a parameter called e1. It takes the id of the DOM element. In the above
example, we have the id #vue_det. It is the id of the div element, which is present in .html.
<div id="vue_det"></div>
Now, whatever we are going to do will affect the div element and nothing outside it.
Next, we have defined the data object. It has value firstname, lastname, and address.
<div id="vue_det">
<h1>Firstname : {{firstname}}</h1>
<h1>Lastname : {{lastname}}</h1>
</div>
20
VueJS
The Firstname : {{firstname}} value will be replaced inside the interpolation, i.e. {{}} with
the value assigned in the data object, i.e. Ria. The same goes for last name.
Next, we have methods where we have defined a function mydetails and a returning value.
It is assigned inside the div as
<h1>{{mydetails()}}</h1>
Hence, inside {{} } the function mydetails is called. The value returned in the Vue instance
will be printed inside {{}}. Check the output for reference.
Output
Now, we need to pass options to the Vue constructor which is mainly data, template, element
to mount on, methods, callbacks, etc.
#data: This type of data can be an object or a function. Vue converts its properties to
getters/setters to make it reactive.
21
VueJS
Example
<html>
<head>
<title>VueJs Introduction</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<script type="text/javascript">
var _obj = { fname: "Raj", lname: "Singh"}
// direct instance creation
var vm = new Vue({
data: _obj
});
console.log(vm.fname);
console.log(vm.$data);
console.log(vm.$data.fname);
</script>
</body>
</html>
22
VueJS
Output
23
VueJS
If there is a component, the data object has to be referred from a function as shown in the
following code.
<html>
<head>
<title>VueJs Introduction</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<script type="text/javascript">
var _obj = { fname: "Raj", lname: "Singh"};
// direct instance creation
var vm = new Vue({
data: _obj
});
console.log(vm.fname);
console.log(vm.$data);
console.log(vm.$data.fname);
// must use function when in Vue.extend()
var Component = Vue.extend({
data: function () {
return _obj
}
});
var myComponentInstance = new Component();
console.log(myComponentInstance.lname);
console.log(myComponentInstance.$data);
</script>
</body>
</html>
24
VueJS
In case of a component, the data is a function, which is used with Vue.extend as shown above.
The data is a function. For example,
data: function () {
return _obj
}
To refer to the data from the component, we need to create an instance of it. For example,
To fetch the details from the data, we need to do the same as we did with the parent
component above. For example,
console.log(myComponentInstance.lname);
console.log(myComponentInstance.$data);
25
VueJS
Props: Type for props is an array of string or object. It takes an array-based or object-based
syntax. They are said to be attributes used to accept data from the parent component.
Example 1
Vue.component('props-demo-simple', {
props: ['size', 'myMessage']
})
Example 2
Vue.component('props-demo-advanced', {
props: {
// just type check
height: Number,
// type check plus other validations
age: {
type: Number,
default: 0,
required: true,
validator: function (value) {
return value >= 0
}
}
}
})
Type: array of string. For example, { [key: string]: any }. It needs to be passed during the
creation of Vue instance.
Example
26
VueJS
})
var vm = new Comp({
propsData: {
msg: 'hello'
}
})
Example
<html>
<head>
<title>VueJs Introduction</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<script type="text/javascript">
var vm = new Vue({
data: { a: 2 },
computed: {
// get only, just need a function
aSum: function () {
return this.a + 2;
},
// both get and set
aSquare: {
get: function () {
return this.a*this.a;
},
set: function (v) {
this.a = v*2;
}
27
VueJS
}
}
})
console.log(vm.aSquare); // -> 4
vm.aSquare = 3;
console.log(vm.a); // -> 6
console.log(vm.aSum); // -> 8
</script>
</body>
</html>
Function aSum just returns this.a+2. Function aSquare again two functions get and set.
Variable vm is an instance of Vue and it calls aSquare and aSum. Also vm.aSquare = 3 calls
the set function from aSquare and vm.aSquare calls the get function. We can check the output
in the browser which looks like the following screenshot.
Methods: Methods are to be included with the Vue instance as shown in the following code.
We can access the function using the Vue object.
<html>
<head>
<title>VueJs Introduction</title>
<script type="text/javascript" src="js/vue.js"></script>
</head>
<body>
<script type="text/javascript">
var vm = new Vue({
data: { a: 5 },
28
VueJS
methods: {
asquare: function () {
this.a *= this.a;
}
}
})
vm.asquare();
console.log(vm.a); // 25
</script>
</body>
</html>
Methods are part of the Vue constructor. Let us make a call to the method using the Vue
object vm.asquare (), the value of the property a is updated in the asquare function. The
value of a is changed from 1 to 25, and the same is seen reflected in the following browser
console.
29
VueJS
30