You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Service is a broad category encompassing any value, function, or feature that an application needs.
4
+
A service is typically a class with a narrow, well-defined purpose.
5
+
A component is one type of class that can use DI.
6
+
7
+
Angular distinguishes components from services to increase modularity and reusability.
8
+
By separating a component's view-related features from other kinds of processing, you can make your component classes lean and efficient.
9
+
10
+
Ideally, a component's job is to enable the user experience and nothing more.
11
+
A component should present properties and methods for data binding, to mediate between the view (rendered by the template) and the application logic (which often includes some notion of a model).
12
+
13
+
A component can delegate certain tasks to services, such as fetching data from the server, validating user input, or logging directly to the console.
14
+
By defining such processing tasks in an injectable service class, you make those tasks available to any component.
15
+
You can also make your application more adaptable by configuring different providers of the same kind of service, as appropriate in different circumstances.
16
+
17
+
Angular does not enforce these principles.
18
+
Angular helps you follow these principles by making it easy to factor your application logic into services and make those services available to components through DI.
19
+
20
+
## Service examples
21
+
22
+
Here's an example of a service class that logs to the browser console:
For clarity and maintainability, it is recommended that you define components and services in separate files.
101
+
102
+
## Injecting services
103
+
104
+
To inject a service as a dependency into a component, you can declare a class field representing the dependency and use Angular's `inject` function to initialize it.
105
+
106
+
The following example specifies the `HeroService` in the `HeroListComponent`.
The `inject` method can be used in both classes and functions, while the constructor method can naturally only be used in a class constructor. However, in either case a dependency may only be injected in a valid [injection context](guide/di/dependency-injection-context), usually in the construction or initialization of a component.
124
+
125
+
## Injecting services in other services
126
+
127
+
When a service depends on another service, follow the same pattern as injecting into a component.
128
+
In the following example, `HeroService` depends on a `Logger` service to report its activities:
Service is a broad category encompassing any value, function, or feature that an application needs.
4
-
A service is typically a class with a narrow, well-defined purpose.
5
-
A component is one type of class that can use DI.
3
+
Un servicio es una categoría amplia que abarca cualquier valor, función o característica que una aplicación necesita.
4
+
Un servicio es típicamente una clase con un propósito específico y bien definido.
5
+
Un componente es un tipo de clase que puede usar DI.
6
6
7
-
Angular distinguishes components from services to increase modularity and reusability.
8
-
By separating a component's view-related features from other kinds of processing, you can make your component classes lean and efficient.
7
+
Angular distingue los componentes de los servicios para aumentar la modularidad y reutilización.
8
+
Al separar las características relacionadas con la vista de un componente de otros tipos de procesamiento, puedes hacer que tus clases de componente sean eficientes y ligeras.
9
9
10
-
Ideally, a component's job is to enable the user experience and nothing more.
11
-
A component should present properties and methods for data binding, to mediate between the view (rendered by the template) and the application logic (which often includes some notion of a model).
10
+
Idealmente, el trabajo de un componente es habilitar la experiencia del usuario y nada más.
11
+
Un componente debe presentar propiedades y métodos para el enlace de datos, para mediar entre la vista (renderizada por la plantilla) y la lógica de la aplicación (que a menudo incluye alguna noción de un modelo).
12
12
13
-
A component can delegate certain tasks to services, such as fetching data from the server, validating user input, or logging directly to the console.
14
-
By defining such processing tasks in an injectable service class, you make those tasks available to any component.
15
-
You can also make your application more adaptable by configuring different providers of the same kind of service, as appropriate in different circumstances.
13
+
Un componente puede delegar ciertas tareas a los servicios, como obtener datos del servidor, validar la entrada del usuario o registrar directamente en la consola.
14
+
Al definir tales tareas de procesamiento en una clase de servicio inyectable, haces que esas tareas estén disponibles para cualquier componente.
15
+
También puedes hacer que tu aplicación sea más adaptable configurando diferentes proveedores del mismo tipo de servicio, según sea apropiado en diferentes circunstancias.
16
16
17
-
Angular does not enforce these principles.
18
-
Angular helps you follow these principles by making it easy to factor your application logic into services and make those services available to components through DI.
17
+
Angular no hace cumplir estos principios.
18
+
Angular te ayuda a seguir estos principios haciendo que sea fácil factorizar la lógica de tu aplicación en servicios y hacer que esos servicios estén disponibles para los componentes a través de DI.
19
19
20
-
## Service examples
20
+
## Ejemplos de servicios
21
21
22
-
Here's an example of a service class that logs to the browser console:
22
+
Aquí tienes un ejemplo de una clase de servicio que registra en la consola del navegador:
For clarity and maintainability, it is recommended that you define components and services in separate files.
100
+
Para claridad y mantenibilidad, se recomienda que definas componentes y servicios en archivos separados.
101
101
102
-
## Injecting services
102
+
## Inyectando servicios
103
103
104
-
To inject a service as a dependency into a component, you can declare a class field representing the dependency and use Angular's `inject`function to initialize it.
104
+
Para inyectar un servicio como dependencia en un componente, puedes declarar un campo de clase que represente la dependencia y usar la función `inject`de Angular para inicializarlo.
105
105
106
-
The following example specifies the`HeroService`in the`HeroListComponent`.
107
-
The type of`heroService`is`HeroService`.
106
+
El siguiente ejemplo especifica el`HeroService`en el`HeroListComponent`.
The `inject`method can be used in both classes and functions, while the constructor method can naturally only be used in a class constructor. However, in either case a dependency may only be injected in a valid [injection context](guide/di/dependency-injection-context), usually in the construction or initialization of a component.
123
+
El método `inject`puede ser usado tanto en clases como en funciones, mientras que el método constructor naturalmente solo puede ser usado en un constructor de clase. Sin embargo, en cualquier caso una dependencia solo puede ser inyectada en un [contexto de inyección](guide/di/dependency-injection-context) válido, usualmente en la construcción o inicialización de un componente.
124
124
125
-
## Injecting services in other services
125
+
## Inyectando servicios en otros servicios
126
126
127
-
When a service depends on another service, follow the same pattern as injecting into a component.
128
-
In the following example, `HeroService`depends on a `Logger`service to report its activities:
127
+
Cuando un servicio depende de otro servicio, sigue el mismo patrón que inyectar en un componente.
128
+
En el siguiente ejemplo, `HeroService`depende de un servicio `Logger`para reportar sus actividades:
0 commit comments