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
[Web workers](https://developer.mozilla.org/docs/Web/API/Web_Workers_API) let you run CPU-intensive computations in a background thread, freeing the main thread to update the user interface.
4
+
Application's performing a lot of computations, like generating Computer-Aided Design \(CAD\) drawings or doing heavy geometric calculations, can use web workers to increase performance.
5
+
6
+
HELPFUL: The Angular CLI does not support running itself in a web worker.
7
+
8
+
## Adding a web worker
9
+
10
+
To add a web worker to an existing project, use the Angular CLI `ng generate` command.
11
+
12
+
<docs-codelanguage="shell">
13
+
14
+
ng generate web-worker <location>
15
+
16
+
</docs-code>
17
+
18
+
You can add a web worker anywhere in your application.
19
+
For example, to add a web worker to the root component, `src/app/app.component.ts`, run the following command.
20
+
21
+
<docs-codelanguage="shell">
22
+
23
+
ng generate web-worker app
24
+
25
+
</docs-code>
26
+
27
+
The command performs the following actions.
28
+
29
+
1. Configures your project to use web workers, if it isn't already.
30
+
1. Adds the following scaffold code to `src/app/app.worker.ts` to receive messages.
const worker = new Worker(new URL('./app.worker', import.meta.url));
48
+
worker.onmessage = ({ data }) => {
49
+
console.log(`page got message: ${data}`);
50
+
};
51
+
worker.postMessage('hello');
52
+
} else {
53
+
// Web workers are not supported in this environment.
54
+
// You should add a fallback so that your program still executes correctly.
55
+
}
56
+
57
+
</docs-code>
58
+
59
+
After you create this initial scaffold, you must refactor your code to use the web worker by sending messages to and from the worker.
60
+
61
+
IMPORTANT: Some environments or platforms, such as `@angular/platform-server` used in [Server-side Rendering](guide/ssr), don't support web workers.
62
+
63
+
To ensure that your application works in these environments, you must provide a fallback mechanism to perform the computations that the worker would otherwise perform.
[Web workers](https://developer.mozilla.org/docs/Web/API/Web_Workers_API)let you run CPU-intensive computations in a background thread, freeing the main thread to update the user interface.
4
-
Application's performing a lot of computations, like generating Computer-Aided Design \(CAD\) drawings or doing heavy geometric calculations, can use web workers to increase performance.
3
+
Los [web workers](https://developer.mozilla.org/es/docs/Web/API/Web_Workers_API)te permiten ejecutar cálculos con uso intensivo de CPU en un hilo en segundo plano, liberando el hilo principal para actualizar la interfaz de usuario.
4
+
Las aplicaciones que realizan muchos cálculos, como generar dibujos de diseño asistido por computadora (CAD) o llevar a cabo operaciones geométricas complejas, pueden usar web workers para aumentar el rendimiento.
5
5
6
-
HELPFUL: The Angular CLI does not support running itself in a web worker.
6
+
ÚTIL: Angular CLI no permite ejecutarse a sí mismo dentro de un web worker.
7
7
8
-
## Adding a web worker
8
+
## Agregar un web worker
9
9
10
-
To add a web worker to an existing project, use the Angular CLI `ng generate`command.
10
+
Para agregar un web worker a un proyecto existente, usa el comando `ng generate`de Angular CLI.
11
11
12
12
<docs-codelanguage="shell">
13
13
14
14
ng generate web-worker <location>
15
15
16
16
</docs-code>
17
17
18
-
You can add a web worker anywhere in your application.
19
-
For example, to add a web worker to the root component, `src/app/app.component.ts`, run the following command.
18
+
Puedes agregar un web worker en cualquier parte de tu aplicación.
19
+
Por ejemplo, para agregar un web worker al componente raíz, `src/app/app.component.ts`, ejecuta el siguiente comando.
20
20
21
21
<docs-codelanguage="shell">
22
22
23
23
ng generate web-worker app
24
24
25
25
</docs-code>
26
26
27
-
The command performs the following actions.
27
+
El comando realiza las siguientes acciones.
28
28
29
-
1.Configures your project to use web workers, if it isn't already.
30
-
1.Adds the following scaffold code to`src/app/app.worker.ts`to receive messages.
29
+
1.Configura tu proyecto para usar web workers, si aún no lo estaba.
30
+
1.Agrega el siguiente código base en`src/app/app.worker.ts`para recibir mensajes.
const worker = new Worker(new URL('./app.worker', import.meta.url));
48
48
worker.onmessage = ({ data }) => {
49
49
console.log(`page got message: ${data}`);
50
50
};
51
51
worker.postMessage('hello');
52
52
} else {
53
-
// Web workers are not supported in this environment.
54
-
// You should add a fallback so that your program still executes correctly.
53
+
// Web workers no están soportados en este entorno.
54
+
// Debes agregar un plan alternativo para que tu programa siga funcionando correctamente.
55
55
}
56
56
57
57
</docs-code>
58
58
59
-
After you create this initial scaffold, you must refactor your code to use the web worker by sending messages to and from the worker.
59
+
Después de crear este siguiente código base, debes refactorizar tu código para usar el web worker enviando y recibiendo mensajes.
60
60
61
-
IMPORTANT: Some environments or platforms, such as `@angular/platform-server`used in [Server-side Rendering](guide/ssr), don't support web workers.
61
+
IMPORTANTE: Algunos entornos o plataformas, como `@angular/platform-server`utilizado en el [renderizado del lado del servidor](guide/ssr), no soportan web workers.
62
62
63
-
To ensure that your application works in these environments, you must provide a fallback mechanism to perform the computations that the worker would otherwise perform.
63
+
Para asegurarte de que tu aplicación funcione en estos entornos, debes proporcionar un mecanismo alternativo para realizar los cálculos que, de otro modo, ejecutaría el worker.
0 commit comments