Vue.js Interview Questions | Eklavya Online

Vue.js Interview Questions

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.

You can create a project by using the following command:

vue init webpack myproject
To run your project, run the following command:

npm run build
After executing the above command, copy index.html and /dist/ folder into your website root directory, and your project will be deployed.

You can install Vue.js in your project by using the following 4 methods:

Yu can use CDN by including tag in HTML file.
You can install Vue.js by using Node Package Manager (NPM).
You can install Vue.js using Bower.
You can also use Vue-cli to setup your project.

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 block, optional and blocks, and possible additional custom blocks.

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.

Vue.component(‘blog-post’, {
// camelCase in JavaScript
props: [‘postTitle’],
template: ‘

{{ postTitle }}


})

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.

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.

You can create a new Vue instance by using the Vue function:

var vm = new Vue({
// options
})
You have to create a new Vue instance when you want to start a Vue application.

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.

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:

  • Mixins facilitate you to easily adhere to the DRY principle and ensure that you do not repeat yourself.
  • Mixins provide great flexibility.
  • Mixin contains options for Vue components.
  • You can use Mixins in Vue.js safely because they do not affect changes outside their defined scope.
  • Mixins in Vue.js provide a great platform for code reusability.

Vue.js is a progressive framework of JavaScript used to create Dynamic User Interfaces and single-page applications.

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:

  • Vue Router follows a modular, component-based router configuration.
  • Very easy to implement.
  • You can customize the Scroll Behavior.
  • Provides Nested route/view mapping.
  • Provides fine-grained navigation control.
  • Route params, query, wildcards
  • js’ transition system provides View transition effects.
  • Links with automatic active CSS classes.
  • HTML5 history mode or hash mode, with auto-fallback in IE9.

Vue.js called a progressive framework because it is being changed and developed continually.

Following is the list of most commonly used directives in Vue.js:

  • v-show
  • v-if
  • v-model
  • v-else
  • v-on

Following is the list of advantages of using Vue.js:

Very Small In Size

One of Vue.js’ biggest advantages is that it is very small in size. This exciting JavaScript plug-in is only 18-21KB, so you can download and use it very easily in no time.

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

Vue.js framework can be integrated with the existing applications very easily. Vue.js has a lot of components for everything. You can integrate it with any application that is written in JavaScript.

Flexible in nature

The flexible nature of Vue.js also makes it easy to understand for the developers of React.js, Angular.js, and any other new JavaScript framework. It provides a lot of flexibility to use virtual nodes to write HTML files, JavaScript files, and pure JavaScript files.

Components

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.

Virtual DOM

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.

Two-Way Communication

Vue.js provides two-way communications with its MVVM architecture that makes it very easy to handle HTML blocks.

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.

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.

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.

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.

Many developers’ community is using Vue.js, and the popularity of this JavaScript language is increasing day by day. That’s why it is supposed to be a potential competitor of Angular.

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.”

Following is the list of some websites using Vue.js on parts of their projects and applications:

  • Grammarly
  • Netflix
  • Adobe
  • Facebook
  • Laracast
  • Behance
  • Gitlab
  • Euronews
  • Codeship
  • Livestorm
  • Xiaomi
  • Alibaba
  • Wizzair etc.

In Vue.js, the $child property is just like $parent property, but it is used to access the child instance.

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.

In Vue.js, the virtual DOM is a tree-like data structure or a collection of JavaScript objects that represents DOM nodes. Vue.js manage the nodes of the virtual DOM, and that should be rendered on the page. These objects are called “virtual nodes” or VNodes.

Evan you is the founder of Vue.js. He was working at Google on several Angular projects when he founded 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:

  1. When you have to filter items in a list (e.g. v-for=”user in users” v-if=”user.isActive”). In these cases, replace users with a new computed property that returns your filtered list (e.g., activeUsers).
  2. To avoid rendering a list if it should be hidden (e.g. v-for=”user in users” v-if=”shouldShowUsers”). In these cases, move the v-if to a container element (e.g., ul, ol).

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.

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.

Example:

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.

Following is the list of main usages of filters in Vue.js:

  • Filters are mainly used to filter the data on the DOM level to provide you the data that is still intact in the storage but is represented in the custom specified manner.
  • Filters are used to enhance the presentation of the view layer.
  • The filters are also reusable.
  • You can declare a filter globally and use it on any desirable component.
  • Filters facilitate you to format your data at the view level.

The virtual DOM’s main purpose is to make DOM manipulation faster and more efficient. It becomes very crucial when you have a lot of nodes in your DOM. In this case, updating these nodes is a very expensive task for processing power and resources required. Here, virtual DOM comes into action and makes JavaScript object significantly faster. Vue.js automatically organizes DOM updates in batches to enhance efficiency.

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:

$ yarn add vue-resource
$ npm install vue-resource

The following types of directives are used in Vue.js:

  • General Directives
  • Literal Directives
  • Empty Directives
  • Custom Directives

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.

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:

mounted() {
this.chart = new PowerGraph();
}
Here, we have to call the graph instance’s destroy() method or implement our own cleanup method:

beforeDestroy() {
this.chart.destroy();
}
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.

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.

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);
});