无法从 ReactJs 组件调用 WebAssembly Js API

Can't call WebAssembly Js API from ReactJs Component

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

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

   </script>
  </body>

</html>

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

'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 文件,这阻止了我。

如何解决这个问题?

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

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

/* global WebAssembly */

尽管更好的选择可能是将它添加到您的 eslint confit 文件中。