在 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
文件,这阻止了我。
如何解决这个问题?
最佳答案
这看起来很像 linting 错误。你在使用 ESLint 吗?
您需要通知您的 linter WebAssembly 是一个全局对象。尝试将以下内容添加到文件顶部:
/* global WebAssembly */
尽管更好的选择可能是将它添加到您的 eslint confit 文件中。
关于javascript - 无法从 ReactJs 组件调用 WebAssembly Js API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48015777/