Vue.js

Vue.js interview questions

Prepare for your Vue.js interview with these common questions and answers.

01What is Vue.js?

Vue.js is a progressive, open-source JavaScript framework for building user interfaces. It is designed from the ground up to be incrementally adoptable, and its core library is focused on the view layer only.

02What are the lifecycle hooks in Vue?

Each Vue component instance goes through a series of initialization steps when it's created. Lifecycle hooks are functions that give users the opportunity to add their own code at specific stages. Common hooks include:

  • beforeCreate, created
  • beforeMount, mounted
  • beforeUpdate, updated
  • beforeUnmount, unmounted
03What is the difference between `v-if` and `v-show`?

v-if is 'real' conditional rendering because it ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles. It has higher toggle costs.

v-show is much simpler - the element is always rendered and remains in the DOM; v-show only toggles the display CSS property of the element. It has higher initial render costs.

04What are computed properties?

Computed properties are used to declaratively describe a value that depends on other values. They are cached based on their reactive dependencies. A computed property will only re-evaluate when some of its reactive dependencies have changed.

05What is the difference between computed properties and methods?

Computed properties are cached. They only re-evaluate when a dependency changes. Use them for data that depends on other data.

Methods are just normal functions. They will always run whenever a re-render happens. Use them when you don't need caching or when you need to pass arguments.

06What are watchers?

A watcher (watch option) is a more generic way to react to data changes. It's most useful when you want to perform asynchronous or expensive operations in response to changing data. It gives you more control than a computed property.

07What is `v-model`?

v-model is a directive that creates two-way data binding on form input, textarea, and select elements. It automatically picks the correct way to update the element based on the input type.

08How do you pass data from a parent to a child component?

You use props. In the parent, you bind the data to an attribute on the child component tag. In the child, you declare the prop in the props option.

09How do you pass data from a child to a parent component?

You use custom events. The child component emits an event using $emit('eventName', data). The parent component listens for this event using @eventName or v-on:eventName and runs a method to handle it.

10What are slots?

Slots are a mechanism for content distribution. They allow you to compose components in a way that is more flexible. A <slot> outlet in a child component allows the parent to pass template fragments into it.

11What is Vuex?

Vuex is a state management pattern and library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion. It's similar to Redux in React.

12What are the core concepts of Vuex?

The core concepts are:

  • State: The single source of truth.
  • Getters: Computed properties for the store.
  • Mutations: The only way to change the state. They must be synchronous.
  • Actions: Similar to mutations, but they can contain asynchronous operations and they commit mutations.
13What is Vue Router?

Vue Router is the official router for Vue.js. It deeply integrates with Vue.js core to make building Single Page Applications with Vue.js a breeze. Features include nested routes, programmatic navigation, and transition effects.

14What are mixins?

Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component uses a mixin, all options in the mixin will be 'mixed' into the component's own options.

15What is the Composition API?

The Composition API is a set of APIs that allows us to author Vue components using imported functions instead of declaring options. It was introduced in Vue 3 to address the limitations of the Options API for large components and to improve logic reuse. Key features are setup, ref, and reactive.

16What is the difference between `ref` and `reactive`?

ref takes a value and returns a reactive and mutable ref object. The object has a single property .value that points to the inner value. It can hold any value type.

reactive takes an object and returns a reactive proxy of the original object. It only works for objects (including arrays and Maps/Sets).

17What are directives in Vue?

Directives are special attributes with the v- prefix. Directive's job is to reactively apply side effects to the DOM when the value of its expression changes. Examples: v-if, v-for, v-bind, v-on.

18How can you create a custom directive?

You can register custom directives globally using app.directive() or locally in a component's directives option. A directive definition object can provide several hook functions (e.g., created, mounted, updated).

19What is the `key` attribute used for?

The key attribute is a special attribute that is primarily used as a hint for Vue's virtual DOM algorithm to identify vnodes when diffing the new list of nodes against the old list. It helps Vue to track each node’s identity, and thus reuse and reorder existing elements.

20What is the difference between `v-bind` and `v-model`?

v-bind (or :) is for one-way data binding. It binds a component's data to a DOM element's attribute.

v-model is for two-way data binding on form elements. It's essentially syntax sugar for v-bind:value and v-on:input.

21What is the Options API?

The Options API is the traditional way of writing Vue components. We organize our component's logic using a set of options like data, methods, computed, and mounted. This works well for small to medium-sized components.

22What is a Single File Component (SFC)?

A Single File Component is a file with a .vue extension that encapsulates the HTML template, JavaScript logic, and CSS styling of a Vue component in a single file. This is the recommended way to author Vue components.

23How does reactivity work in Vue?

Vue uses a reactivity system that tracks dependencies. When you declare a piece of data in data or with ref/reactive, Vue wraps it in a proxy. When this data is accessed (read), Vue records it as a dependency. When the data is changed (written), Vue notifies all the dependencies to re-render.

24What is the purpose of the `nextTick` function?

Vue.nextTick(callback) defers the callback to be executed after the next DOM update cycle. It's used when you want to perform an action after the DOM has been updated with the latest data changes.

25What are dynamic components?

Dynamic components allow you to switch between components at the same mount point. This is done using the special <component> element and binding its is attribute to a component name or object.

26What are props validations?

Vue allows you to specify requirements for the props a component receives. You can define the type, whether it's required, a default value, and even a custom validator function. This helps prevent bugs and makes components more robust.

27What is the provide/inject API?

Provide and inject are used for dependency injection. They allow an ancestor component to serve as a dependency injector for all its descendants, regardless of how deep the component hierarchy is. This is another way to avoid prop drilling.

28What is Pinia?

Pinia is the new official state management library for Vue, recommended for Vue 3 applications. It's simpler, more lightweight, and has better TypeScript support than Vuex. It's considered the successor to Vuex.

29How can you optimize performance in a Vue app?

Several ways:

  • Use v-if instead of v-show for components with high toggle cost.
  • Use keep-alive to cache components.
  • Use functional components.
  • Use code splitting and lazy loading for routes and components.
  • Virtualize long lists.
30What is the difference between `v-html` and `v-text`?

v-text updates the element's textContent. It treats the data as plain text.

v-html updates the element's innerHTML. It's used to render raw HTML. Using it can be dangerous as it can easily lead to XSS vulnerabilities if the content is user-provided.

Next step

Preparing for a role?

Tell us what you’re aiming for. A mentor can help you close the gaps before the interview.