我正在使用nativescript-camera捕获我的android应用程序中的图像,但我总是得到TypeError:无法设置未定义的属性'imageSource'。我正在按照here指南进行操作,我的所有相机代码似乎都是正确的,并且我不确定自己在哪里做错了。
export class SocialRegistrationComponent implements OnInit {
imageSource: ImageSource;
constructor() {}
ngOnInit() {
}
capturePicture() {
const options = {
saveToGallery: false,
allowsEditing: false,
format: 'png'
};
camera.requestPermissions().then(
function success() {
camera.takePicture(options)
.then((capturedImageAsset) => {
let imageSource = new imageSourceModule.ImageSource;
imageSource.fromAsset(capturedImageAsset)
.then((capturedImageSource: ImageSource) => {
console.log(capturedImageSource); // Display correctly
this.imageSource = capturedImageSource; // Error here, this.imageSource undefined
});
}).catch((err) => {
console.log('Error -> ' + err.message);
});
},
function failure() {
// failed
}
);
}
}
上下文(this
)将不会指向成功函数中的Angular组件。您必须使用箭头功能或将上下文保留在局部变量中。
success() => {
// Using arrow function retains the context
....
}