Import modules in a angular Worker?

javascript angular ionic-framework ionic3 web-worker

277 观看

1回复

18 作者的声誉

we are developing a ionic application. I created a service(.ts) and imported a geo-json-pathfinder. Initializing this pathfinder takes 10-20 seconds, so im supposed to outsource it into a Worker(.js).

import PathFinder from 'geojson-path-finder';

at the top of my worker.js returns an error:

SyntaxError: import declarations may only appear at top level of a module

How i can deal with it? And am i able to send this pathfinder to my ui thread like this?

let pathfinder : Pathfinder = new Pathfinder(geojson);
self.postMessage({obj: pathfinder})
作者: Blubb 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


1

25500 作者的声誉

There are two issues.

  1. Most likely, you cannot just pass the path finder like that. Read the documentation more carefully. You'll see that you should only pass the result of graph preprocessing. Check the sourcecode of the project, you can see how they're obtained.

    const preprocessedGraph = preprocess(graph, options);
    

    You will need to mess around with this a little to make it do what you need.

  2. To import ES6 modules into web worker, use the second parameter:

    const myWorker = new Worker("worker.js", "module");
    
作者: Tomáš Zato 发布者: 09.01.2018 11:50
32x32