Web Workers - 如何导入模块 [英] Web Workers - How To Import Modules

查看:898
本文介绍了Web Workers - 如何导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ES2015导入/导出模块。

I am using ES2015 Import / Export modules.

在我的工作文件中,当我尝试导入像往常一样的函数时:

In my worker file, when I try to import functions like I normally do:

worker.js

import { a, b, c } from "./abc.js";

我收到错误:
SyntaxError:import declarations only may only出现在模块的顶层

当我在我的模块'abc.js'中导出函数时,我不知道如何使用他们使用旧的(显然在出路)语法:

As I am exporting functions in my module 'abc.js', I am not sure how to use them using the old (& apparently on its way out) syntax:

self.importScripts( "/app/abc.js" );

所以,我的问题是,我们如何将新的导入模块语法与工人一起使用?

So, my question is, how do we use the new import module syntax with workers?

第二个问题是,当从一个模块导入时, importScripts 导入到什么地方没有全局对象父元素导出?

Second question is, what does importScripts import into when it imports from a module in where is there is no global object parent being exported?

推荐答案

任何浏览器都不支持工作程序中的ES2015模块(否则甚至不支持模块)。一旦他们这样做,你必须创建一个这样的工人:

ES2015 modules in workers are not yet supported in any browser (not even ones that support modules otherwise). Once they do, you have to create a worker like this:

new Worker("worker.js", { type: "module" });

参见: https://html.spec.whatwg.org/#module-worker-example

For现在你必须使用 importScripts()

For now you have to use importScripts().

这些是每个浏览器的错误报告:

These are the bug-reports for each browser:

  • Firefox
  • Chromium (it's currently behind a flag: --enable-experimental-web-platform-features)
  • Webkit
  • Edge (also this uservoice page)

这篇关于Web Workers - 如何导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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