Vue
Basic
Questions
1. What is Vue.js?
o Vue.js is a progressive JavaScript framework
used for building user interfaces and single-page applications. It is designed
to be incrementally adaptable, focusing on declarative rendering and component
composition.
2. What are the key
features of Vue.js?
o Reactivity: Two-way data binding using v-model.
o Component-Based Architecture: Encapsulates HTML,
CSS, and JavaScript within components.
o Directives: Special HTML
attributes like v-bind, v-for, and v-if.
o Virtual DOM: Efficiently
updates and renders components.
o Transition Effects: Animates elements
when they enter/leave the DOM.
3. What are Vue directives?
Give examples.
o Directives are special
tokens in the markup that tell the library to do something with the DOM.
§ v-bind: Dynamically binds an attribute (:src="imageUrl").
§ v-for: Loops through an array (v-for="item
in items").
§ v-if: Conditional rendering (v-if="isVisible").
4. What is the difference
between v-if and v-show?
o v-if: Completely removes and re-renders elements in the DOM based on
the condition. Best for runtime conditions.
o v-show: Toggles the visibility of an element using CSS (display: none). Best for toggling frequently.
Intermediate Questions
5. What is the Vue
instance?
o The Vue instance is the root of a
Vue application. It is created by instantiating the Vue class.
javascript
new Vue({
el: '#app',
data: { message: 'Hello Vue!' },
});
6. What are Vue components?
o Components are reusable,
encapsulated blocks of code. They enable code reusability and modularity.
javascript
Vue.component('my-component', {
template: '<div>A custom
component</div>',
});
7. What is a computed
property in Vue? How is it different from methods?
o Computed Properties: Cached values based on
dependencies. Recomputed only when dependencies change.
o Methods: Executes every time
it's called, without caching.
javascript
computed: {
fullName() {
return this.firstName + ' ' +
this.lastName;
},
}
8. Explain the Vue
lifecycle hooks.
o Lifecycle hooks are
special functions that run at specific stages of a component's lifecycle.
§ created: After instance creation.
§ mounted: After the DOM is mounted.
§ updated: After reactive data changes.
§ destroyed: After the instance is destroyed.
· What is
Vuex, and why is it used?
- Vuex is a state management
library for Vue.js. It manages shared state across components in a
predictable manner.
javascript
const store = new
Vuex.Store({
state: { count: 0 },
mutations: {
increment(state) { state.count++; } },
});
· How
do you manage routing in a Vue app?
- Vue Router is used to manage
routing in Vue.js applications.
javascript
const routes = [
{ path: '/home',
component: Home },
{ path: '/about',
component: About },
];
const router = new
VueRouter({ routes });
new Vue({ router
}).$mount('#app');
· What
are mixins in Vue.js?
- Mixins are a way to distribute
reusable functionalities across components.
javascript
const myMixin = {
created() {
console.log('Mixin hook called'); },
};
export default {
mixins: [myMixin],
};
· What
is the difference between v-bind and v-model?
- v-bind: Dynamically binds an
attribute to a property.
html
<img
v-bind:src="imageUrl" />
- v-model: Two-way binding between form
inputs and data.
html
Copy code
<input
v-model="inputValue" />
Practical Questions
13. How would you optimize a Vue app?
o Use lazy loading for
routes and components.
o Use Vue’s keep-alive feature to cache components.
o Avoid large computed
properties; split them into smaller ones.
o Minimize watchers and
deep watchers.
o Use virtual scrolling
for large lists.
14. How do you handle form validation in Vue?
o Use libraries like vee-validate or write custom validation logic with watchers
and computed properties.
15. Explain the difference between props and $emit.
o props: Pass data from parent to child components.
o $emit: Trigger events from child to parent
components.
Javascript
// Child
this.$emit('event-name', data);
// Parent
<ChildComponent
@event-name="handleEvent" />
16. What is Vue's reactivity system, and how does it
work?
o Vue’s reactivity is
achieved using ES6 Proxy objects, which
intercept changes to data and trigger DOM updates.
The
frameworks can create vue project
- For general Vue.js
development: Vue CLI, Vite.
- For full-stack, SSR, or
static websites: Nuxt.js, Gridsome.
- For building
cross-platform apps: Quasar Framework.
- For building UI-rich
apps: Vuetify, Element UI/Element Plus, BootstrapVue.
- For documentation or
simple static sites: VuePress.
|
Framework/Tool |
Features |
Use
Case |
|
Vue CLI |
Feature-rich, presets for Vue 3. |
Traditional Vue projects. |
|
Vite |
Super-fast,
modern development environment. |
Modern Vue
projects, speed-critical apps. |
|
Nuxt 3 |
SSR, SSG, or hybrid rendering with Vue
3. |
Server-side or full-stack apps. |
|
Quasar |
Build Vue
apps for multiple platforms. |
Multi-platform
apps (SPA, PWA, Mobile). |
|
Laravel + Vue 3 |
Integrates Vue with a Laravel backend. |
Backend-driven Vue applications. |
|
Custom Setup |
Fully
customizable build process. |
Advanced
configurations, non-standard setups. |
Each
framework serves a specific purpose, so choose the one that best fits your
project's needs. If you’re looking for speed and simplicity with Vue 3, Vite is
highly recommended. If you need a full-featured SSR solution, Nuxt.js is
the best choice.
Summary of Common Directives:
- v-bind: Bind an attribute or class to
a dynamic value.
- v-model: Two-way binding for form
elements.
- v-for: Loop through an array or
object to render items.
- v-if, v-else-if, v-else: Conditionally render
elements.
- v-show: Toggle visibility without
removing elements from the DOM.
- v-on: Listen for DOM events and
execute methods.
- v-text, v-html: Bind raw HTML or text
content.
- v-slot: Define slots for components.
- v-pre, v-cloak, v-once, v-memo: Performance and rendering
optimizations.
Summary
of Vue Lifecycle Hooks
|
Hook |
When it is called |
Use Case Example |
|
beforeCreate |
Before
component instance is created. |
Rarely
used. |
|
created |
After component instance is
created, data is reactive. |
Initializing data, setting up API
calls. |
|
beforeMount |
Right
before mounting the component to the DOM. |
Perform
any last-minute operations before rendering. |
|
mounted |
After the component has been
mounted to the DOM. |
DOM manipulation, initializing
third-party libraries. |
|
beforeUpdate |
Before
the data changes and re-rendering happens. |
Optimizing
performance or reacting to data changes before render. |
|
updated |
After the data changes and the
component has been re-rendered. |
DOM updates after data changes. |
|
beforeUnmount |
Right
before the component is destroyed and removed from the DOM (Vue 3 only). |
Cleanup,
stopping timers, removing event listeners. |
|
unmounted |
After the component has been destroyed and removed from
the DOM (Vue 3 only). |
Cleanup, resource release, removing subscriptions. |
Additional Notes
- Vue's lifecycle hooks are a
powerful way to run code at specific points during the component’s life
cycle.
- The Composition API in
Vue 3 gives you more flexibility by allowing you to use these hooks within
the setup() function.
- In Vue 2, hooks like beforeDestroy and destroyed are used instead of beforeUnmount and unmounted in Vue 3.
Component-Based Architecture vs
Composition API
Comparison
|
Feature |
Component-Based
Architecture |
Composition API |
|
Definition |
Organizing code into
reusable and modular components. |
A new API in Vue.js to
define logic using functions and hooks. |
|
Code Organization |
Code is organized by component,
with a clear structure. |
Logic is organized by function
inside the setup() method. |
|
Reusability |
Reusable components
are the core, often through props and events. |
Logic can be reused
across components using composables. |
|
Flexibility |
Components can grow complex and
interdependent. |
More flexible, as logic can be
shared and combined in many ways. |
|
Learning Curve |
Easy for beginners;
based on basic component principles. |
Slightly steeper
curve, especially for those new to the Composition API. |
|
Use Cases |
Suitable for small to medium-sized
projects. |
Best for large-scale applications
where logic reuse and flexibility are critical. |
|
TypeScript Support |
Supported but can be
tricky with large components. |
Excellent TypeScript
support, with better type inference. |
Conclusion
- Component-Based Architecture is a general
architectural pattern that focuses on dividing an application into small,
reusable components, which helps in organizing code and making
applications more modular.
- Composition API, on the other hand, is a
specific feature of Vue 3 that allows developers to organize and reuse
logic more effectively by grouping related functionality into composable
functions, offering flexibility and better code reusability.
passing data between parent and child components
In
Vue.js (both Vue 2 and Vue 3), passing data between parent and child components
is a core concept, and Vue provides several methods for communication between
components. Below is an explanation of how to pass data between parent and
child components in both Vue 2 and Vue 3.
- · Parent to Child: Use props in both Vue 2 and Vue 3.
- · Child to Parent: Use $emit() or defineEmits() in Vue 3, and this.$emit() in Vue 2.
- · Two-way Binding: Use v-model with default prop
(value in Vue 2, modelValue in Vue 3) for two-way communication between
parent and child.
In Vue 3, defineEmits() is a function used in the Composition
API to define custom events that a component will emit to its parent.
It helps manage event emission more explicitly and clearly than the
traditional this.$emit() used in Vue 2.
const emit
= defineEmits([/* event names */]);
A well-structured
Vue.js project improves maintainability, scalability, and collaboration.
Root Directory
plaintext
my-vue-app/
├── public/├── src/│ ├── assets/│ ├── components/│ ├── views/│ ├── router/│ ├── store/│ ├── composables/│ ├── styles/│ ├── utils/│ ├── App.vue│ ├── main.js├── .env├── .gitignore├── package.json├── vite.config.js / vue.config.js├── README.md
public/
- Static files like icons,
index.html, and other assets. - Contents are served as-is and do not go through the build pipeline.
App.vue: The root Vue component that acts
as the entry point for the application.
main.js: Application bootstrap file where
the Vue app is created and plugins are initialized.
.env:
Environment variables for different modes (e.g., development, production).
vite.config.js or vue.config.js: Configures
the build process and plugins.
package.json:
Project dependencies and scripts.
Comments
Post a Comment