ng generate service nombre-del-servicio
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root', // Hace que el servicio sea singleton en toda la aplicación
})
export class MiServicio {
constructor() {}
obtenerDatos() {
return ['dato1', 'dato2', 'dato3'];
}
}
import { Component } from '@angular/core';
import { MiServicio } from './mi-servicio.service';
@Component({
selector: 'app-mi-componente',
template: `
<ul>
<li *ngFor="let dato of datos"></li>
</ul>
`,
})
export class MiComponente {
datos: string[];
constructor(private miServicio: MiServicio) {
this.datos = this.miServicio.obtenerDatos();
}
}
ng generate service data
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class DataService {
private apiUrl = 'https://api.example.com/datos';
constructor(private http: HttpClient) {}
obtenerDatos(): Observable<any[]> {
return this.http.get<any[]>(this.apiUrl);
}
agregarDato(dato: any): Observable<any> {
return this.http.post<any>(this.apiUrl, dato);
}
eliminarDato(id: number): Observable<any> {
return this.http.delete<any>(`${this.apiUrl}/${id}`);
}
}
import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-datos',
template: `
<ul>
<li *ngFor="let dato of datos"></li>
</ul>
`,
})
export class DatosComponent implements OnInit {
datos: any[] = [];
constructor(private dataService: DataService) {}
ngOnInit() {
this.dataService.obtenerDatos().subscribe((datos) => {
this.datos = datos;
});
}
}
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class AuthService {
isLoggedIn = false;
login() {
this.isLoggedIn = true;
}
logout() {
this.isLoggedIn = false;
}
}
import { Injectable } from '@angular/core';
import { AuthService } from './auth.service';
@Injectable({
providedIn: 'root',
})
export class UserService {
constructor(private authService: AuthService) {}
getUserStatus() {
return this.authService.isLoggedIn;
}
}
import { Component } from '@angular/core';
import { UserService } from './user.service';
@Component({
selector: 'app-user-status',
template: `
<p>User is </p>
`,
})
export class UserStatusComponent {
userStatus: boolean;
constructor(private userService: UserService) {
this.userStatus = this.userService.getUserStatus();
}
}
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class DataService {
private mensajeSource = new BehaviorSubject<string>('Mensaje inicial');
currentMensaje = this.mensajeSource.asObservable();
cambiarMensaje(mensaje: string) {
this.mensajeSource.next(mensaje);
}
}
import { Component } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-componente1',
template: `
<input [(ngModel)]="nuevoMensaje" />
<button (click)="enviarMensaje()">Enviar Mensaje</button>
`,
})
export class Componente1 {
nuevoMensaje: string;
constructor(private dataService: DataService) {}
enviarMensaje() {
this.dataService.cambiarMensaje(this.nuevoMensaje);
}
}
import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-componente2',
template: `<p>Mensaje recibido: </p>`,
})
export class Componente2 implements OnInit {
mensaje: string;
constructor(private dataService: DataService) {}
ngOnInit() {
this.dataService.currentMensaje.subscribe((mensaje) => {
this.mensaje = mensaje;
});
}
}