clase:daw:diw:1eval:tema03
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Revisión previa | |||
| — | clase:daw:diw:1eval:tema03 [2025/11/05 16:48] (actual) – [Filtros] Lorenzo | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| + | ====== 3. Creación de componentes ====== | ||
| + | En este tema vamos a crear nuevo componentes de Angular | ||
| + | |||
| + | ===== Crear tu nuevo componente botón ===== | ||
| + | Vamos a crear nosotros un nuevo componente '' | ||
| + | |||
| + | |||
| + | Para ello vamos a crear los 3 ficheros que necesitas todo componente: | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | |||
| + | Los 3 ficheros los crearás dentro de la carpeta '' | ||
| + | |||
| + | <sxh text; | ||
| + | src/ | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | El contenido de cada uno de ellos será: | ||
| + | |||
| + | |||
| + | <sxh typescript; title: boton.ts> | ||
| + | import { Component | ||
| + | import {CommonModule} from ' | ||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | imports: [CommonModule], | ||
| + | templateUrl: | ||
| + | styleUrl: ' | ||
| + | }) | ||
| + | export class Boton { | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | La parte más importante es el objeto que se le pasa al decocador '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | |||
| + | <sxh typescript; title: boton.html> | ||
| + | <button class=" | ||
| + | </ | ||
| + | |||
| + | |||
| + | <sxh css; title: boton.scss> | ||
| + | .boton { | ||
| + | font-family: | ||
| + | font-size: 16px; | ||
| + | padding: 6px; | ||
| + | |||
| + | border-radius: | ||
| + | border-width: | ||
| + | border-style: | ||
| + | |||
| + | display: inline-block; | ||
| + | cursor: pointer; | ||
| + | text-decoration: | ||
| + | |||
| + | |||
| + | border-color: | ||
| + | background-color: | ||
| + | color: #ffffff; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== Usando un componente ===== | ||
| + | Como ya hicimos en el tema anterior, hay que importarlo en el '' | ||
| + | |||
| + | Primero lo importamos en el '' | ||
| + | |||
| + | |||
| + | Primero lo importamos en el '' | ||
| + | |||
| + | <sxh typescript; | ||
| + | import { Component, signal } from ' | ||
| + | import { RouterOutlet } from ' | ||
| + | import {MatButtonModule} from ' | ||
| + | import { Boton } from ' | ||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | imports: [RouterOutlet, | ||
| + | templateUrl: | ||
| + | styleUrl: ' | ||
| + | }) | ||
| + | export class App { | ||
| + | protected readonly title = signal(' | ||
| + | |||
| + | |||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | Y ahora ya podemos usar la etiquetya ''< | ||
| + | |||
| + | <sxh typescript; | ||
| + | < | ||
| + | </ | ||
| + | | ||
| + | <h1 >Hola mundo</ | ||
| + | | ||
| + | <button matButton=" | ||
| + | Aceptar | ||
| + | </ | ||
| + | |||
| + | <br> | ||
| + | <br> | ||
| + | |||
| + | < | ||
| + | | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ===== Contenido de la etiqueta ===== | ||
| + | Nuestros botones es muy pobre porque no permite indicar el texto del botón. Para poder hacerlo simplemente hay que usar la etiqueta ''< | ||
| + | |||
| + | |||
| + | <sxh typescript; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | Y ahora ya podremos cambiar el texto: | ||
| + | |||
| + | <sxh typescript; | ||
| + | < | ||
| + | </ | ||
| + | | ||
| + | <h1 >Hola mundo</ | ||
| + | | ||
| + | <button matButton=" | ||
| + | Aceptar | ||
| + | </ | ||
| + | |||
| + | <br> | ||
| + | <br> | ||
| + | |||
| + | < | ||
| + | | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | ===== Personalizar del componente ===== | ||
| + | Vamos ahora añadir atributos a nuestro componente para poder personalizarlo. Para ello vamos a añadir propiedades a la clase TypeScript del componente con el decorador '' | ||
| + | |||
| + | |||
| + | Nuestro objetivo es poder hacer lo siguiente: | ||
| + | <sxh html> | ||
| + | <boton [backgroundColor]="'# | ||
| + | </ | ||
| + | |||
| + | |||
| + | Lo primero es añadir las propiedades '' | ||
| + | |||
| + | <sxh typescript; | ||
| + | import { Component, Input } from ' | ||
| + | import {CommonModule} from ' | ||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | imports: [CommonModule], | ||
| + | templateUrl: | ||
| + | styleUrl: ' | ||
| + | }) | ||
| + | export class Boton { | ||
| + | @Input() backgroundColor: | ||
| + | @Input() color: string = '# | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Para usarlas en el '' | ||
| + | |||
| + | * Directamente interpolando los valores: | ||
| + | |||
| + | <sxh typescript; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | * Usando '' | ||
| + | |||
| + | <sxh typescript; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | * Usando '' | ||
| + | |||
| + | <sxh typescript; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | * Por último lo ideal siempre es usar clases CSS y evitar usar '' | ||
| + | |||
| + | <sxh typescript; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <sxh typescript; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <sxh typescript; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | Pero en ese caso hay que limitar los posibles valores, definir las clases, relacionarlas con las propiedades TypeScript. Esto lo veremos en el siguiente apartado. | ||
| + | |||
| + | ===== Limitando los valores ===== | ||
| + | En el apartado anterior hemos visto como definir los colores de los botones pero como vimos en el primer tema, realmente hay únicamente unos valores de colores que puede haber | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | |||
| + | Así que realmente lo que queremos es solo definir la función del botón.De forma que se use de la siguiente manera: | ||
| + | |||
| + | <sxh html; | ||
| + | <boton [funcion]="' | ||
| + | </ | ||
| + | |||
| + | |||
| + | * Por lo tanto el atributo del botón va a ser '' | ||
| + | |||
| + | <sxh typescript; | ||
| + | import {Component, Input} from ' | ||
| + | import {CommonModule} from ' | ||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | imports: [CommonModule], | ||
| + | templateUrl: | ||
| + | styleUrl: ' | ||
| + | }) | ||
| + | export class Boton { | ||
| + | @Input() funcion:' | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | * Ahora hay que definir las clases CSS en el archivo '' | ||
| + | |||
| + | <sxh css; | ||
| + | .boton { | ||
| + | font-family: | ||
| + | font-size: 16px; | ||
| + | padding: 6px; | ||
| + | |||
| + | border-radius: | ||
| + | border-width: | ||
| + | border-style: | ||
| + | |||
| + | display: inline-block; | ||
| + | cursor: pointer; | ||
| + | text-decoration: | ||
| + | |||
| + | |||
| + | border-color: | ||
| + | background-color: | ||
| + | color: #ffffff; | ||
| + | } | ||
| + | |||
| + | .funcion--normal { | ||
| + | border-color: | ||
| + | background-color: | ||
| + | color: #ffffff; | ||
| + | } | ||
| + | |||
| + | .funcion--alternativa { | ||
| + | border-color: | ||
| + | background-color: | ||
| + | color: #ffffff; | ||
| + | } | ||
| + | |||
| + | .funcion--peligrosa { | ||
| + | border-color: | ||
| + | background-color: | ||
| + | color: #ffffff; | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | * Y por último relacionamos la propiedad TypScript '' | ||
| + | |||
| + | <sxh html; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <note tip> | ||
| + | Notar el truco de '' | ||
| + | </ | ||
| + | |||
| + | * Otra forma alternativa sería: | ||
| + | |||
| + | <sxh html; | ||
| + | <button class=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ===== Acción de href ===== | ||
| + | Por último vamos a añadir acciones al botón, como el '' | ||
| + | |||
| + | |||
| + | Para crear el atributo '' | ||
| + | * Añadir la propiedad '' | ||
| + | * Usar la propiedad en el HTML | ||
| + | * Cambiar de tipo ''< | ||
| + | |||
| + | |||
| + | <sxh TypeScript; | ||
| + | import {Component, Input} from ' | ||
| + | import {CommonModule} from ' | ||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | imports: [CommonModule], | ||
| + | templateUrl: | ||
| + | styleUrl: ' | ||
| + | }) | ||
| + | export class Boton { | ||
| + | @Input() funcion:' | ||
| + | @Input() href: | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | <sxh html; | ||
| + | <a class=" | ||
| + | < | ||
| + | </a> | ||
| + | </ | ||
| + | |||
| + | Hay que destacar que además de usar ahora '' | ||
| + | |||
| + | Hemos puesto | ||
| + | [attr.href]=" | ||
| + | en vez de simplemente | ||
| + | href={{ href }} | ||
| + | porque si no hay nada en href, no se añadirá el atributo | ||
| + | |||
| + | ===== Acción onClick ===== | ||
| + | Queremos que se ejecute la función '' | ||
| + | |||
| + | |||
| + | <sxh typescript; | ||
| + | import { Component, signal } from ' | ||
| + | import { RouterOutlet } from ' | ||
| + | |||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | imports: [RouterOutlet], | ||
| + | templateUrl: | ||
| + | styleUrl: ' | ||
| + | }) | ||
| + | export class App { | ||
| + | protected readonly title = signal(' | ||
| + | |||
| + | alerta() : void { | ||
| + | alert(' | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | En el HTML lo indicaremos así | ||
| + | |||
| + | <sxh html; | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <h1 >Hola mundo</ | ||
| + | |||
| + | <button matButton=" | ||
| + | Aceptar | ||
| + | </ | ||
| + | |||
| + | <br> | ||
| + | <br> | ||
| + | |||
| + | <boton [funcion]="' | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | Para ello tenemos que hacer 3 cosas en el botón: | ||
| + | * Decir que vamos a emitir un evento | ||
| + | * Emitir el evento desde TypeScript | ||
| + | * Enlazar el click de ''< | ||
| + | |||
| + | |||
| + | |||
| + | <sxh typescript; | ||
| + | import {Component, EventEmitter, | ||
| + | import {CommonModule} from ' | ||
| + | |||
| + | @Component({ | ||
| + | selector: ' | ||
| + | imports: [CommonModule], | ||
| + | templateUrl: | ||
| + | styleUrl: ' | ||
| + | }) | ||
| + | export class Boton { | ||
| + | @Input() funcion:' | ||
| + | @Input() href: | ||
| + | @Output() onClick = new EventEmitter< | ||
| + | |||
| + | handleOnClick(): | ||
| + | this.onClick.emit(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | <sxh html; | ||
| + | <a class=" | ||
| + | < | ||
| + | </a> | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Filtros ===== | ||
| + | Angular permite modificar los datos cuando se interpolan en el html. | ||
| + | |||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | <sxh html> | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | <sxh html> | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | Los formatos predefinidos de fecha se pueden ver en [[https:// | ||
| + | ===== Ejercicios ===== | ||
| + | |||
| + | ==== Ejercicio 1 ==== | ||
| + | |||
| + | Usando el elemento HTML de [[https:// | ||
| + | |||
| + | <sxh html> | ||
| + | <label for=" | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | |||
| + | Crea el componente ''< | ||
| + | <sxh html> | ||
| + | < | ||
| + | |||
| + | <boton (onClick)=" | ||
| + | |||
| + | </ | ||
| + | |||
| + | y que muestre: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Añade en la página un botón de forma que al pulsarlo se incremente en 10 el valor. | ||
| + | |||
| + | <note tip> | ||
| + | Para generar los '' | ||
| + | |||
| + | <sxh bash> | ||
| + | npm install uuid | ||
| + | npm install --save-dev @types/uuid | ||
| + | </ | ||
| + | |||
| + | |||
| + | <sxh typescript> | ||
| + | import { v4 as uuidv4 } from ' | ||
| + | |||
| + | class MiClase{ | ||
| + | uniqueId: string; | ||
| + | |||
| + | constructor() { | ||
| + | this.uniqueId =uuidv4(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Ejercicio 2 ==== | ||
| + | Crea un componente llamado panel que permita hacer los siguientes paneles | ||
| + | |||
| + | {{: | ||
| + | |||
| + | |||
| + | Y crea una página HTML donde se vean los paneles. | ||
| + | |||
| + | |||
| + | ==== Ejercicio 3 ==== | ||
| + | |||
| + | Crea ahora un componente para crear los siguientes botones: | ||
| + | |||
| + | ^ | ||
| + | ^ Función | ||
| + | | Normal | ||
| + | | Alternativa | ||
| + | | Peligrosa | ||
