如何在angular 8中的Web Worker中导入打字稿模块? [英] How to import a typescript module inside a Web Worker in angular 8?

查看:159
本文介绍了如何在angular 8中的Web Worker中导入打字稿模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自Angular 8起,您现在可以从CLI生成Web Worker到您的应用程序.我的做法与官方指南完全一样:

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

而且效果很好.

但是,一旦我尝试使用以下方式将任何模块导入到app.worker.ts的顶部:

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:

还有其他方法可以将模块导入到Web Worker中吗?

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

推荐答案

我的问题是我尝试导入的共享模块是使用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.

这篇关于如何在angular 8中的Web Worker中导入打字稿模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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