在Web Worker中使用角度模块 [英] using angular modules in web workers

查看:98
本文介绍了在Web Worker中使用角度模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WEB WORKER中使用angular 8 jit编译器,但是在尝试导入Compiler模块或web-worker.ts文件中的任何其他angular模块时遇到错误

I am trying to use angular 8 jit compler in WEB WORKER but getting an error while trying to import the Compiler module or any other angular module in web-worker.ts file

/// <reference lib="webworker" />
import {Compiler } from '@angular/core';

addEventListener('message', ({ data }) =>
 {
  let response = `worker response to ` + data  ;


});       

node_modules/@angular/core/core.d.ts:13052:20-错误TS2304:找不到名称文档".

node_modules/@angular/core/core.d.ts:13052:20 - error TS2304: Cannot find name 'Document'.

推荐答案

Web worker无法访问DOM,因此窗口上的 document 对象不可用,或者任何其他导入的模块.请参阅 answer 的引文,该引文由TJ拥挤者:

Web worker doesn't have any access to the DOM, so the document object on the window isn't available for you or any other imported module. See this quote from this well-explained answer by T.J. Crowder:

服务工作者(通常是Web工作者)根本没有直接访问DOM的权限.而是让工作人员将信息发布到主线程,并让主线程中的代码酌情更新DOM.浏览器上JavaScript的线程模型是,只有一个主UI线程(您的页内代码在其上运行的默认线程)可以访问DOM.其他的都被隔离了.

Service workers — web workers in general — don't have direct access to the DOM at all. Instead, have the worker post the information to the main thread, and have code in the main thread update the DOM as appropriate. The threading model for JavaScript on browsers is that there is only one main UI thread (the default one your in-page code runs on), which can access the DOM. The others are walled off from it.

此外,不要忘记将您的工作程序配置为模块类型:

In addition, don't forget to configure your worker as module type:

@Injectable()
export class SomeWorkerService {
  private readonly worker = new Worker('./custom.worker', { type: 'module' }); 
  ...
}

这篇关于在Web Worker中使用角度模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆