Rates TodayNIFTY25,670-0.83%SENSEX83,630.7-0.83%Gold 24K ₹11,320.24/g down186.46Silver ₹134.77/g down2.72
InsightforGeeks

Vue.js interview questions and answers

25 questions · Easy, Medium and Hard

These Vue.js questions focus on Vue 3 and the Composition API, which is what most teams use today. They cover the questions interviewers ask most: templates and directives, reactivity, components, routing, state management and performance.

Each answer is short enough to say in an interview, with a small example where it helps. For the harder questions, explaining how Vue works, not only how to use it, is what stands out.

Easy 9Medium 10Hard 6

Easy questions

What is Vue.js?

Vue is a progressive JavaScript framework for building user interfaces. You describe the UI with templates and components, and Vue updates the page automatically when your data changes. "Progressive" means you can add it to one part of an existing page, or use it to build a full single-page app with routing and state management.

Often asked next: How is Vue different from React?

What is a single-file component?

A single-file component is a .vue file that keeps a component's template, logic and styles together. It has up to three blocks: <template> for the markup, <script> (usually <script setup>) for the logic, and <style> for the CSS, which can be scoped so it only affects that component.

<script setup>
import { ref } from 'vue';
const count = ref(0);
</script>

<template>
  <button @click="count++">Clicked {{ count }} times</button>
</template>

<style scoped>
button { padding: 8px 16px; }
</style>

What is the difference between the Options API and the Composition API?

The Options API organises a component by option type: data, methods, computed, watch. The Composition API organises it by feature, using functions such as ref, computed and watch inside setup() or <script setup>. The Composition API makes it easier to group related logic, reuse it in composables, and use TypeScript. The Options API still works and is fine for small components.

New projects normally use the Composition API with <script setup>.

Vue.js learning hub →

What are the most common directives in Vue?

v-bind (shortened to :) binds an attribute to data, v-on (shortened to @) listens to events, v-model creates two-way binding on form inputs, v-if and v-else render conditionally, v-show toggles visibility with CSS, and v-for renders a list. Directives are special attributes that start with v- and apply behaviour to an element.

<input v-model="name" :placeholder="hint" @keyup.enter="save" />
<p v-if="name">Hello, {{ name }}</p>
<li v-for="item in items" :key="item.id">{{ item.title }}</li>

What is the difference between v-if and v-show?

v-if adds or removes the element from the DOM, and also creates and destroys the components inside it. v-show always renders the element and only toggles its CSS display. Use v-show when you switch something on and off often, because it is cheaper to toggle. Use v-if when the condition rarely changes, or when the content should not exist at all until needed.

What is the difference between ref and reactive?

ref wraps any value, including primitives such as numbers and strings, in an object with a .value property. In templates the .value is unwrapped for you, but in JavaScript you write count.value. reactive makes an object or array reactive and you access its properties directly.

reactive has limits: you cannot replace the whole object without losing reactivity, and destructuring its properties into variables loses the connection. Many teams use ref everywhere for consistency.

import { ref, reactive } from 'vue';

const count = ref(0);
count.value++;

const user = reactive({ name: 'Asha', age: 30 });
user.age++;

Why is the key attribute needed in v-for?

key gives Vue a stable identity for each item, so when the list changes it can reuse, move or remove the right elements instead of patching them in place. Use a unique, stable value such as an ID. Using the array index as a key can cause wrong state or flicker when items are added, removed or reordered.

What are props and emits, and how does data flow between components?

Props pass data down from a parent to a child, and the child must not change them (one-way data flow). Emits let the child tell the parent that something happened, by sending an event the parent listens to. In <script setup> you declare them with defineProps and defineEmits.

<!-- Child.vue -->
<script setup>
const props = defineProps({ title: String });
const emit = defineEmits(['close']);
</script>

<template>
  <h2>{{ props.title }}</h2>
  <button @click="emit('close')">Close</button>
</template>

What are the lifecycle hooks in Vue 3?

In the Composition API they are functions you call in setup: onBeforeMount, onMounted (the component is on the page, good for fetching data or touching the DOM), onBeforeUpdate, onUpdated, onBeforeUnmount and onUnmounted (clean up timers and listeners). The Options API equivalents are mounted, unmounted and so on.

Medium questions

What is the difference between computed, methods and watch?

A computed property derives a value from other reactive data and caches it: it only recalculates when something it depends on changes. A method runs every time you call it and has no cache. watch runs a side effect, such as calling an API or saving to local storage, when a value changes.

Use computed for values you show or use in logic, and watch only for side effects.

const first = ref('Asha');
const last = ref('Nair');
const full = computed(() => `${first.value} ${last.value}`);

watch(first, (newName) => {
  console.log('First name changed to', newName);
});

What is the difference between watch and watchEffect?

watch needs you to name the source to watch, gives you the old and new values, and runs only when that source changes (not immediately, unless you set immediate: true). watchEffect runs at once and automatically tracks every reactive value it reads, re-running when any of them changes. Use watch when you need control or the old value, and watchEffect for simple effects that depend on several values.

What are slots?

Slots let a parent pass template content into a child component, which makes components flexible. The default slot takes content between the component's tags; named slots let you fill several places; scoped slots let the child pass data back to the content the parent supplies.

<!-- Card.vue -->
<template>
  <div class="card">
    <header><slot name="title" /></header>
    <slot />
  </div>
</template>

<!-- Usage -->
<Card>
  <template #title>Profile</template>
  <p>Card content</p>
</Card>

What is a composable?

A composable is a function, usually named useSomething, that uses Vue's reactivity and lifecycle features to package reusable stateful logic, such as tracking the mouse, fetching data or handling a form. Each component that calls it gets its own state. Composables replace mixins, which were harder to follow because it was not clear where a property came from.

// useCounter.js
import { ref } from 'vue';

export function useCounter(start = 0) {
  const count = ref(start);
  const increment = () => count.value++;
  return { count, increment };
}

How does v-model work on a component?

On a native input, v-model binds the value and listens for input events. On a component it works the same way through a prop and an event. In Vue 3.4 and later, the defineModel() macro makes this short: it gives you a ref that is synced with the parent. Before that, you declared a modelValue prop and emitted update:modelValue.

<!-- TextField.vue -->
<script setup>
const model = defineModel();
</script>
<template>
  <input v-model="model" />
</template>

<!-- Parent -->
<TextField v-model="email" />

What are provide and inject?

provide lets a component make a value available to all its descendants, and inject lets any descendant read it, without passing props through every level (avoiding prop drilling). It suits things such as a theme or a form context. Because the dependency is implicit, use it for shared plumbing, and prefer a store such as Pinia for application state.

How does Vue Router work, and what are navigation guards?

Vue Router maps URLs to components. You define routes (with dynamic segments such as /users/:id), show the matched component with <RouterView>, and link with <RouterLink>. Navigation guards run during a route change; router.beforeEach is commonly used to check login and redirect, and a route can also have its own beforeEnter. You can load route components lazily with () => import('./Page.vue') to keep the first download small.

const routes = [
  { path: '/', component: Home },
  { path: '/users/:id', component: () => import('./User.vue') },
];

router.beforeEach((to) => {
  if (to.meta.requiresAuth && !isLoggedIn()) return '/login';
});

What is Pinia, and why is it preferred over Vuex?

Pinia is the official state management library for Vue. A store holds shared state, getters (like computed values) and actions. Compared with Vuex it has no separate mutations, works naturally with the Composition API and TypeScript, and has a simpler, lighter API. Vuex is now in maintenance mode, and new projects should use Pinia.

import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

export const useCartStore = defineStore('cart', () => {
  const items = ref([]);
  const total = computed(() => items.value.reduce((sum, i) => sum + i.price, 0));
  function add(item) { items.value.push(item); }
  return { items, total, add };
});

What is nextTick, and when do you need it?

Vue updates the DOM asynchronously and batches changes. If you change data and immediately read the DOM, you still see the old version. nextTick returns a promise that resolves after the DOM has been updated, so you can safely read or measure it, for example to focus an input that has just been shown with v-if.

import { nextTick } from 'vue';

show.value = true;
await nextTick();
inputEl.value.focus();

What are Teleport and KeepAlive?

<Teleport> renders part of a component's template somewhere else in the DOM, such as at the end of <body>, which is useful for modals and tooltips that must escape a parent's styling or stacking. <KeepAlive> caches component instances when you switch between them, so their state is kept (for example, tabs that keep their form data) instead of being destroyed and recreated.

Hard questions

How does Vue 3's reactivity system work?

Vue 3 wraps objects in JavaScript Proxy objects. When code reads a reactive property, Vue records which effect (such as a render function or a computed) is running, tracking that dependency. When a property is changed, Vue triggers the effects that depend on it, so only the affected parts re-run.

Vue 2 used Object.defineProperty, which could not detect added or deleted properties or some array changes without special methods. The Proxy approach removed those limits.

Often asked next: Why does destructuring a reactive object lose reactivity?

What are the main differences between Vue 2 and Vue 3?

Vue 3 added the Composition API, Proxy-based reactivity, multiple root nodes in a template (fragments), <Teleport>, better TypeScript support, a smaller and faster core, and a rewritten compiler. It also changed some APIs, such as v-model on components, and removed filters. Vue 2 reached end of life on 31 December 2023, so new projects should use Vue 3.

How does Vue optimise rendering?

Vue's template compiler analyses templates ahead of time. It marks which parts of a template are static and hoists them so they are created once, and it adds patch flags to dynamic nodes so that, on update, Vue compares only the parts that can change instead of the whole tree. This compiler-informed virtual DOM is one reason Vue 3 is fast without developers writing manual optimisations.

How would you improve the performance of a slow Vue app?

Find the problem first with the browser's performance tools and Vue DevTools. Then: use computed instead of repeated method calls in templates; load route components and heavy components lazily (defineAsyncComponent); use shallowRef or shallowReactive for large objects that do not need deep tracking; virtualise very long lists; use v-once or v-memo for parts that rarely change; and avoid putting large amounts of data into reactive state unnecessarily.

What are the common pitfalls when using watch?

Watching a property of a reactive object needs a getter function, such as watch(() => user.name, ...), because passing user.name directly passes a plain value. Watching an object deeply needs { deep: true }, which can be costly on large data. Watchers also run after the DOM updates by default; use flush: 'pre' or 'sync' only when you must. And remember to avoid changing the watched value inside its own watcher, which can cause loops.

What is server-side rendering, and how does Nuxt help?

With server-side rendering (SSR), the server turns the Vue app into HTML for each request, so users and search engines see content immediately, then Vue "hydrates" the page to make it interactive in the browser. SSR improves first load and SEO but adds complexity, because code must run safely on the server, where there is no window or document. Nuxt is a framework built on Vue that provides SSR, file-based routing, data fetching and static generation out of the box.

Practise in code

Interviews often include a short coding task. These challenges run in your browser.

Keep learning

Handy free tools

More interview questions