且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在angular 8中的Web Worker中导入打字稿模块?

更新时间:2022-12-23 12:06:05

我的问题是我尝试导入的共享模块是使用ng generate module创建的.这样,angular会自动导入NgModuleCommonModule,并向导出的类添加带有声明和导入的@NgModule.这些使您可以访问DOM,并且Web工作人员无法访问DOM.这就是我正在谈论的编译错误.即使我实际上并没有真正使用这些导入,但是这些都不是我将该模块导入到Webworker中的原因.无论如何,它们在那里,并且正在引起错误.

My problem was that this shared module I was trying to import was created with ng generate module. In doing so, angular automatically imports NgModule, CommonModule and adds an @NgModule with declarations and imports to the exported class. These give you access to the DOM, and web workers cannot have access to the DOM. Which is what the compile error I was getting was talking about. Even tho I wasn't really using these imports and none of these were the reason why I was importing this module into the webworker. They were there anyway and they were causing the error.

我通过简单地从共享模块中删除了NgModuleCommonModule和整个@NgModule的2条导入语句来解决了这个问题.然后,它完美无误地导入了网络工作者.

I fixed it by simply removing the 2 import statements for NgModule, CommonModule and the whole @NgModule from my shared module. Then it imported fine without any errors into the web worker.