【发布时间】:2019-09-02 05:01:23
【问题描述】:
我正在尝试使用人脸检测库。我已经在我的 index.html 中将它与脚本标签链接起来,并希望在我的 profile_pic 组件中使用该文件。在 componentDidMount 上,当我调用该函数时,它说 face_detection 不是函数。当我将相同的文件与简单的 javascript 一起使用时,它运行成功。现在如何将它与 react 一起使用?
我正在从 img 标签中获取图像,并且有一个文件输入控件用于动态更改图像的值。
这是我的渲染方法
render() {
return (
<div
style={{
marginTop: "20px",
display: "flex",
flexDirection: "column",
alignItems: "center"
}}
>
{/* div for upload photo starts*/}
<div>
<input
accept="image/*"
type="file"
id="image_upload"
style={{ display: "none" }}
onChange={e => {
this.handle_image_upload(e);
}}
/>
<label htmlFor="image_upload">
<img
id='picture'
src={this.state.image}
alt="Man or Woman"
style={{ width: "150px", height: "150px", cursor: "pointer" }}
/>
</label>
</div>
</div>
);
}
这是 componentDidMount 生命周期方法
componentDidMount() {
document.getElementById("image_upload").addEventListener("change",()=>{this.face_detection()})
}
这是人脸检测函数
face_detection=()=>{
console.log($('#picture'))
$('#picture').faceDetection({
complete: function (faces) {
if (faces.length > 0) {
console.log('There is a face in this image')
}
}
});
}
我已经成功地使用了同一个库而没有反应。现在我该如何解决这个错误?
【问题讨论】:
-
为什么要将它添加为脚本标签而不是捆绑在 webpack 中?您的导入是什么样的?
-
import 语句只从 jQuery 中导入 $ 并从 react 中做出反应。是否还有其他需要导入的文件?
-
如果我直接将它导入到我想要使用该函数的组件中,它会给出许多错误,上面写着“预期分配或函数调用,而是看到一个表达式”。它表明这些错误来自库文件
标签: javascript reactjs