【问题标题】:Can't call WebAssembly Js API from ReactJs Component无法从 ReactJs 组件调用 WebAssembly Js API
【发布时间】:2018-06-09 11:47:51
【问题描述】:

在 HTML 文件中调用 WebAssembly API 效果很好,

<html>
  <body>
    <script>
      ...
          WebAssembly.instantiate(bytes, importObject)
       ...

   </script>
  </body>

</html>

但是,在 React 组件中调用它,当在我的 react-app 的根目录运行“npm start”时,会导致此错误:

'WebAssembly' is not defined  no-undef

import React from 'react';

export default class WasmContainer extends React.Component {

    constructor(props) {
        super(props);
        this.func = this.func.bind(this);
    }

    func(){
            ...
        WebAssembly.instantiate(bytes, importObject)
            ...

        return "...";
    }

    render() {
        return (<p>{this.func()}</p>)
    }
}

我想从 React 组件运行 .wasm 文件,这阻碍了我。

如何解决这个问题?

【问题讨论】:

  • WebAssembly 在你的 React 组件中没有定义
  • 导入它:import WebAssembly from 'web-assembly'
  • 你指的是这个包吗:web-assembly?

标签: javascript reactjs webassembly


【解决方案1】:

这看起来很像一个 linting 错误。你在用 ESLint 吗?

您需要通知 linter WebAssembly 是一个全局对象。尝试将以下内容添加到文件顶部:

/* global WebAssembly */

虽然更好的选择可能是将其添加到您的 eslint confit 文件中。

【讨论】:

    猜你喜欢
    • 2020-11-10
    • 2020-03-23
    • 2017-04-15
    • 1970-01-01
    • 2017-11-24
    • 2020-09-15
    • 2023-01-23
    • 2017-08-19
    相关资源
    最近更新 更多