Vue Answers
0:000:00
Vue.js Answers
Basic Vue.js Answers
# | Question | Answer | Examples | Comparison to ReactJS |
---|---|---|---|---|
1 | What is Vue.js? | A progressive JavaScript framework for building user interfaces | Developed by Evan You, known for its ease of use and flexibility | Both are JavaScript frameworks/libraries for building UIs. Vue is often considered easier to learn initially, especially for those with traditional HTML/CSS/JS backgrounds. |
2 | What is the purpose of Vue.js? | Building reusable UI components and managing application state | Creating single-page applications (SPAs), interactive web interfaces | Both focus on component-based architecture. Vue uses HTML-based templates; React uses JSX (JavaScript XML). |
3 | What is a Component? | A fundamental building block of a Vue application; a reusable piece of UI | A .vue file containing <template>, <script>, and <style> sections | Both use components. Vue uses Single File Components (SFCs) which bundle template, script, and style in one file. React typically uses separate files for each. |
4 | How do you create a simple component? | Define a JavaScript object with options API (data, methods, props) | <template><h1>Hello, {{ name }}</h1></template> <script> export default { props: ['name'] }; </script> | Vue uses HTML-based templates; React uses JSX. Both are compiled into similar function calls by build tools. |
5 | What are Props? | Data passed down from a parent component to a child component | <Child name="Alice" /> (name is a prop) | Both use props for passing data down. Vue's props are defined explicitly in the component's options. React's props are passed as function arguments. |
6 | What is State? | Data that is managed by a component and can change over time (internal) | data() { return { count: 0 }; } (Options API) or const count = ref(0); (Composition API) | Both manage component state. Vue uses data() function (Options API) or ref/reactive (Composition API). React uses useState hook. |
7 | How do you handle events in Vue? | Using the v-on directive (or its shorthand @) | <button @click="handleClick">Click Me</button> | Both use event handling. Vue uses directives (v-on: or @); React uses event handlers passed as props (onClick). |
8 | What is the v-if directive? | Conditionally renders an element based on a condition | <div v-if="isVisible">Visible</div> | Similar to React's conditional rendering logic. |
9 | How do you iterate over lists in Vue? | Using the v-for directive | <ul><li v-for="item in items" :key="item.id">{{ item.name }}</li></ul> | Similar to React's map() method. Vue uses v-for with :key; React uses map() with a key prop. |
10 | What is the v-model directive? | Two-way data binding on form elements | <input v-model="message"> | Vue's built-in directive for two-way form binding. React typically requires explicit value prop and onChange handler. |
Intermediate Vue.js Answers
# | Question | Answer | Examples | Comparison to ReactJS |
---|---|---|---|---|
1 | What is the Options API? | The traditional way to define components using an object with options like data, methods, computed | export default { data() { ... }, methods: { ... } } | More similar to class components in React (though syntax is different). |
2 | What is the Composition API? | A newer way to structure component logic using setup() function and reactive primitives (ref, reactive) | import { ref } from 'vue'; export default { setup() { const count = ref(0); return { count }; } } | More similar to React Hooks (useState, useEffect). Allows better code organization and reuse. |
3 | What is ref() in the Composition API? | Creates a reactive reference to a primitive value; needs to be accessed with .value | const count = ref(0); count.value++; | Similar to React's useState for primitive values. |
4 | What is reactive() in the Composition API? | Creates a reactive proxy object; allows direct access to properties | const state = reactive({ count: 0 }); state.count++; | Similar to React's useState for objects/arrays. |
5 | What is computed()? | Creates a reactive property based on other reactive state; caches results | const doubledCount = computed(() => count.value * 2); | Similar to React's useMemo. |
6 | What is watch()? | Observes a reactive property or source and runs a callback when it changes | watch(count, (newValue, oldValue) => { ... }); | Similar to React's useEffect with dependency arrays. |
7 | What are Lifecycle Hooks? | Functions that are called at specific stages of a component's life (e.g., mounted, updated) | onMounted(() => { ... }); (Composition API), mounted() { ... } (Options API) | Similar to React's lifecycle methods (class components) or useEffect (functional components). |
8 | What is Vue Router? | The official routing library for Vue.js | createRouter, createWebHistory, | Similar to React Router. |
9 | What is Pinia? | The official state management library for Vue.js (recommended for Vue 3) | defineStore, useStore, state, getters, actions | Similar to Redux or Zustand in React. |
10 | What is Vuex? | The previous official state management library for Vue.js (often used in Vue 2) | createStore, state, getters, mutations, actions, modules | Similar to Redux. |
11 | What are Slots? | Placeholders in a component's template where content can be injected by the parent | Similar to React's children. | |
12 | What is the difference between v-if and v-show? | v-if conditionally creates/removes the element; v-show toggles the CSS display property | v-if is better for less frequent changes; v-show is better for frequent toggling. | Similar to React's conditional rendering using if/else or ternary operators vs. conditional CSS display. |
13 | How do you pass data from child to parent? | Using custom events ($emit) | emit('my-event', payload) and parent listens @my-event="handleEvent". | Similar to React's passing callbacks down as props. |
14 | What is a Transition Component? | A built-in component for adding CSS transitions and animations to elements | Similar to React's CSS transitions/animations or libraries like react-transition-group. | |
15 | What is a Global API? | A component that is available anywhere in the application | app.component('MyGlobalComponent', MyGlobalComponent) | Less common in modern React; components are typically imported and used locally. |
Advanced Vue.js Answers
# | Question | Answer | Examples | Comparison to ReactJS |
---|---|---|---|---|
1 | What are Composables? | Functions that encapsulate reusable stateful logic using the Composition API | function useCounter() { const count = ref(0); ... return { count, increment }; } | Similar to custom React Hooks (e.g., useFetch, useLocalStorage). |
2 | What is Teleport? | Allows part of a component's template to be rendered in a different location in the DOM (e.g., modals) | <Teleport to="#modal-root"> <MyModal /> </Teleport> | Similar to React's createPortal. |
3 | What is Suspense? | A built-in component for handling asynchronous setup (e.g., lazy loading components) | <Suspense fallback="<div>Loading...</div>"><MyAsyncComponent /></Suspense> | Similar to React's Suspense used with React.lazy. |
4 | What is the difference between v-if and v-for? | v-if has higher priority than v-for when both are used on the same element (use <template> to separate them) | <template v-for="item in items"><li v-if="item.isActive">{{ item.name }}</li></template> | Similar to React, you generally need to keep them separate or use conditional logic within the map call. |
5 | What is the difference between v-model.lazy and v-bind/ v-on? | v-model.lazy updates the data only on change event (or blur) instead of input event | <input v-model.lazy="message"> | Similar to React using onChange handlers that only update state on specific events (e.g., blur or debounce). |
6 | What is provide / inject? | A way to pass data down to any descendant component deep in the component tree without prop drilling | Parent: provide('theme', this.theme); Child: const theme = inject('theme'); | Similar to React's Context API (Provider / useContext). |
7 | What are Mixins? | A way to share reusable functionality across components (less common now with Composition API) | export default { mixins: [myMixin] }; | Similar to React's Higher-Order Components (HOCs) or custom Hooks. Mixins can lead to naming conflicts and unclear property origins. |
8 | What is the purpose of script setup? | A syntax sugar for the Composition API components, reducing boilerplate code | <script setup> import { ref } from 'vue'; const count = ref(0); </script> | A more concise way to write Composition API components. |
9 | What is createApp (Vue 3)? | The entry point for creating a Vue 3 application instance | import { createApp } from 'vue'; const app = createApp(App); app.mount('#app'); | Similar to ReactDOM.createRoot in React 18+. |
10 | What is the reactivity system in Vue 3? | Uses Proxies to track changes to data more efficiently than ObjectdefineProperty | ref and reactive are the core reactive primitives. | Similar to React's state management using useState. |
11 | What is a render function? | An alternative to templates for defining components using JavaScript code | render() { return h('div', {}, 'Hello'); } | Similar to React's JSX or React.createElement(). |
12 | What is the difference between Vue 2 and Vue 3? | Vue 3 introduced Composition API, Proxy-based reactivity, better TypeScript support, Teleport, Suspense, etc. | Vue 3 generally offers better performance, smaller bundle size, and more flexible component structure. | |
13 | How does the event system work in Vue.js? | Relies on the browser's native DOM events system, often using a synthetic event delegation | Similar to React's use of browser events and synthetic event system. | |
14 | What are the benefits of the Composition API? | Better code organization, reusability, improved TypeScript inference, avoids this context issues | Similar to the benefits of React Hooks. | |
15 | What are the drawbacks of the Composition API? | Steeper initial learning curve compared to Options API, can be less intuitive for beginners | Requires understanding of ref, reactive, setup, etc. |