ng g c domains/shared/components/counter
ng g c domains/info/pages/about
Ciclo de vida de los componentes
- constructor
- ngOnChanges
- ngOnInit
- ngDoCheck
- ngAfterContentInit
- ngAfterContentChecked
- ngAfterViewInit
- ngAfterViewCheck
- ngOnDestroy
Introducción a Angular y Fundamentos
Aprende Angular 17
Creando tu primer proyecto en Angular
Implementando estilos
Mostrando elementos
Property Binding en Angular
Event Binding: click y doble click
Event binding: keydown
Modelo de reactividad con Signals
Creando un Signal en Angular
Estructuras de control en Angular
Directivas de control
Uso de ngFor
ngFor para objetos
Update Tasks
Uso de ngIf
Uso de ngSwitch y ngSwitchDefault
Controlando un input
Manejo de formularios en Angular
Alistando tu aplicación para producción
Estilos al modo Angular
Clases en Angular
Editing mode
Estados compuestos con computed
Usando effect para localStorage
Uso de ngbuild
Despliegue con Firebase Hosting
Nueva sintaxis en Angular
Directivas @For, @switch
Migrando a la nueva sintaxis de Angular v17
Componentes Reutilizables y Comunicación
Construyendo un e-commerce en Angular
Componentes en Angular
Mostrando los componentes
Angular DevTools
Uso de Inputs en Angular
Uso de Outputs en Angular
Componentes para Producto
Ciclo de vida de los componentes
Ciclo de vida de componentes
Ciclo de vida de componentes: ngOnChanges
Ciclo de vida de componentes: ngOnInit
Detectando cambios en los inputs
Evitando memory leaks con ngDestroy
Audio player con ngAfterViewInit
Creando la página "about us" o "conócenos"
Mejorando la interfaz del producto
Creando componente de productos
Creando el Header
Creando el carrito de compras
Comunicación padre e hijo
Calculando el total con ngOnChanges
El problema del prop drilling
Reactividad con signals en servicios
Entendiendo la inyección de dependencias
Integración y Datos
Obteniendo datos una REST API
Importaciones cortas en Typescript
Pipes en Angular
Construyendo tu propio pipe
Utilizando librerías de JavaScript en Angular
Conociendo las directivas
Deployando un proyecto en Vercel
Enrutamiento y Navegación
Ruta 404
Uso del RouterLink
Vistas anidadas
Uso del RouterLinkActive
Detalle de cada producto
Obteniendo datos del producto
Galería de imagenes
Detalle de la galería
Perfeccionando tu e-commerce
Mostrando categorias desde la API
Url Params
LazyLoading y Code Splitting
Aplicando LazyLoading
Prefetching
Usando la nueva sintaxis de Angular 17
Lanzando tu aplicación a producción
You don't have access to this class
Keep learning! Join and start boosting your career
Every component in Angular has a lifecycle, meaning that it goes through several events from the time it is created until it is destroyed. These events include its creation, update and eventual removal from the application. This understanding is crucial to manage component logic effectively and leverage Angular's capabilities for a seamless user experience.
The lifecycle of a component in Angular includes a specific set of events in order, such as constructor
, ngOnChanges
, ngOnInit
, ngDoCheck
, and finally ngOnDestroy
. Here we break down these events:
Constructor: this is the first method that is executed when creating a component. Although it is a place to initialize variables, it is important to note that any asynchronous actions should not be placed at this point, as it is not recommended. It is especially useful for assigning values directly and prior to rendering the component.
ngOnChanges: Runs both before and during component rendering. It is useful to detect changes in the @Input
and adapt the logic according to these variations.
ngOnInit: Runs only once after the changes have been applied and just before the component is rendered for the first time.
ngDoCheck: Provides an opportunity to detect and react to changes, to customize how the custom change detection works.
ngOnDestroy: Invoked just before Angular destroys the component. This is the perfect place to unsubscribe and free up resources.
To experiment with these events, let's create a simple component called Counter
and an About
page containing it. Let's follow along with some practical steps:
To work with the lifecycle, let's set up two inputs(@Input
) inside the component:
{import { Component, Input } from '@angular/core';
@Component({ selector: 'app-counter', template: '<p>Counter component loaded!</p>'})export class CounterComponent { @Input() duration: number = 0; @Input() message: string = '';
constructor() { console.log('Constructor executed.'); }
ngOnChanges(changes: SimpleChanges) { console.log('ngOnChanges executed.', changes); }
ngOnInit() { console.log('ngOnInit executed.'); }
ngDoCheck() { console.log('ngDoCheck executed.'); }
ngOnDestroy() { console.log('ngOnDestroy executed.'); } } }}
Required Inputs: By default, duration
is 0 and message
is an empty string. TypeScript's inference engine helps to automatically detect their types.
Constructor Usage: Only direct values should be defined. Avoid any actions involving promises or asynchronous processes.
Change Tracking: Using ngOnChanges
, you can identify which specifications have changed using the SimpleChanges
object.
This knowledge of the lifecycle in Angular allows you to control when and how certain code segments are executed, optimizing the performance and logic of the component. With continued practice and debugging, they will become an essential tool in the development of complex applications. Go ahead and explore these concepts in your Angular projects!
Contributions 9
Questions 0
ng g c domains/shared/components/counter
ng g c domains/info/pages/about
Ciclo de vida de los componentes
✅
Want to see more contributions, questions and answers from the community?