在Web Worker问题中加载脚本

时间:2018-07-11 23:24:19

标签: webpack-dev-server web-worker webpack-4 highlightjs

我在将网络工作者导入脚本时遇到问题。我正在尝试导入Highlight.pack.js,但它给了我下面的错误消息。任何建议都会很棒!

  

无法在'WorkerGlobalScope'上执行'importScripts':URL   “ /Scripts/highlighter/highlight.pack.js”无效。

AngularJS:1.4.7

Webpack:4

这是我的Highlight.worker.js:

self.onmessage = function (e) {
    importScripts('/Scripts/highlighter/highlight.pack.js');
    for (var i = 0; i < e.data.textBlocksToSend.length ; i++) {
        e.data.textBlocksToSend[i].textcontent = self.hljs.highlightAuto(e.data.textBlocksToSend[i].textcontent);

    }
    self.postMessage({ textBlocksToReturn: e.data.textBlocksToSend });
}

这是我的指令:

var Worker = require('./highlight.worker.js');

module.exports = function (app) {
    app.directive("syntaxHighlighter", ["highlightService", function (highlightService) {
        return {
            scope: {
                htmlContent: "="
            },
            link: function ($scope, $element, $attrs, controller) {

                $scope.$watch('htmlContent', function () {

                    .
                    .
                    .
                    .
                    .
                    .

                    if (window["Worker"]) {
                        var worker = new Worker();
                        worker.onmessage = function (event) {
                            for (var i = 0; i < elements.length; i++) {
                                $(elements[i]).html(event.data.textBlocksToReturn[i].textcontent.value);
                            }
                        }
                        worker.postMessage({ textBlocksToSend: textBlocks });
                    }
                });
            }
        };
    }]);
};

0 个答案:

没有答案