Top 50 Vue.js Interview Questions & Answers
Practise 50 Vue.js interview questions on Vue 3 reactivity, the Composition API, ref vs reactive, components, Vue Router, Pinia and performance.
Vue.js Fundamentals
Q1. What is Vue.js?
Vue.js is a progressive JavaScript framework for building user interfaces. It combines declarative templates with a reactivity system, and can be added to part of a page or used to build a full single-page app.
Q2. What does progressive framework mean?
You can adopt Vue gradually, starting with a script tag on one page and adding routing, state management and a build step only when the project needs them.
Q3. What are the main features of Vue 3?
The Composition API, a faster Proxy-based reactivity system, better TypeScript support, smaller bundles through tree-shaking, <Teleport>, <Suspense> and components with several root nodes.
Q4. What is a Single-File Component?
A .vue file that keeps a component's <template>, <script> and <style> together. Build tools such as Vite compile it into JavaScript.
Q5. What is the difference between the Options API and the Composition API?
The Options API organises code into data, methods and computed options. The Composition API uses functions like ref and computed inside setup, so related logic stays together and is easier to reuse.
Q6. What is <script setup>?
A compile-time shortcut for the Composition API. Top-level variables, functions and imported components are available in the template directly, with less boilerplate than a setup() function.
Q7. How do you create a Vue app?
Call createApp(App), register plugins such as the router with app.use(), then mount it with app.mount('#app'). New projects are usually created with npm create vue@latest.
Q8. What is the virtual DOM in Vue?
A JavaScript representation of the UI. When state changes, Vue renders a new virtual tree, compares it with the previous one and updates only the real DOM nodes that changed.
Q9. How does the Vue compiler optimise templates?
It marks static parts of a template so they are skipped when updating, and flags which bindings are dynamic. Vue then compares only the parts that can change.
Q10. How is Vue different from React?
Vue uses HTML-based templates and tracks reactive dependencies automatically, so components update without manual memoisation. React uses JSX and re-renders a component when its state changes.
Reactivity & the Composition API
Q11. How does reactivity work in Vue 3?
Vue wraps state in JavaScript Proxies. Reading a property records which effect depends on it, and writing to it triggers those effects, such as re-rendering a component.
Q12. What is the difference between ref and reactive?
ref wraps any value, including primitives, and is accessed through .value in script. reactive makes an object deeply reactive but cannot hold primitives and loses reactivity if you replace or destructure it.
Q13. Why does destructuring a reactive object break reactivity?
Destructuring copies the current values into plain variables, cutting the link to the Proxy. Use toRefs() to turn each property into a ref that stays connected.
Q14. What is a computed property?
A value derived from reactive state with computed(). It is cached and only recalculated when one of its dependencies changes, unlike a method, which runs on every render.
Q15. What is the difference between watch and watchEffect?
watch observes specific sources and gives both new and old values, running lazily by default. watchEffect runs immediately and tracks whatever reactive values it reads automatically.
Q16. What is shallowRef used for?
It makes only the .value assignment reactive, not the object inside. It improves performance for large data that you replace as a whole rather than change in place.
Q17. What does nextTick do?
Vue batches DOM updates and applies them asynchronously. await nextTick() waits until the DOM has been updated after a state change, for example before measuring an element.
Q18. What is a composable?
A function that uses Composition API features to package reusable stateful logic, named by convention with use, such as useMouse() or useFetch(). It replaces mixins in Vue 3.
Q19. Why are composables better than mixins?
Mixins hide where properties come from and can clash on names. Composables return values explicitly, so their sources are clear, names can be changed when destructuring, and types are easier to infer.
Q20. What are the main lifecycle hooks in the Composition API?
onMounted, onUpdated and onUnmounted are the most used, along with onBeforeMount, onBeforeUpdate and onBeforeUnmount. Clean up timers and listeners in onUnmounted.
Components & Templates
Q21. How do you pass data from a parent to a child?
Declare props in the child with defineProps(), then pass values as attributes in the parent, such as <UserCard :user="user" />. Props flow one way and should not be changed by the child.
Q22. How does a child communicate with its parent?
The child declares events with defineEmits() and calls emit('save', data). The parent listens with @save="handleSave".
Q23. How does v-model work on a component?
It passes a modelValue prop and listens for update:modelValue. In Vue 3.4 and later, defineModel() creates a ref that handles both sides automatically.
Q24. What is the difference between v-if and v-show?
v-if adds or removes the element from the DOM, which costs more to toggle. v-show only switches CSS display, which costs more on first render but toggles cheaply.
Q25. Why do you need a key with v-for?
The key gives each item a stable identity, so Vue can reuse and move the right elements when the list changes. Use a unique ID, not the array index, for lists that can be reordered.
Q26. What are slots?
Slots let a parent pass template content into a child component. There are default slots, named slots with #header, and scoped slots that receive data back from the child.
Q27. What is provide and inject?
A way to pass data from an ancestor to any descendant without passing props through each level. It suits themes, configuration or form context.
Q28. What is <Teleport>?
It renders part of a component's template elsewhere in the DOM, such as in body, while keeping it logically inside the component. Modals and tooltips commonly use it.
Q29. What is <KeepAlive>?
A wrapper that caches inactive component instances instead of destroying them, so their state is kept when switching tabs or views. Use the include and max props to control it.
Q30. What are custom directives?
Reusable low-level DOM behaviour registered as an object with hooks like mounted and updated, used as v-focus. They suit direct DOM tasks that components do not handle well.
Vue Router & State Management
Q31. What is Vue Router?
The official routing library for Vue. It maps URLs to components, supports nested and dynamic routes, and uses <RouterView> to display the matched component and <RouterLink> for navigation.
Q32. How do you define dynamic routes?
Use a parameter in the path, such as /users/:id, and read it with useRoute().params.id. Watch the param, because the same component instance is reused when only the param changes.
Q33. What are navigation guards?
Functions that run during navigation, such as router.beforeEach() globally or beforeEnter per route. They are commonly used to redirect users who are not logged in.
Q34. How do you lazy-load routes?
Use a dynamic import for the component, such as component: () => import('./views/Admin.vue'). The bundler splits it into a separate chunk that loads only when the route is visited.
Q35. What is the difference between history mode and hash mode?
History mode uses clean URLs like /about but needs the server to return index.html for every route. Hash mode uses /#/about and works on any static host without server configuration.
Q36. What is Pinia?
The official state management library for Vue. Stores are defined with defineStore() and have state, getters and actions, with full TypeScript support and Vue Devtools integration.
Q37. How is Pinia different from Vuex?
Pinia removes mutations, so actions change state directly. It has a simpler API, works naturally with the Composition API, supports multiple stores without nesting modules and gives better type inference.
Q38. How do you keep reactivity when destructuring a Pinia store?
Use storeToRefs(store) for state and getters, which returns refs that stay reactive. Actions can be destructured directly from the store.
Q39. When do you need a global store instead of props?
When many unrelated components share the same data, such as the current user or a cart. Data used by only one component tree is simpler to keep in local state or a composable.
Q40. What is Nuxt?
A framework built on Vue that adds server-side rendering, static generation, file-based routing and server API routes. It plays a similar role for Vue that Next.js plays for React.
Performance, Testing & Ecosystem
Q41. How do you improve performance in a Vue app?
Lazy-load routes and heavy components, use v-show for frequent toggles, use shallowRef for large data, virtualise long lists, and avoid creating new objects in templates on every render.
Q42. What is defineAsyncComponent?
It defines a component that is loaded only when it is first rendered, splitting it into its own chunk. You can set loading and error components and a timeout.
Q43. What does v-once do?
It renders an element or component only once and skips it in later updates. Use it for content that never changes after the first render.
Q44. What does v-memo do?
It skips updating part of a template unless the listed dependencies change. It is useful in large v-for lists where most items stay the same.
Q45. How do you test Vue components?
Use Vitest as the test runner with Vue Test Utils or Testing Library to mount components, simulate user events and check the rendered output. Use Playwright or Cypress for end-to-end tests.
Q46. How does Vue support TypeScript?
Vue 3 is written in TypeScript. Use <script setup lang="ts">, type props with defineProps<Props>(), and check templates with vue-tsc.
Q47. How do you prevent XSS in Vue?
Vue escapes text interpolation automatically. Avoid v-html with user input, or sanitise the HTML first with a library like DOMPurify.
Q48. What build tool do Vue projects use?
New Vue projects use Vite, created by the author of Vue. It provides a fast dev server with hot module replacement and optimised production builds.
Q49. What is the Vue Devtools extension?
A browser extension for inspecting the component tree, props, state, Pinia stores, router state and performance timelines of a running Vue app.
Q50. What is server-side rendering in Vue?
Rendering components to HTML on the server, then hydrating them in the browser. It improves first load and SEO, and is usually done through Nuxt rather than set up by hand.