Skip to main content

25 Best Vue.js 2 Interview Questions and Answers

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 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:1name: 'Anil Singh'age: 32},
      {id:2name: 'Aradhya Singh' , age: 4},
      {id:3name: 'Reena Singh' , age: 25},
      {id:4name: '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:1name: "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!!
By Anil Singh | Rating of this article (*****)

Popular posts from this blog

List of Countries, Nationalities and their Code In Excel File

Download JSON file for this List - Click on JSON file    Countries List, Nationalities and Code Excel ID Country Country Code Nationality Person 1 UNITED KINGDOM GB British a Briton 2 ARGENTINA AR Argentinian an Argentinian 3 AUSTRALIA AU Australian an Australian 4 BAHAMAS BS Bahamian a Bahamian 5 BELGIUM BE Belgian a Belgian 6 BRAZIL BR Brazilian a Brazilian 7 CANADA CA Canadian a Canadian 8 CHINA CN Chinese a Chinese 9 COLOMBIA CO Colombian a Colombian 10 CUBA CU Cuban a Cuban 11 DOMINICAN REPUBLIC DO Dominican a Dominican 12 ECUADOR EC Ecuadorean an Ecuadorean 13 EL SALVADOR

39 Best Object Oriented JavaScript Interview Questions and Answers

Most Popular 37 Key Questions for JavaScript Interviews. What is Object in JavaScript? What is the Prototype object in JavaScript and how it is used? What is "this"? What is its value? Explain why "self" is needed instead of "this". What is a Closure and why are they so useful to us? Explain how to write class methods vs. instance methods. Can you explain the difference between == and ===? Can you explain the difference between call and apply? Explain why Asynchronous code is important in JavaScript? Can you please tell me a story about JavaScript performance problems? Tell me your JavaScript Naming Convention? How do you define a class and its constructor? What is Hoisted in JavaScript? What is function overloadin

React | Encryption and Decryption Data/Text using CryptoJs

To encrypt and decrypt data, simply use encrypt () and decrypt () function from an instance of crypto-js. Node.js (Install) Requirements: 1.       Node.js 2.       npm (Node.js package manager) 3.       npm install crypto-js npm   install   crypto - js Usage - Step 1 - Import var   CryptoJS  =  require ( "crypto-js" ); Step 2 - Encrypt    // Encrypt    var   ciphertext  =  CryptoJS . AES . encrypt ( JSON . stringify ( data ),  'my-secret-key@123' ). toString (); Step 3 -Decrypt    // Decrypt    var   bytes  =  CryptoJS . AES . decrypt ( ciphertext ,  'my-secret-key@123' );    var   decryptedData  =  JSON . parse ( bytes . toString ( CryptoJS . enc . Utf8 )); As an Example,   import   React   from   'react' ; import   './App.css' ; //Including all libraries, for access to extra methods. var   CryptoJS  =  require ( "crypto-js" ); function   App () {    var   data

Encryption and Decryption Data/Password in Angular

You can use crypto.js to encrypt data. We have used 'crypto-js'.   Follow the below steps, Steps 1 –  Install CryptoJS using below NPM commands in your project directory npm install crypto-js --save npm install @types/crypto-js –save After installing both above commands it looks like  – NPM Command  1 ->   npm install crypto-js --save NPM Command  2 ->   npm install @types/crypto-js --save Steps 2  - Add the script path in “ angular.json ” file. "scripts" : [                "../node_modules/crypto-js/crypto-js.js"               ] Steps 3 –  Create a service class “ EncrDecrService ” for  encrypts and decrypts get/set methods . Import “ CryptoJS ” in the service for using  encrypt and decrypt get/set methods . import  {  Injectable  }  from   '@angular/core' ; import   *   as   CryptoJS   from   'crypto-js' ; @ Injectable ({    providedIn:   'root' }) export   class   EncrDecrS