【问题标题】:Web Worker onMessage is null in IE11IE11 中的 Web Worker onMessage 为空
【发布时间】:2019-05-16 16:35:03
【问题描述】:

我在我的 react 应用程序的 IE11 中收到错误“Promise is Undefined”。当我在我的代码中使用 Worker 时

const worker = new Worker('./worker.ts')
console.log('worker' , worker);

当我在 Chrome 和 IE 11 中检查日志时,Chrome 将 worker onMessage 作为函数返回,而 IE 11 返回 null,并且我还看到消息“权限被拒绝”。我在 IE 11 “Promise is Undefined”中也遇到了这个错误。

我在文件开头导入了 import 'babel-polyfill'。除了这个 Worker 之外,所有其他 Promise 都运行良好。

我该如何解决这个问题?

【问题讨论】:

    标签: javascript reactjs typescript ecmascript-6 internet-explorer-11


    【解决方案1】:

    React 可能无法立即与 IE 兼容,

    来自官方文档:

    React 支持所有流行的浏览器,包括 Internet Explorer 9 及更高版本,但旧版浏览器(如 IE 9 和 IE 10)需要一些 polyfill。

    我们不支持不支持 ES5 方法的旧浏览器,但是 如果使用 polyfill,您可能会发现您的应用程序确实可以在旧版浏览器中运行 例如 es5-shim 和 es5-sham 都包含在页面中。你在你的 如果您选择走这条路,就拥有自己。


    要使您的应用程序在 IE(11 或 9)上运行,您必须安装 React-app-polyfill:

    https://www.npmjs.com/package/react-app-polyfill

    特点:

    每个 polyfill 都确保存在以下语言特性:

    Promise (for async / await support)
    window.fetch (a Promise-based way to make web requests in the browser)
    Object.assign (a helper required for Object Spread, i.e. { ...a, ...b })
    Symbol (a built-in object used by for...of syntax and friends)
    Array.from (a built-in static method used by array spread, i.e. [...arr])
    

    用法

    首先,使用 Yarn 或 npm 安装包:

    npm install react-app-polyfill
    

    现在,您可以导入要支持的最低版本的入口点。例如,如果您导入 IE9 入口点,这将包括 IE10 和 IE11 支持。

    Internet Explorer 9

    // This must be the first line in src/index.js
    import 'react-app-polyfill/ie9';
    
    // ...
    

    Internet Explorer 11

    // This must be the first line in src/index.js
    import 'react-app-polyfill/ie11';
    
    // ...
    

    您还可以使用以下文档配置清单以处理不同的浏览器:https://github.com/browserslist/browserslist

    例子:

    "browserslist": [
        ">0.2%",
        "not dead",
        "ie >= 9"
    ]
    

    【讨论】:

    • 感谢 Treycos 的回复。我已经尝试过上述解决方案所有其他承诺都工作正常。但只面临这个 Worker 的问题。
    • 哦,好吧,既然你的 Worker 使用的是 Promise,我认为这个解决方案会奏效
    【解决方案2】:

    通过在 worker.ts 顶部添加 import 'babel-polyfill' 来修复它。

    注意:我已经在 index.js 中导入了 babel-polyfill。但是对于工人,我不得不再次添加它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多