3

I´m testing Web Workers and combining them with Angular 7+

This is my app.component.html

<div>
  <input type="text" name="lat" placeholder="Latitude" #lat (input)="updateMap( lat )">
  <input type="text" name="lng" placeholder="Longitude" #lng (input)="updateMap( lng )">
</div>

This is my app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'mapa';
  coords: any = {lat: 0, lng: 0};
  worker: Worker;

  ngOnInit() {
      this.worker = new Worker( './app.component.worker.js' );
  }

  updateMap( selector: any ) {
    const key   = selector.getAttribute('name');
    const value = Number(selector.value);
    this.coords[ key ] = value;

    this.worker.postMessage( this.coords );
  }
}

This is my Webworker definition app.component.worker.js

onmessage = ( e ) => {
    console.log( e );
};

All the files are at the same level.

project
 -e2e
 -node_modules
 -src
   -app
     -app-routing.module.ts
     -app.component.css
     -app.component.html
     -app.component.ts
     -app.component.worker.js
   -lot of files
 -lot of files

When the local server runs, appears this message

GET http://localhost:4200/app.component.worker.js 404 (Not Found)

I tried to set as relative path

'./src/app/app.component.worker.js'

But stills not working

What is wrong with my code? Does exists a best way to do all with Typescript?

1
  • Exactly the same problem, I even include the script path in angular.json script but still do not work.. Have you got any updates? Commented Oct 23, 2019 at 6:24

3 Answers 3

7

Do not forget to run: ng generate web-worker And then restart the angular serve , if not ,it will not compile your worker file into webpack.

Sign up to request clarification or add additional context in comments.

1 Comment

This did it for me. Followed the Angular docs instructions, but did not think to restart ng serve to fix the errors. Thanks!
5

I was also facing same issue with this code

const worker = new Worker('./web-worker/messenger.worker');

Change to

const worker = new Worker('./web-worker/messenger.worker', { type: `module` });

After this I am not getting this issue with web worker.

I am using this code on Angular 8.

2 Comments

If this answer helpfull Please rate up. Thank you
For me this introduced all sorts of type conflicts. One way I've gotten a worker running under Angular quickly is serving the worker as a .js file from the assets folder.
2

For me it just worked by putting the path src/app/web-worker/messenger.worker.

Then try the following:

const worker = new Worker('src/app/web-worker/messenger.worker', { type: `module` });

1 Comment

+1 but why have you mentioned like { type: module } you can simply write { type: 'module' }. Tilde isnt required

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.