Vue.js Interview Questions

Below are the list of Best Vue.js Interview Questions and Answers
Virtual DOM in Vue is a JavaScript object that represents the Document Object Model (DOM). The application updates the Virtual DOM instead of the DOM directly. So, it minimizes the updating cost of the real DOM as it is computationally expensive. Virtual DOM offers the ability to control the timing at which the Virtual DOM is rendered. Virtual DOM will just maintain the state of the data without re-rendering until you choose it. Virtual DOM also offers the ability to optimize the performance of your web applications by minimizing the number of times the DOM has to be updated.
Mixins in Vue JS are a chunk of defined logic that is stored in a particular way. It can be re-used over and over to add functionality to your Vue instances and components. It is important that we need Vue JS because,
- You can easily adhere to the DRY principle with mixins. It ensures that you do not repeat yourself.
- You get a lot of flexibility with mixins. Mixin contains options for Vue components.
- Mixins are safe and they do not affect changes outside their defined scope.
- Mixins in Vue JS are a great platform for code reusability.
VueX is a state management pattern and library for the application using Vue JS. it acts as a centralized store for all the different components in your Vue JS application. It has rules to ensure that the state can be only mutated in a predictable fashion. It can be integrated with the official devtool extension of Vue to provide additional features. Vuex mainly helps in dealing with shared state management with the cost of more concepts and boilerplate.
Filters in Vue JS helps in applying common text formatting. It is used in two places, mustache interpolations, and v-bind expressions. It mainly filters the data on the DOM level. So you get data that is still intact in the storage but is represented in the custom specified manner. It enhances the presentation of the view layer. The filters are also reusable. You can declare a filter globally and use it on any desirable component. It gives you the power to format your data at the view level.
Components in Vue JS are a single, independent unit of an interface. They have their own state, markup, and style.
A Vue component can be defined in four ways.
- The first is new Vue({ /*options */ }).
- The second is Vue.component(‘component-name’, { /* options */ }).
- The third way is by using the local components.
- The fourth is in the .vue files or Single File Components.
The first two ways are the standard ways to use Vue when building an application that is not a SPA (Single Page Application). The Single File Components are uses in the Single Page Application.
There are two ways to import a JavaScript library to the Vue Component.
The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file.
import * as mykey from '../assets/js/mykey.js';
The second way is to include your external JavaScript file into the mounted hook of your Vue component.
We can use various HTTP libraries to call REST Api's from Vue JS. One of the popular libraries is Axios. It simple to use and lightweight. To include it in your project, execute the following command.
npm install axios --save
Implementing GET method using Axios in Vue JS
axios({ method: "GET", "URL": "https://httpbin.org/ip" }).then(result => { this.ip = result.data.origin; }, error => { console.error(error); });
We can send an HTTP request using Axios with a promise. If the request is successful, we’ll get the result.
- Templates
- Reactivity
- Components
- Transitions
- Routing
– for example, it needs to set up data observation, compile the template, and create the necessary data bindings. Along the way, it will also invoke some lifecycle hooks, which give us the opportunity to execute custom logic. For example, the created hook is called after the instance is created:
new Vue({ data: { a: 1 }, created: function () { // `this` points to the vm instance console.log('a is: ' + this.a) } }) // => "a is: 1"
There are also other hooks which will be called at different stages of the instance’s lifecycle, for example compiled, ready and destroyed. All lifecycle hooks are called with their this context pointing to the Vue instance invoking it. Some users may have been wondering where the concept of “controllers” lives in the Vue.js world, and the answer is: there are no controllers in Vue.js. Your custom logic for a component would be split among these lifecycle hooks.
Lifecycle Diagram
Below diagram shows complete life cycle of Vue Instance
Source: https://v1.vuejs.org/guide/instance.html#Instance-Lifecycle
Also, Read React js Interview questions
var vm = new Vue({ // options })
In Vue js v-bind is used for one-way data flow or binding.
In two-way data binding the view(UI) part of application automatically updates when data Model is changed.
In Vue.js v-model directive is used for two way data binding.
In below example you can see how Two-Way Bindings is implemented.
<div id="app"> {{message}} <input v-model="message"> </div> <script type="text/javascript"> var message = 'Vue.js is rad'; new Vue({ el: '#app', data: { message } }); </script>
A Vue.js filter is essentially a function that takes a value, processes it, and then returns the processed value. In the markup it is denoted by a single pipe (|) and can be followed by one or more arguments:
<element directive="expression | filterId [args...]"></element>
In Vue 2.0, there are no built-in filters are availables, however you are free to create your own filters.
Vue.filter('reverse', function (value) { return value.split('').reverse().join('') })
Following is the way to register a Vue component inside other component
export default { el: '#your-element' components: { 'your-component' } }
Below are list of commonly used directives in Vue.js
- v-show
- v-if
- v-model
- v-else
- v-on
- General Directives
- Literal Directives
- Empty Directives
- Custom Directives
You can install it via yarn or NPM.
$ yarn add vue-resource $ npm install vue-resource
Example:
Creating a Constant in Vue js.
export const SITE_URL = 'https://www.onlineinterviewquestions.com';
Importing a Constant in Vue js.
import {SITE_URL} from './path/to/constants.js';
Related Interview Questions-
JavaScript Interview Questions
-
JavaScript Closure Interview Questions
-
Node JS Interview Questions with Express
-
AngularJs
-
Ajax Interview Questions
-
Aurelia Interview Questions
-
Backbone js Interview Questions
-
D3.js interview questions
-
Ecmascript 2017 Interview Questions
-
Emberjs Interview questions
-
ES6 Interview Questions
-
Ext js Interview Questions
-
Grunt js Interview questions
-
Gulp Js interview questions
-
Handlebars js interview questions
-
JQuery Interview Questions
-
JSON Interview Questions
-
Knockout js Interview Questions
-
Koa Js Interview questions
-
Less.js interview questions
-
Marionette js interview questions
Subscribe Our NewsLetter
Never Miss an Articles from us.
Featured Categories
- Common Interview Questions
- Python Flask Interview Questions
- NoSQL interview questions
- JQuery Interview Questions
- C programming interview questions
- AngularJS Interview Questions
- Node JS Interview Questions with Express
- JavaScript Interview Questions
- Core Java interview questions
- HTML Interview Questions
- Laravel interview questions
- Wordpress Interview Questions
- PHP Interview Questions