我正在使用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
}
);
}
}
答案 0 :(得分:1)
上下文(this
)不会指向成功函数中的Angular组件。您必须使用箭头功能或将上下文保留在局部变量中。
success() => {
// Using arrow function retains the context
....
}