[英]Access Class properties inside callback
我正在使用此插件:带有Ionic 3的https://github.com/blinkmobile/cordova-plugin-sketch 。
我的最后一个重要步骤是使结果超出回调函数的范围,以便进一步使用它。
这是我的代码:
anhangArray: Array<{ name: string, value: string }>=[];
takePhoto() {
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
correctOrientation: true,
allowEdit: false,
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
//base64Image = 'data:image/jpeg;base64,' + imageData;
//>>> FILE_URI
this.getSketch(imageData);
}, (err) => {
// Handle error
});
}
getSketch(src: string) {
window.navigator.sketch.getSketch(this.onSuccess, this.onFail, {
destinationType: window.navigator.sketch.DestinationType.DATA_URL,
encodingType: window.navigator.sketch.EncodingType.JPEG,
inputType: window.navigator.sketch.InputType.FILE_URI,
inputData: src
});
}
onSuccess(imageData) {
var _mythis = this;
if (imageData == null) { return; }
// do your thing here!
setTimeout(function () {
if (imageData.indexOf("data:image") >= 0) {
} else {
imageData = "data:image/jpeg;base64," + imageData;
}
_mythis.anhangArray.push({ name: "anhang_" + parseInt(_mythis.user._kunnr), value: imageData });
console.log(this.anhang);
}, 0);
}
错误:未被捕获的TypeError:无法读取未定义的属性'anhangArray'
您的问题是window.navigator.sketch.getSketch() 。 虽然window.navigator.sketch.getSketch会触发您的回调this.onSuccess ,但这会更改回调的范围。 要解决此问题,您可以更改实现,如下所示。
getSketch(src: string) {
window.navigator.sketch.getSketch(imageData => {
this.onSuccess(imageData);
}, error => {
this.onFail(error);
}, {
destinationType: window.navigator.sketch.DestinationType.DATA_URL,
encodingType: window.navigator.sketch.EncodingType.JPEG,
inputType: window.navigator.sketch.InputType.FILE_URI,
inputData: src
});
}
您也可以通过这种方式解决:
this.camera.getPicture(options).then((imageData) => { // imageData is either a base64 encoded string or a file URI // If it's base64: //base64Image = 'data:image/jpeg;base64,' + imageData; //>>> FILE_URI this.getSketch(imageData).bind(this);
.bind(this)
将范围传递给调用者之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.