How to import a typescript module inside a Web Worker in angular 8?

∥☆過路亽.° 提交于 2019-12-11 10:02:31

问题


Since Angular 8, you can now generate web worker to your app from the CLI. I did so exactly like the official guide:

https://angular.io/guide/web-worker

And it works perfectly.

But as soon as I try to import any module to the top of app.worker.ts with:

import { MyData } from '../shared/shared.module';

Then I get compile errors:

Is there any other way to import modules into web workers?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/56616120/how-to-import-a-typescript-module-inside-a-web-worker-in-angular-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!