我正在 Angular v8 项目中做我的第一个网络工作者。我想让我的网络工作者在那个单独的线程中进行 HTTP 服务调用。不幸的是,在 ng 构建过程中,我收到如下所示的编译器错误。特别是对于 V6 代码,我看到了建议使用 webpack 的指导。我编写代码就好像 Angular 将我的 Typescript 代码编译成一个 JavaScript 文件,它引用了 Angular 导入的服务(尽管只有后端),把它放在正确的 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 调用的方法。
帮助 :)
谢谢,樵夫