2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# Background processing using web workers
  
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								# 用 Web Worker 处理后台进程
  
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								[Web workers ](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API ) allow you to run CPU-intensive computations in a background thread,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								freeing the main thread to update the user interface.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								If you find your application performs a lot of computations, such as generating CAD drawings or doing heavy geometrical calculations, using web workers can help increase your application's performance.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								[Web Worker ](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API )允许你在后台线程中运行 CPU 密集型计算,解放主线程以更新用户界面。如果你发现你的应用会进行很多计算,比如生成 CAD 图纸或进行繁重的几何计算,那么使用 Web Worker 可以帮助你提高应用的性能。
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "alert is-helpful" >  
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The CLI does not support running Angular itself in a web worker.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								CLI 不支持在 Web Worker 中运行 Angular。
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >  
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-02 12:47:19 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## Adding a web worker
  
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								## 添加一个 Web Worker
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								To add a web worker to an existing project, use the Angular CLI `ng generate`  command.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								要把 Web Worker 添加到现有项目中,请使用 Angular CLI `ng generate`  命令。
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`ng generate web-worker`  *location*  
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`ng generate web-worker`  *生成到的位置*  
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								You can add a web worker anywhere in your application.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								For example, to add a web worker to the root component, `src/app/app.component.ts` , run the following command.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								你可以在应用的任何位置添加 Web Worker。例如, `src/app/app.component.ts` ,请运行如下命令。
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-02 19:47:02 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								`ng generate web-worker app`  
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								The command performs the following actions.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								该命令会执行以下操作。
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								-  Configures your project to use web workers, if it isn't already. 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								  把你的项目配置为使用 Web Worker, 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								-  Adds the following scaffold code to `src/app/app.worker.ts`  to  receive messages. 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  把如下脚手架代码添加到 `src/app/app.worker.ts`  以接收消息。
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-example  language = "typescript"  header = "src/app/app.worker.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  addEventListener('message', ({ data }) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    const response = `worker response to ${data}` ;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    postMessage(response);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  });
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								 < / code-example > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								-  Adds the following scaffold code to `src/app/app.component.ts`  to use the worker. 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  把如下脚手架代码添加到 `src/app/app.component.ts`  以使用这个 Worker。
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < code-example  language = "typescript"  header = "src/app/app.component.ts" > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  if (typeof Worker !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // Create a new
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    const worker = new Worker('./app.worker', { type: 'module' });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    worker.onmessage = ({ data }) => {
							 
						 
					
						
							
								
									
										
										
										
											2019-06-26 15:04:06 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								      console.log(`page got message: ${data}` );
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    worker.postMessage('hello');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  } else {
							 
						 
					
						
							
								
									
										
										
										
											2019-12-02 12:47:19 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								    // Web workers are not supported in this environment.
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								    // You should add a fallback so that your program still executes correctly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2019-09-09 12:53:37 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								  < / code-example > 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-01 15:43:20 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								After you generate this initial scaffold, you must refactor your code to use the web worker by sending messages to and from the worker.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								生成这个初始脚手架之后,你必须把代码重构成向这个 Worker 发送消息和从 Worker 接收消息,以便使用 Web Worker。
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< div  class = "alert is-important" >  
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								Some environments or platforms, such as `@angular/platform-server`  used in [Server-side Rendering ](guide/universal ), don't support web workers. To ensure that your application will work in these environments, you must provide a fallback mechanism to perform the computations that the worker would otherwise perform.
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-10 16:08:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
										 
							
							
								某些环境或平台(比如[服务端渲染 ](guide/universal )中使用的 `@angular/platform-server`  不支持 Web Worker。为了确保你的应用能够在这些环境中工作, 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-05 16:17:03 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-03-11 10:14:10 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
								
									
								 
							
							
								< / div >