如何使用Nativescript worker与打字稿?

时间:2017-10-17 07:06:13

标签: typescript worker angular2-nativescript

我试图将Nativescript Worker与Typescript一起使用,但我遇到了这个错误:

  

错误错误:com.tns.NativeScriptException:找不到模块:" ../ shared / workers / workername.js",相对于:app //

这是我的代码,我使用与Nativescript文档中相同的模板:

var worker = new Worker("../shared/workers/workername");
    worker.onmessage = (msg) => {
        if(msg.data.success) {

            console.log("resultat: " + msg.data.result);
            worker.terminate();
        }
        else {
            console.log("error during process !!!!!");
        }
    };

    worker.onerror = (err) => {
        console.log(`An unhandled error occurred in worker: ${err.filename}, line: ${err.lineno} :`);
        console.log(err.message);
    }

    worker.postMessage({url: "", filename: "up2"});

和我的workername.js:

require('globals'); // necessary to bootstrap tns modules on the new thread

global.onmessage = function(msg) {
    var request = msg.data;
    var url = request.url;
    var filename = request.filename;

    var result = download(url, filename);

    var msg = result !== undefined ? { success: true, result: result } : { }

    global.postMessage(msg);
}


function download(url, name) {
    return ...
}

1 个答案:

答案 0 :(得分:0)

我用同样的方式解释了here 并且不要忘记像这样配置你的 Webpack 配置文件:

new CopyWebpackPlugin([
                { from: { glob: 'css/**', dot: false } },
               -> { from: { glob: 'shared/workers/**', dot: false } },
                { from: { glob: 'images/**', dot: false } },
                { from: { glob: 'fonts/**', dot: false } },
                { from: { glob: 'sounds/**', dot: false } },
                { from: { glob: '**/*.jpg', dot: false } },
                { from: { glob: '**/*.png', dot: false } },
            ], copyIgnore),