我收到错误:'TypeError: this.createCanvas is not a function'。
我不明白为什么。我已经在构造函数中绑定了它。
import React, { Component } from 'react';
class ImageEditor extends Component {
constructor() {
super();
this.createCanvas = this.createCanvas.bind(this);
this.getImageDimensions = this.getImageDimensions.bind(this);
}
componentDidMount() {
const imageurl = "https://storage.googleapis.com/" + this.props.location.state.imageuri;
this.getImageDimensions(imageurl);
}
getImageDimensions(url) {
var img = new Image();
img.onload = function() {
this.createCanvas(this.width, this.height);
}
img.src = url;
}
createCanvas(width, height) {
let mycanvas = document.createElement("canvas");
mycanvas.width = width;
mycanvas.height = height;
mycanvas.id = "mycanvas";
document.body.appendChild(mycanvas);
}
render() {
return (
<h1>{this.imageurl}</h1>
)
}
}
导出默认图像编辑器;
你img.onload
也必须得到一个箭头函数,否则this
就不是你想要的。您可以在此处阅读有关为什么会出现这种情况的更多信息。
img.onload = () => {
this.createCanvas(img.width, img.height);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句