What Is Vue.js?
The Vue.js is a progressive JavaScript framework and used to building the interactive user interfaces and also it’s focused on the view layer only (front end).
The Vue.js is a progressive JavaScript framework and used to building the interactive user interfaces and also it’s focused on the view layer only (front end).
The Vue.js is easy to integrate with other libraries and others existing projects. Vue.js is very popular for Single Page Applications developments.
The Vue.js is lighter, smaller in size and so faster. It also supports the MVVM (Model-View-ViewModel) pattern.
The Vue.js is supporting to multiple Components and libraries like -
ü Tables and data grids
ü Notifications
ü Loader
ü Calendar
ü Display time, date and age
ü Progress Bar
ü Tooltip
ü Overlay
ü Icons
ü Menu
ü Charts
ü Map
ü Pdf viewer
ü And so on
The Vue.js was developed by “Evan You”, an Ex Google software engineer. The latest version is Vue.js 2. The Vue.js 2 is very similar to Angular because Evan You was inspired by Angular and the Vue.js 2 components looks like -
ü v-show
ü v-if
ü v-for
ü v-else
ü v-on
ü v-model
What Is Vue.js 2?
The Vue.js 2 is similar to ReactJs and Angular in some ways. There are some key features that will help us to started easily i.e.
ü Directives
ü Components
ü Template and JSX
ü Routing
ü And so on
The Vue.js 2 is similar to ReactJs and Angular in some ways. There are some key features that will help us to started easily i.e.
ü Directives
ü Components
ü Template and JSX
ü Routing
ü And so on
New In VueJs 2 -
ü Rendering layer is now based on a lightweight virtual DOM implementation
ü Detection of static class names and attributes
ü Detection of sub trees without dynamic bindings
ü It supports server side rendering with client side hydration
ü It Support to JSX
ü Template-to-virtual-DOM compiler
Example 1 - VueJs 2 v-if conditions
HTML Code -
<script src="https://unpkg.com/vue"></script>
<div id="demo-app">
<p v-if="IsEnable()">{{ WelcomeMsg }}</p>
</div>
VueJs Code -
var app = new Vue({
el: '#demo-app',
data: {
WelcomeMsg: 'Hello, My Name Is Anil Singh.'
},
methods: {
IsEnable: function() {
return true;
}
}
});
Result looks like - Hello, My Name Is Anil Singh.
Example 2 – VueJs 2 v-for loop -
HTML Code –
<script src="https://unpkg.com/vue"></script>
<div><h2>v-for loop</h2></div>
<div id="forLoop">
<ol>
<li v-for="user in users">
{{ user.name }} , Age - {{ user.age }}
</li>
</ol>
</div>
VueJs 2 Code –
var app = new Vue({
el: '#forLoop',
data: {
users: [
{id:1, name: 'Anil Singh', age: 32},
{id:2, name: 'Aradhya Singh' , age: 4},
{id:3, name: 'Reena Singh' , age: 25},
{id:4, name: 'Sunil', age: 27 }
]
}
});
The Result looks like –
v-for loop -
ü Anil Singh , Age - 32
ü Aradhya Singh , Age - 4
ü Reena Singh , Age - 25
ü Sunil , Age – 27
How To Install Vuejs/ Vuejs 2?
Download Vue.js by following below methods –
ü https://vuejs.org/js/vue.min.js
ü npm install vue
ü bower install vue
ü CDN – https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js
ü Routing - https://unpkg.com/vue-router@3.0.1/dist/vue-router.js
Install the vue-cli tool globally-
npm install -g vue-cli
After vue-cli globally installed, scaffold a new Vuejs 2 app -
vue init webpack ultimate-startup-battle
How a View Model works in Vue.js?
HTML Code-
<script src="https://unpkg.com/vue"></script>
<div class="demo-app" id="demo-app">
<p>{{WelcomeMsg}}</p>
</div>
VueJs Code-
new Vue({
el: '.demo-app', //'#demo-app'
data: {
WelcomeMsg: 'Hello, Anil Singh'
}
});
Result Looks Like- Hello, Anil Singh
What's New In Vuejs 2?
ü Rendering layer is now based on a lightweight virtual DOM implementation
ü Detection of static class names and attributes
ü Detection of sub trees without dynamic bindings
ü It supports server side rendering with client side hydration
ü It Support to JSX
ü Template-to-virtual-DOM compiler
What's similar React and Vue.js?
ü Both React and Vue.js are Utilize a virtual DOM.
ü Both React and Vue.js are Provide a compassable view components.
ü Both React and Vue.js are handling state, routing, network requests and so on.
What Are differences in React and Vue.js?
ü The Vuejs virtual DOM rendering is so faster than that of ReactJs virtual DOM rendering.
ü The Vuejs component’s dependencies are automatically render but ReactJs avoid unnecessary re-rendering so we can manually implements.
ü The Vuejs 2 app size is smaller than ReactJs and Angular.
How to CREATE an instance of Vuejs?
//View Model Class
var viewModel = new Vue({
// options
})
////View Model Class
var viewModel = new Vue({
el: '#demo-app',
data: {
WelcomeMsg: 'Hello, My Name Is Anil Singh.'
},
methods: {
IsEnable: function() {
return true;
}
}
});
Example 1 - VueJs 2 v-if conditions
HTML Code -
<script src="https://unpkg.com/vue"></script>
<div id="demo-app">
<p v-if="IsEnable()">{{ WelcomeMsg }}</p>
</div>
VueJs Code -
var app = new Vue({
el: '#demo-app',
data: {
WelcomeMsg: 'Hello, My Name Is Anil Singh.'
},
methods: {
IsEnable: function() {
return true;
}
}
});
Result looks like - Hello, My Name Is Anil Singh.
Example 2 – VueJs 2 v-for loop -
HTML Code –
<script src="https://unpkg.com/vue"></script>
<div><h2>v-for loop</h2></div>
<div id="forLoop">
<ol>
<li v-for="user in users">
{{ user.name }} , Age - {{ user.age }}
</li>
</ol>
</div>
VueJs 2 Code –
var app = new Vue({
el: '#forLoop',
data: {
users: [
{id:1, name: 'Anil Singh', age: 32},
{id:2, name: 'Aradhya Singh' , age: 4},
{id:3, name: 'Reena Singh' , age: 25},
{id:4, name: 'Sunil', age: 27 }
]
}
});
The Result looks like –
v-for loop -
ü Anil Singh , Age - 32
ü Aradhya Singh , Age - 4
ü Reena Singh , Age - 25
ü Sunil , Age – 27
What Is Vuejs 2 Lifecycle?
ü beforeCreate
ü created
ü beforeMount
ü mounted
ü beforeUpdate
ü updated
ü activated
ü deactivated
ü beforeDestroy
ü destroyed
The beforeCreate () method is called synchronously after the VueJs instance has just been initialized.
The created () method is called synchronously after the VueJs instance is created.
The BeforeMount () method is called right before the component is mounted.
The Mounted () method is called after the component has just been mounted
The BeforeUpdate () method is method is called when the data changes, before the virtual DOM is re-rendered.
The Updated () method is called after a data change.
The Activated () method is called when a kept alive component is activated.
The Deactivated () method is called when a kept alive component is deactivated.
The BeforeDestroy () method is called right before a VueJs instantiated.
The Destroyed () method is called after a VueJs instance has been destroyed.
What are the Advantages/Disadvantages of Vuejs?
Vuejs Advantages -
ü Easy for applications and interfaces development
ü Support Two-way communication as like AngularJs
ü Ability to control the states
ü Natural thought process
Vuejs Disadvantages-
ü Limited scope
ü Single creator
ü Small developer community
What Is VueJs Routing? How to achieve it?
There is no any built-in features for router but we will achieve using below -
<script src="https://unpkg.com/vue@2.5.5/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
Install the vue-router using command –
npm install vue-router
Using Repository from GitHub –
git clone https://github.com/vuejs/vue-router.git node_modules/vue-router
cd node_modules/vue-router
npm install
npm run build
How To Use filters in Vue.js 2?
HTML Code –
<script src="https://unpkg.com/vue"></script>
<div id="demo-app">
<label>Enter name </label>
<input type="text" v-model="name" id="name" name="name" />
<h4>My Name is - {{ name }} and my age is - {{ age }} years. </h4>
</div>
VueJs Code –
//Model class
var userModel = {
id:1,
name: "Anil Singh",
age: 24
};
//View Model Class
var userViewModel = new Vue({
el: '#demo-app',
data: userModel
});
Result looks here - https://jsfiddle.net/r6hhcvg7/
How To CREATE Custom filters in Vue.js 2?
HTML Code –
<script src="https://unpkg.com/vue"></script>
<div id="demo-app">
<ul>
<li v-for="customer in customers">
{{customer.id}} {{ customer.name }} and Age Are ({{customer.age}}) Years
</li>
<ul>
<div>
VueJs Code –
//Model class
var customerModel = {
name: "Anil Singh",
age: 32,
customers: [
{ id:1, name: "code-sample.com", age: 5400 },
{ id:2, name: "code-sample.xyz", age: 4540 },
{ id:3, name: "code-view.com", age: 4520 }
]
};
//Customer View Model.
var customerViewModel = new Vue({
el: '#demo-app',
data: customerModel
});
The result looks here – https://jsfiddle.net/ofubzd6w/
How To Handle Events in Vue.js?
HTML Code –
<script src="https://unpkg.com/vue"></script>
<div id="demo-app">
<input type="text" v-model="name">
<button v-on:click="myClickMethod">Click Me? </button>
<div>
VueJs Code –
//Model class
var customerModel = {
name: "Anil Singh",
age: 32
};
//Customer View Model.
var customerViewModel = new Vue({
el: '#demo-app',
data: customerModel,
// A click handler inside methods
methods: {
myClickMethod: function() {
alert("Hello " + this.name);
}
}
});
The Result looks here - https://jsfiddle.net/erttwer6/
What is VueJs Life-cycle Diagram?
The below is a diagram for the instance life-cycle and this is refer from the vuejs.org guide.
The VueJs Life-cycle Diagram looks like –
How to CREATE Constants in Vuejs?
//Creating a Constant
export const base_URL = 'https://code-sample.com/'
//Importing a Constant
import {base_URL} from '../app/base/constants.js';
What Is Computed Property and how Differs from method in VueJs?
GET/SET Computed Property looks like-
computed :{
AddUser : {
get : function() {
return this.firstName+" "+this.lastName;
},
set : function(name) {
var fname = name.split(" ");
this.firstName = fname[0];
this.lastName = fname[1]
}
}
}
Example for Computed methods-
<html>
<head>
<title>VueJs - Get/Set in Computed Properties</title>
<script src="https://unpkg.com/vue@2.5.5/dist/vue.js"></script>
</head>
<body>
<div id = "computedProperty">
Full Name - <input type = "text" v-model = "fullname" />
<h4>My Name is - {{firstName}} {{lastName}} and my Age is - {{ age}} years.</h4>
</div>
</body>
<script type = "text/javascript">
var vm = new Vue({
el: '#computedProperty',
data: {
firstName : "Anil",
lastName : "Singh",
age :32
},
methods: {
addUser: function(e){
alert("Add Method Called!");
}
},
computed :{
fullname : {
get : function() {
return this.firstName+" "+this.lastName;
}
}
}
});
</script>
</html>
The Result looks here - https://jsfiddle.net/k2mcn6b7/
Full Name -
My Name is - Anil Singh and my Age is - 32 years.
I hope you are enjoying with this post! Please share with you friends. Thank you!!