如何在打字稿中使用 Nativescript 工作者? [英] How to use Nativescript worker with typescript?

查看:30
本文介绍了如何在打字稿中使用 Nativescript 工作者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Nativescript Worker 与 Typescript 一起使用,但出现此错误:

I'm trying to use Nativescript Worker with Typescript but i had this error:

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

ERROR Error: com.tns.NativeScriptException: Failed to find module: "../shared/workers/workername.js", relative to: app//

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

This is my code, i'm using the same template as in Nativescript documentation:

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:

and my 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 ...
}

推荐答案

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

I have used the same way explained here and don't forget to configure your Webpack config file like this:

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),

这篇关于如何在打字稿中使用 Nativescript 工作者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆