Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions adev-es/src/app/routing/sub-navigation-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,13 @@ const DOCS_SUB_NAVIGATION_DATA: NavigationItem[] = [
status: 'updated',
},
{
label: 'Creating and using services',
label: 'Crando y usando servicios',
path: 'guide/di/creating-and-using-services',
contentPath: 'guide/di/creating-and-using-services',
status: 'updated',
},
{
label: 'Defining dependency providers',
label: 'Definiendo proveedores de dependencias',
path: 'guide/di/defining-dependency-providers',
contentPath: 'guide/di/defining-dependency-providers',
status: 'updated',
Expand Down
105 changes: 105 additions & 0 deletions adev-es/src/content/guide/di/creating-and-using-services.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Creating and using services

Services are reusable pieces of code that can be shared across your Angular application. They typically handle data fetching, business logic, or other functionality that multiple components need to access.

## Creating a service

You can create a service with the [Angular CLI](tools/cli) with the following command:

```bash
ng generate service CUSTOM_NAME
```

This creates a dedicated `CUSTOM_NAME.ts` file in your `src` directory.

You can also manually create a service by adding the `@Injectable()` decorator to a TypeScript class. This tells Angular that the service can be injected as a dependency.

Here is an example of a service that allows users to add and request data:

```ts
// 📄 src/app/basic-data-store.ts
import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class BasicDataStore {
private data: string[] = []

addData(item: string): void {
this.data.push(item)
}

getData(): string[] {
return [...this.data]
}
}
```

## How services become available

When you use `@Injectable({ providedIn: 'root' })` in your service, Angular:

- **Creates a single instance** (singleton) for your entire application
- **Makes it available everywhere** without any additional configuration
- **Enables tree-shaking** so the service is only included in your JavaScript bundle if it's actually used

This is the recommended approach for most services.

## Injecting a service

Once you've created a service with `providedIn: 'root'`, you can inject it anywhere in your application using the `inject()` function from `@angular/core`.

### Injecting into a component

```angular-ts
import { Component, inject } from '@angular/core';
import { BasicDataStore } from './basic-data-store';

@Component({
selector: 'app-example',
template: `
<div>
<p>{{ dataStore.getData() }}</p>
<button (click)="dataStore.addData('More data')">
Add more data
</button>
</div>
`
})
export class ExampleComponent {
dataStore = inject(BasicDataStore);
}
```

### Injecting into another service

```ts
import { inject, Injectable } from '@angular/core';
import { AdvancedDataStore } from './advanced-data-store';

@Injectable({
providedIn: 'root',
})
export class BasicDataStore {
private advancedDataStore = inject(AdvancedDataStore);
private data: string[] = [];

addData(item: string): void {
this.data.push(item);
}

getData(): string[] {
return [...this.data, ...this.advancedDataStore.getData()];
}
}
```

## Next steps

While `providedIn: 'root'` covers most use cases, Angular offers additional ways to provide services for specialized scenarios:

- **Component-specific instances** - When components need their own isolated service instances
- **Manual configuration** - For services that require runtime configuration
- **Factory providers** - For dynamic service creation based on runtime conditions
- **Value providers** - For providing configuration objects or constants

You can learn more about these advanced patterns in the next guide: [defining dependency providers](/guide/di/defining-dependency-providers).
48 changes: 24 additions & 24 deletions adev-es/src/content/guide/di/creating-and-using-services.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Creating and using services
# Creando y usando servicios

Services are reusable pieces of code that can be shared across your Angular application. They typically handle data fetching, business logic, or other functionality that multiple components need to access.
Los servicios son piezas de código reutilizables que pueden compartirse a través de tu aplicación Angular. Típicamente manejan la obtención de datos, lógica de negocio u otra funcionalidad que múltiples componentes necesitan acceder.

## Creating a service
## Creando un servicio

You can create a service with the [Angular CLI](tools/cli) with the following command:
Puedes crear un servicio con el [Angular CLI](tools/cli) con el siguiente comando:

```bash
ng generate service CUSTOM_NAME
```

This creates a dedicated `CUSTOM_NAME.ts` file in your `src` directory.
Esto crea un archivo dedicado `CUSTOM_NAME.ts` en tu directorio `src`.

You can also manually create a service by adding the `@Injectable()` decorator to a TypeScript class. This tells Angular that the service can be injected as a dependency.
También puedes crear manualmente un servicio añadiendo el decorador `@Injectable()` a una clase TypeScript. Esto le indica a Angular que el servicio puede ser inyectado como una dependencia.

Here is an example of a service that allows users to add and request data:
Aquí hay un ejemplo de un servicio que permite a los usuarios agregar y solicitar datos:

```ts
// 📄 src/app/basic-data-store.ts
Expand All @@ -34,21 +34,21 @@ export class BasicDataStore {
}
```

## How services become available
## Cómo los servicios se vuelven disponibles

When you use `@Injectable({ providedIn: 'root' })` in your service, Angular:
Cuando usas `@Injectable({ providedIn: 'root' })` en tu servicio, Angular:

- **Creates a single instance** (singleton) for your entire application
- **Makes it available everywhere** without any additional configuration
- **Enables tree-shaking** so the service is only included in your JavaScript bundle if it's actually used
- **Crea una única instancia** (singleton) para toda tu aplicación
- **Lo hace disponible en todas partes** sin ninguna configuración adicional
- **Habilita tree-shaking** para que el servicio solo se incluya en tu bundle de JavaScript si realmente se usa

This is the recommended approach for most services.
Este es el enfoque recomendado para la mayoría de los servicios.

## Injecting a service
## Inyectando un servicio

Once you've created a service with `providedIn: 'root'`, you can inject it anywhere in your application using the `inject()` function from `@angular/core`.
Una vez que has creado un servicio con `providedIn: 'root'`, puedes inyectarlo en cualquier parte de tu aplicación usando la función `inject()` de `@angular/core`.

### Injecting into a component
### Inyectando en un componente

```angular-ts
import { Component, inject } from '@angular/core';
Expand All @@ -70,7 +70,7 @@ export class ExampleComponent {
}
```

### Injecting into another service
### Inyectando en otro servicio

```ts
import { inject, Injectable } from '@angular/core';
Expand All @@ -93,13 +93,13 @@ export class BasicDataStore {
}
```

## Next steps
## Próximos pasos

While `providedIn: 'root'` covers most use cases, Angular offers additional ways to provide services for specialized scenarios:
Aunque `providedIn: 'root'` cubre la mayoría de los casos de uso, Angular ofrece formas adicionales de proveer servicios para escenarios especializados:

- **Component-specific instances** - When components need their own isolated service instances
- **Manual configuration** - For services that require runtime configuration
- **Factory providers** - For dynamic service creation based on runtime conditions
- **Value providers** - For providing configuration objects or constants
- **Instancias específicas de componente** - Cuando los componentes necesitan sus propias instancias aisladas de servicio
- **Configuración manual** - Para servicios que requieren configuración en tiempo de ejecución
- **Proveedores factory** - Para creación dinámica de servicios basada en condiciones de tiempo de ejecución
- **Proveedores de valor** - Para proveer objetos de configuración o constantes

You can learn more about these advanced patterns in the next guide: [defining dependency providers](/guide/di/defining-dependency-providers).
Puedes aprender más sobre estos patrones avanzados en la siguiente guía: [definiendo proveedores de dependencias](/guide/di/defining-dependency-providers).
Loading