Vue.js Interview Questions
35 questions with detailed answers
Question:
What is Vue.js and what are its key features?
Answer:
Vue.js is a progressive JavaScript framework for building user interfaces. Key features include: reactive data binding, component-based architecture, virtual DOM, directives, computed properties, and easy integration with existing projects.
Question:
Explain the Vue.js instance lifecycle hooks.
Answer:
Vue.js lifecycle hooks: beforeCreate, created, beforeMount, mounted, beforeUpdate, updated, beforeDestroy, destroyed. Example: mounted() { console.log("Component mounted"); }
Question:
What is data binding in Vue.js? Explain with examples.
Answer:
Data binding connects data to DOM elements. One-way: {{ message }}, Two-way: v-model="message". Example: <input v-model="name"> <p>{{ name }}</p>
Question:
What are Vue.js directives? List common directives.
Answer:
Directives are special attributes with v- prefix. Common ones: v-if, v-for, v-show, v-model, v-bind, v-on, v-html, v-text. Example: <div v-if="isVisible">Content</div>
Question:
Explain v-if vs v-show in Vue.js.
Answer:
v-if conditionally renders elements (adds/removes from DOM), v-show toggles CSS display property. v-if has higher toggle cost, v-show has higher initial render cost. Use v-show for frequent toggles.
Question:
What are computed properties in Vue.js?
Answer:
Computed properties are cached based on dependencies. Example: computed: { fullName() { return this.firstName + " " + this.lastName; } }. They update only when dependencies change.
Question:
What is the difference between methods and computed properties?
Answer:
Computed properties are cached and only re-evaluate when dependencies change. Methods run every time they are called. Use computed for expensive operations that depend on reactive data.
Question:
How do you handle events in Vue.js?
Answer:
Use v-on directive or @ shorthand. Example: <button @click="handleClick">Click</button>. Methods: { handleClick() { console.log("Clicked"); } }
Question:
What are Vue.js components and how do you create them?
Answer:
Components are reusable Vue instances. Create with: Vue.component("my-component", { template: "<div>Hello</div>" }) or as single file components (.vue files).
Question:
Explain props in Vue.js components.
Answer:
Props pass data from parent to child components. Example: props: ["message"] or props: { message: { type: String, required: true } }. Use in template: {{ message }}
Question:
What is v-model and how does it work?
Answer:
v-model creates two-way data binding on form inputs. It's syntactic sugar for :value and @input. Example: <input v-model="message"> equals <input :value="message" @input="message = $event.target.value">
Question:
How do you loop through data in Vue.js templates?
Answer:
Use v-for directive. Examples: <li v-for="item in items" :key="item.id">{{ item.name }}</li> or <li v-for="(item, index) in items" :key="index">{{ item }}</li>
Question:
How do you handle forms in Vue.js?
Answer:
Use v-model for two-way binding. Handle validation with computed properties or libraries like VeeValidate. Example: <input v-model="form.email" :class="{ error: !isValidEmail }">
Question:
What are dynamic components in Vue.js?
Answer:
Dynamic components switch between components using <component :is="currentComponent"></component>. Useful for tabs, modals, or conditional rendering of different components.
Question:
Explain Vue.js transitions and animations.
Answer:
Use <transition> wrapper for enter/leave animations. CSS classes: v-enter, v-enter-active, v-leave, v-leave-active. Example: <transition name="fade"><div v-if="show"></div></transition>
Question:
What are watchers in Vue.js and when to use them?
Answer:
Watchers observe data changes and perform actions. Use for async operations or expensive operations. Example: watch: { question(newVal) { this.getAnswer(); } }
Question:
Explain Vue.js slots and their types.
Answer:
Slots distribute content. Types: default slots, named slots, scoped slots. Example: <slot name="header"></slot>. Scoped: <slot :user="user"></slot>
Question:
What is Vuex and why is it used?
Answer:
Vuex is state management pattern for Vue.js. Centralized store for all components. Contains state, mutations, actions, getters. Used for complex applications with shared state.
Question:
Explain Vue Router and its key features.
Answer:
Vue Router is official router for Vue.js. Features: nested routes, route guards, lazy loading, programmatic navigation. Example: { path: "/user/:id", component: User }
Question:
What are mixins in Vue.js?
Answer:
Mixins distribute reusable functionalities across components. Example: const myMixin = { created() { console.log("Mixin hook called"); } }. Use: mixins: [myMixin]
Question:
How do you communicate between parent and child components?
Answer:
Parent to child: props. Child to parent: $emit events. Example: this.$emit("update", data). Parent: <child @update="handleUpdate"></child>
Question:
What are custom directives in Vue.js?
Answer:
Custom directives for low-level DOM access. Example: Vue.directive("focus", { inserted(el) { el.focus(); } }). Use: <input v-focus>
Question:
Explain Vue.js filters and their usage.
Answer:
Filters transform text formatting. Example: filters: { capitalize(value) { return value.charAt(0).toUpperCase() + value.slice(1); } }. Use: {{ message | capitalize }}
Question:
What is the virtual DOM in Vue.js?
Answer:
Virtual DOM is JavaScript representation of real DOM. Vue creates virtual DOM tree, compares with previous version, and updates only changed parts for better performance.
Question:
What is Vue 3 Composition API and its advantages?
Answer:
Composition API provides better logic reuse and TypeScript support. Uses setup() function with reactive(), ref(), computed(). Better code organization for complex components.
Question:
Explain Vue.js reactivity system and how it works internally.
Answer:
Vue uses Object.defineProperty (Vue 2) or Proxy (Vue 3) to track dependencies. When data changes, it triggers re-renders of dependent components through a reactive system.
Question:
What are render functions in Vue.js?
Answer:
Render functions create virtual DOM programmatically. Example: render(h) { return h("div", { class: "container" }, "Hello"); }. More flexible than templates for complex logic.
Question:
How do you optimize Vue.js application performance?
Answer:
Use lazy loading, code splitting, v-show vs v-if, computed properties, virtual scrolling, bundle analysis, tree shaking, and proper key usage in v-for loops.
Question:
What are Vue.js plugins and how do you create them?
Answer:
Plugins add global functionality. Example: const MyPlugin = { install(Vue, options) { Vue.prototype.$myMethod = function() {} } }. Use: Vue.use(MyPlugin)
Question:
Explain server-side rendering (SSR) with Vue.js.
Answer:
SSR renders Vue components on server for better SEO and initial load time. Use Nuxt.js or Vue SSR. Challenges: hydration, state management, browser-specific code.
Question:
What are provide/inject in Vue.js?
Answer:
Provide/inject allows ancestor components to serve as dependency injector for descendants. Example: provide: { theme: "dark" }, inject: ["theme"]. Alternative to prop drilling.
Question:
How do you handle error boundaries in Vue.js?
Answer:
Use errorCaptured lifecycle hook to catch errors in child components. Example: errorCaptured(err, instance, info) { console.error(err); return false; }
Question:
What is Vue.js teleport and when to use it?
Answer:
Teleport renders component content in different DOM location. Example: <teleport to="body"><modal></modal></teleport>. Useful for modals, tooltips, notifications.
Question:
Explain Vue.js custom events and event modifiers.
Answer:
Custom events: this.$emit("custom-event", data). Event modifiers: @click.stop, @submit.prevent, @keyup.enter. Chain modifiers: @click.stop.prevent
Question:
How do you test Vue.js components?
Answer:
Use Vue Test Utils with Jest or Mocha. Test props, events, computed properties, methods. Example: wrapper = mount(Component, { propsData: { msg: "Hello" } })