在 Angular V8 中构建 Web Worker 时未找到核心 Angular 对象

问题描述 投票:0回答:1

我正在 Angular v8 项目中做我的第一个 Web Worker。我想让我的网络工作者在那个单独的线程中进行 HTTP 服务调用。不幸的是,在 ng 构建过程中,我收到编译器错误,如下所示。特别是对于 V6 代码,我看到了建议搞乱 webpack 的指导。

我编写的代码就好像 Angular 编译我的 Typescript 代码一样,该代码将 Angular 导入的服务(仅后端)引用到 JavaScript 文件中,将其放入正确的 webpack(如果相关的话)中,新的 webworker() 构造函数将获取它的内容需要。

ERROR in ./src/app/modules/softlayer/bluereport-uploaders/report-uploader/softcopy-report-file-uploader.worker.ts (./node_modules/worker-plugin/dist/loader.js!./src/app/modules/softlayer/bluereport-uploaders/report-uploader/softcopy-report-file-uploader.worker.ts)
Module build failed (from ./node_modules/worker-plugin/dist/loader.js):
Error: node_modules/@angular/common/http/http.d.ts(1,1): error TS6053: File '/mnt/c/projects-new/cirrus-bluecost-rules-client/client/node_modules/@angular/common/http/http.ngfactory.ts' not found.
node_modules/@angular/core/core.d.ts(1,1): error TS6053: File '/mnt/c/projects-new/cirrus-bluecost-rules-client/client/node_modules/@angular/core/core.ngfactory.ts' not found.
node_modules/@angular/core/core.d.ts(1470,29): error TS2304: Cannot find name 'Element'.
node_modules/@angular/core/core.d.ts(1471,29): error TS2304: Cannot find name 'Element'.
node_modules/@angular/core/core.d.ts(1538,26): error TS2304: Cannot find name 'Node'.
node_modules/@angular/core/core.d.ts(1539,29): error TS2304: Cannot find name 'Node'.
node_modules/@angular/core/core.d.ts(6426,33): error TS2304: Cannot find name 'Node'.
node_modules/@angular/core/core.d.ts(9552,62): error TS2304: Cannot find name 'Element'.
node_modules/@angular/core/core.d.ts(9554,62): error TS2304: Cannot find name 'Node'.
node_modules/@angular/core/core.d.ts(9577,59): error TS2304: Cannot find name 'Element'.
node_modules/@angular/core/core.d.ts(10050,83): error TS2304: Cannot find name 'HTMLElement'.
node_modules/@angular/core/core.d.ts(13030,20): error TS2304: Cannot find name 'Document'.
node_modules/@angular/core/core.d.ts(13033,13): error TS2304: Cannot find name 'HTMLElement'.
node_modules/@angular/core/core.d.ts(13041,20): error TS2304: Cannot find name 'Document'.
node_modules/@angular/core/core.d.ts(13044,13): error TS2304: Cannot find name 'Document'.
node_modules/@angular/core/core.d.ts(13052,20): error TS2304: Cannot find name 'Document'.
node_modules/@angular/core/core.d.ts(13055,13): error TS2304: Cannot find name 'Window'.
src/app/core/services/softlayer.service.ts(1,1): error TS6053: File '/mnt/c/projects-new/cirrus-bluecost-rules-client/client/src/app/core/services/softlayer.service.ngfactory.ts' not found.
src/app/modules/softlayer/bluereport-uploaders/report-uploader/softcopy-report-file-uploader.worker.ts(16,37): error TS2339: Property 'data' does not exist on type 'Event'.

    at AngularCompilerPlugin._update (/mnt/c/projects-new/cirrus-bluecost-rules-client/client/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:776:31)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async AngularCompilerPlugin._make (/mnt/c/projects-new/cirrus-bluecost-rules-client/client/node_modules/@ngtools/webpack/src/angular_compiler_plugin.js:667:13)

我的网络工作者文件是:

/// <reference lib="webworker" />
import { SoftlayerService } from '../../../../core/services/softlayer.service';
import { SpringBatchJobExecutionStatus } from '../../../../shared/models/SpringBatchJobExecutionStatus';

function sleep(ms: number) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}

function postMessageWithLog(message: SpringBatchJobExecutionStatus) {
    console.log('Sending message back: ' + JSON.stringify(message));
    postMessage(message);
}

self.addEventListener('message', ({ data }) => {
    console.log('@@@ Begin addEventListener');
    const softLayerService: SoftlayerService = data['softlayerService'];
    const files: FileList = data['files'];
    (async () => {
        softLayerService.postAsyncFiles(files.item(0))
            .subscribe((postAsyncFilesData: SpringBatchJobExecutionStatus) => {
                console.log('@@@ http success handler for postAsyncFiles');
                postAsyncFilesData.httpRequestSuccessful = true;
                postMessageWithLog(postAsyncFilesData);

                if (postAsyncFilesData.exitStatus === 'RUNNING' || postAsyncFilesData.batchStatus === 'STARTING') {
                    console.log('Beginning jobExecutionResult fetch loop');
                    let errorDetected = false;``
                    let jobExecutionResult = postAsyncFilesData;
                    while (jobExecutionResult.batchStatus === 'RUNNING' ||
                        jobExecutionResult.batchStatus === 'STARTING' &&
                        !errorDetected) {
                        console.log('@@@ jobExecutionResult=' + JSON.stringify(jobExecutionResult));
                        postMessageWithLog(jobExecutionResult);
                        console.log('Before sleep');
                        sleep(10000);
                        console.log('After sleep');
                        softLayerService.getAsyncFiles(jobExecutionResult.jobInstanceId, jobExecutionResult.jobExecutionId).subscribe(
                            (getAsyncFilesData: SpringBatchJobExecutionStatus) => {
                                jobExecutionResult = getAsyncFilesData;
                                postMessageWithLog(jobExecutionResult);
                            },
                            (error) => {
                                console.log('Error getting job execution status ' + error);
                                let errorBlock = new SpringBatchJobExecutionStatus();
                                errorBlock.httpRequestSuccessful = false;
                                postMessageWithLog(errorBlock);
                                errorDetected = true;
                            }
                        );
                        console.log('After end jobExecutionResult fetch loop');
                    } /* end while */

                } else {
                    let errorBlock = postAsyncFilesData;
                    errorBlock.httpRequestSuccessful = false;
                    console.log('Not entering check loop batchStatus=' +
                        errorBlock.batchStatus +
                        ' exitStatus=' + errorBlock.exitStatus);
                    postMessageWithLog(errorBlock);
                }

            },
                (error) => {
                    let errorBlock = new SpringBatchJobExecutionStatus();
                    errorBlock.httpRequestSuccessful = false;
                    postMessageWithLog(errorBlock);
                });

    }
    )();
    console.log('@@@ addEventListener(...): worker ended');
});

请原谅console.log 语句。一旦测试了其关键方面,其中大多数将被删除。

SoftlayerService 类包含(最终)通过 RXJS 进行 RESTful 调用的方法。

angular typescript web-worker angular-webpack
1个回答
0
投票

我通过添加 tsconfig.json 解决了这个问题: “角度编译器选项”:{ “skipLibCheck”:正确, “fullTemplateTypeCheck”:true, “严格注入参数”:true }

我认为这是skipLibCheck:true,但不能确定。

© www.soinside.com 2019 - 2024. All rights reserved.