Most Important Vue.js Interview Questions with Answers
1) What is Vue.js?/ What do you understand by Vue.js?
2) Why is Vue.js called a progressive framework?
Vue.js called a progressive framework because it is being changed and developed continually.
3) Why is Vue.js supposed to be a competitor of Angular in upcoming days?
Vue.js is also used to build User Interfaces and single-page applications like Angular. Nowadays, it is evolving very fast, and with time, new libraries and extensions are coming into existence.
4) Who is the founder/ inventor of Vue.js?
Evan you is the founder of Vue.js. He was working at Google on several Angular projects when he founded Vue.js.
5) What is the VUE-resource? How would you install the Vue-Resource?
The VUE-resource is a plug-in for Vue.js. This plug-in is used with Vue.js to make web requests and handle responses, in which XHMLHttpRequests or JSONP is used.
You can use the following yarn or npm command to install VUE-resource:
6) How can you install Vue.js in your project?
You can install Vue.js in your project by using the following 4 methods:
7) How can you create an instance of Vue.js?
You can create a new Vue instance by using the Vue function:
You have to create a new Vue instance when you want to start a Vue application.
8) What are the advantages of using Vue.js?
Following is the list of advantages of using Vue.js:
Very Small In Size
Easy to Understand and Use
The framework of Vue.js is very easy to understand, and it id one of the reasons for the popularity of this framework. The users can easily add Vue.js to their web project because of its simple structure and develop applications.
Simple Integration with Existing Applications
Flexible in nature
You can create reusable custom elements in Vue.js applications.
Easy & comprehensive documentation
The documentation of Vue.js is very easy and comprehensive so that developers can develop applications or web pages, only having little knowledge about HTML.
Vue.js uses virtual DOM similar to other existing frameworks such as ReactJS, Ember, etc. Virtual DOM is a light-weight in-memory tree representation of the original HTML DOM and updated without affecting the original DOM.
Vue.js provides two-way communications with its MVVM architecture that makes it very easy to handle HTML blocks.
9) Name some websites which are using Vue.js?
Following is the list of some websites using Vue.js on parts of their projects and applications:
10) What is the difference between one-way data flow/ or one-way data binding and two-way data binding?
In one-way data binding or one-way data flow, the view (UI) part of the application does not update automatically. In this model, when the data Model is changed, you need to write some custom code to make it updated every time after the change. The v-bind directive is used for one-way data flow or binding in Vue.js.
On the other hand, in two-way data binding, the view (UI) part of the application is automatically updated when the data Model is changed. The v-model directive is used for two way data binding in Vue.js.
11) How can you create Two-Way Bindings in Vue.js?
The v-model directive is used to create Two-Way Bindings in Vue js. In Two-Way Bindings, data or model binds with DOM, and Dom binds back to the model.
Let's see an example to demonstrate how Two-Way Bindings is implemented.
12) What do you understand by components props in Vue.js?
In Vue.js, every component instance has its own isolated scope. So, you cannot directly reference parent data in a child component's template.
Props are used to pass down data to the child components. Props are custom attributes. You can register on a component. When a value is passed to a prop attribute, it becomes a property on that component instance.
13) What is the requirement of Mixins in Vue.js?
Mixins in Vue.js are a set of defined logic that is stored in a particular way. Mixins can be re-used repeatedly to add functionality to your Vue instances and components. Mixins are important because they provide a lot of functionalities. Following is the list of features that Mixins provide:
14) What is Vuex?
VueX is a state management pattern and library for the Vue.js application. It is used as a centralized store for all the different components in the Vue.js application. Vuex provides some rules to ensure that the state can only be mutated in a predictable fashion. You can get a lot of additional features by integrating Vuex with the official devtool extension of Vue.js.
15) What are filters in Vue.js?
The Filters are functionality provided by Vue.js components that allow you to apply formatting and transformations to your dynamic template data. Filters are used in two places, mustache interpolations, and v-bind expressions. Filters don't change a component data or anything, but they only affect the output.
16) What are the main usages of filters in Vue.js?
Following is the list of main usages of filters in Vue.js:
17) Can we call Rest API from Vue.js? How?
Yes, we can call Rest API from Vue.js. There are several HTTP libraries that can used to call REST Api's from Vue.js. One of the popular libraries is Axios. It is very simple to use and lightweight. You can include it in your project by using the following command.
18) How can you deploy the Vue.js application?
You can create a project by using the following command:
To run your project, run the following command:
After executing the above command, copy index.html and /dist/ folder into your website root directory, and your project will be deployed.
19) What is Vue-loader in Vue.js?
The Vue-loader is a loader module for webpack in Vue.js that is used to write single file components using the .vue file format.
The single-file component contains three sections called template, script, and style. The webpack can extract and process each section using separate loader modules such as the SASS or SCSS loaders. The vue-loader module makes static assets to be treated as module dependencies and enables processing using webpack loaders.
20) How can you handle Events in Vue.js?
See the following example which demonstrates how to handle Events in Vue.js:
21) What are the most commonly used Directives in Vue.js?
Following is the list of most commonly used directives in Vue.js:
22) What is the difference between v-show and v-if directives?
The most common differences between v-show and v-if directives are given below:
23) What is the key in Vue.js?
To render DOM elements more efficiently, Vue.js reuses the elements instead of creating a new element every time. The key is a kind of flag that tells Vue "if the data associated with this child component is moved somewhere else, then move the component along with it to preserve the changes that already exist."
24) Why is it recommended not to use v-if and v-for directives together on the same element in Vue.js?
It is recommended not to use v-if and v-for directives together on the same element because the v-for directive has a higher priority than v-if directive. If you use both directives together, then there are two common cases where this may be tempting:
25) How many types of the directive are used in Vue.js?
The following types of directives are used in Vue.js:
26) What is a single-file component in Vue.js?
In Vue.js, a single-file component is a file with a .vue extension that contains a Vue component. The single-file component consists of the component's template, logic, and styles, all bundled together in one file. It also contains one <script> block, optional <template> and <style> blocks, and possible additional custom blocks.
27) How to use a single-file component in Vue.js?
To use a single-file component in Vue.js, we have to set up Vue Loader for parsing the file (It is done automatically as a part of a webpack building pipeline). It also supports non-default languages such as Sass or HTML templating languages with pluggable pre-processors.
28) What is $parent property in Vue.js?
In Vue.js, the $parent property is used to access the parent instance from a child. It is similar to the $root property. The $parent property provides direct access, but it makes the application hard to test and debug. In this property, it is very difficult to find out where the mutation comes from.
29) What is $child property in Vue.js?
In Vue.js, the $child property is just like $parent property, but it is used to access the child instance.
30) What is the usage of ref in Vue.js?
The ref if an attribute that is used to access a component directly. Despite having the props and events, if you want to access a child component directly, you can assign a reference ID to the child component using the ref attribute.
Now you can use this.$refs.usernameInput where we have to define this ref in the component.
You should avoid accessing $refs from within templates or computed properties because $refs are only populated after the component has been rendered, and they are not reactive.
31) What are the most common cause of memory leaks in Vue.js apps, and how can they be solved?
In Vue.js applications, memory leaks often come from using third-party libraries that create their own instances and/or manipulate the DOM. The v-if directive and the Vue Router destroy Vue component instances. To overcome this issue, do a cleanup action before the component gets destroyed. It should be done manually in the beforeDestroy() lifecycle hook.
For example, suppose we have a fictional library named PowerGraph.js, inside our component. It creates a graph instance that displays some data on the page:
Here, we have to call the graph instance's destroy() method or implement our own cleanup method:
If we don't do cleanup action before our component gets destroyed, then that memory will never be released, and this will be a memory leak.
32) Explain the all life cycle events or hooks in Vue instance in Vue.js. / Explain the Life cycle of Vue Instance with diagram.
When a Vue instance is created in Vue.js, it goes through a series of steps after creation. First, they are created then mounted and after that destroyed at the end. In this process, it also runs functions known as life cycle hooks. These life cycle hooks allow the developers to add their own code at a specific stage.
Following is the list of all events or hooks a Vue instance goes through:
beforeCreate event: This is the first event or hook that occurs in the creation process. It facilitates developers to perform actions even before the component has been added to the DOM. We cannot access the DOM inside of this event.
created event: This event is used to run the code after creating the instance. It facilitates you to access the reactive data, but the mounting or rendering of templates and Virtual DOM is not completed yet.
beforeMount event: The beforeMount event is used to execute just before the initial render happens and after the template or render functions have been compiled. This is the rarely used event, and in most cases, you don't need to use this event.
mounted event: This is the most frequently used event or hook. In this event, you have full access to the reactive component, templates, and rendered DOM.
beforeUpdate event: This event is executed just before the data changes on the component and the update cycle's start. It runs right before the DOM is patched and re-rendered.
updated: This event is used to execute after the data changes on the component and the DOM re-renders. If you want to access the DOM after a property change, it is the best place to complete this action.
beforeDestroy: This event is used to execute just before tearing down the instance. This is the second last step of the Vue Instance life process and is the right place to clean up events or reactive subscriptions if you have to do this.
destroyed: This is the last step of the Vue Instance life process and used to do any last minute clean up.
Lifecycle Diagram of the Vue Instance
The following diagram shows the complete life cycle of Vue Instance:
33) What is Vue Router? What do you understand by Vue Router?
The official router of Vue.js is called Vue Router. It is by default integrated with Vue.js core and used to build Single Page Applications with Vue.js.
Following is the list of important features of Vue Router:
34) What is the difference between slots and scoped slots in Vue.js?
In Vue.js, a slot is a placeholder in a child component filled with content passed from the parent component.
In the slot, the component is compiled in the parent's scope and then passed to the child component. So, it is not possible to use child component properties in a slot's content.
In Scoped slot, you can pass child component data to the parent scope and then use it in slot content.
35) What is the virtual DOM in Vue.js?
36) What is the main purpose of using virtual DOM in Vue.js? / What is the benefit of using virtual DOM in Vue.js?
37) How does data flow between components in a Vue.js app?
In Vue.js, the data is passed to child components from the parent component using a prop or a custom attribute. This custom attribute becomes a property on the child component instance. This procedure is called a one-way data flow.
Once the parent component updates a prop value, the child component is automatically updated. The child component can communicate back to the parent via an event, but mutating a property inside a child component should not be done. It does not affect the parent component also unless it is an object or array. When the child component instance emits an event, the parent assigns a handler to that event, and data is passed back to the parent.