What are Vue life cycles ? Which all you used?
In Vue.js, lifecycle hooks are functions that allow developers to run code at specific stages of a Vue component's lifecycle. These hooks provide control over how the component is initialized, updated, or destroyed.
Lifecycle Phases
- Creation Phase:
- Before the component is added to the DOM.
- Mounting Phase:
- When the component is inserted into the DOM.
- Updating Phase:
- When the component's reactive data changes, triggering re-rendering.
- Destruction Phase:
- When the component is removed from the DOM.
Common Lifecycle Hooks
Below is a categorized list of hooks with their purpose:
1. Creation Phase
beforeCreate():- Called after the component instance is initialized but before data observation and events setup.
- Rarely used.
created():- Called after the component is initialized, reactive data and events are set up, but the DOM is not yet available.
- Useful for fetching data or initializing logic.
2. Mounting Phase
beforeMount():- Called before the component is added to the DOM.
mounted():- Called after the component is mounted in the DOM.
- Ideal for interacting with the DOM or initializing third-party libraries.
3. Updating Phase
beforeUpdate():- Called before the DOM is updated when reactive data changes.
updated():- Called after the DOM is updated.
- Avoid heavy computations here as they might cause re-rendering.
4. Destruction Phase
beforeUnmount()(formerlybeforeDestroyin Vue 2):- Called right before the component is removed from the DOM.
unmounted()(formerlydestroyedin Vue 2):- Called after the component is completely removed.
- Ideal for cleanup tasks (e.g., removing event listeners or stopping intervals).
Explain how watch works in vue ?Where You used?
watch observes changes in reactive properties or props and triggers a handler.The watch option in Vue is used to monitor changes in a specific reactive property or data variable and execute code when that property changes.
watch: {
propertyName(newValue, oldValue) {
// Code to run when propertyName changes
}
}
propertyName: The reactive property or data variable being watched.newValue: The updated value of the watched property.oldValue: The previous value of the watched property.When to Use watch
- When you want to respond to changes in reactive properties or
props. - When side effects (like API calls, debouncing, etc.) need to be triggered upon data updates.
- When monitoring deeply nested objects or arrays for changes.
models.py separates the database logic from other application logic, making the code modular and reusable.The
models.py file is the backbone of your Flask application's data layer. It defines how data is structured, stored, and related in your database. By using SQLAlchemy, you can work with databases in an intuitive, Pythonic way while taking advantage of powerful ORM features like relationships, constraints, and migrations.Explain the Database Schema?
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
SQLAlchemy: ORM to define and interact with database tables.UserMixin: Provides methods for user authentication when using Flask-Login.Creating the Database Instance
- This
dbinstance is used to define and manage database models.
Models are Python classes that inherit fromdb.Model. Each model corresponds to a table in the database. - db is the database object
- The schema defines tables and their relationships in your database. Each table has fields, and foreign keys define relationships between tables.
Key Components in the Example
__tablename__:- Specifies the name of the database table explicitly. If omitted, the table name defaults to the class name in lowercase.
db.Column:- Defines a column in the table.
db.Integer: Integer type.db.String: String type with a maximum length.db.Text: Long text type.nullable=False: Field is required.unique=True: Ensures unique values.
- Defines a column in the table.
Primary Key:
primary_key=True: Uniquely identifies each record in the table.
Relationships:
db.ForeignKey: Links a column to another table.db.relationship: Establishes a connection between models.
Default Values:
default: Sets a default value for a column.
Methods:
__repr__: Defines how an instance of the class is represented, useful for debugging.
1. General API Design
- Framework: Flask with
flask-restfulfor building RESTful APIs. - Database: SQLite for data storage, with SQLAlchemy for ORM.
- Authentication: APIs are secured using authentication (tokens) and role-based access control.
- Response Format: APIs return responses in JSON format.
User Management
Registration
POST /api/register
Allows new users to registerLogin
POST /api/login
Authenticates the user and returns a token.Get User Details
GET /api/users/<id>
Fetches details of a specific user.
Technologies Supporting the API
- Flask-RESTful: Simplifies API creation.
- JWT Authentication: Secures endpoints.
- SQLAlchemy: Manages data storage and retrieval.
- Flask-Caching with Redis: Improves performance for repetitive API calls (e.g., fetching services).
API Features
Authentication and Authorization
- Role-based access using decorators like
@auth_required,@roles_required. - Pagination and Filtering, validation
Frontend Structure
The frontend is built with Vue.js, a modern JavaScript framework for creating dynamic and interactive user interfaces.
Single-Page Application (SPA):
UI Frameworks:
- Bootstrap: Used for responsive design and styling.
Components:
- Reusable Vue.js components simplify UI management and reduce redundancy.
State Management:
- Vue's reactive data and props are used for passing and managing state locally.
- No global state management (e.g., Vuex) is used unless explicitly needed.
Routing:
- Vue Router manages navigation between views.
- Navigation guards restrict access based on user roles
Backend Structure
The backend is built using Flask, a lightweight and modular web framework, and is responsible for handling business logic, database management, and API communication.
RESTful APIs are created using Flask-RESTful.
Authentication:
- (Token) is used for secure user authentication.
- Role-based access control is enforced with decorators like
@roles_requiredand@auth_required. - Celery + Redis handles tasks like sending email notifications and generating monthly reports.
- Redis is used to cache frequently accessed data (e.g., service listings).
No comments:
Post a Comment