无法从 ReactJs 组件调用 WebAssembly Js API [英] Can't call WebAssembly Js API from ReactJs Component
问题描述
在 HTML 文件中调用 WebAssembly API 效果很好,
Calling the WebAssembly API in an HTML file works perfectly,
<html>
<body>
<script>
...
WebAssembly.instantiate(bytes, importObject)
...
</script>
</body>
</html>
但是,在 React 组件中调用它,当在我的 react-app 的根目录运行npm start"时,会导致此错误:
However, calling it in a React Component, when running 'npm start' at the root of my react-app, results in this error:
'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
文件,这阻碍了我.
I want to run a .wasm
file from a React Component and this is blocking me.
如何解决这个问题?
推荐答案
这看起来很像 linting 错误.你在使用 ESLint 吗?
This looks very much like a linting error. Are you using ESLint?
你需要通知你的 linter WebAssembly 是一个全局对象.尝试将以下内容添加到文件顶部:
You need to inform your linter that WebAssembly is a global object. Try adding the following to the top of the file:
/* global WebAssembly */
虽然更好的选择可能是将其添加到您的 eslint 配置文件中.
Although a better option might be to add it to your eslint confit file.
这篇关于无法从 ReactJs 组件调用 WebAssembly Js API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!