【问题标题】:Nativescript Angular Cannot assign image to property after taking pictureNativescript Angular拍照后无法将图像分配给属性
【发布时间】:2019-12-05 07:41:03
【问题描述】:

我正在使用 nativescript-camera 在我的 Android 应用程序中捕获图像,但我总是收到 TypeError: Cannot set property 'imageSource' of undefined。我正在按照指南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
            }
        );

    }

}

【问题讨论】:

    标签: angular android-camera nativescript nativescript-angular


    【解决方案1】:

    上下文 (this) 不会指向成功函数内的 Angular 组件。您必须使用箭头函数或在局部变量中保留上下文。

    success() => {
      // Using arrow function retains the context
      .... 
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 2019-06-13
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2019-04-30
      • 1970-01-01
      相关资源
      最近更新 更多