Angular 4打字稿Cordova相机插件错误"属性'相机'类型' Navigator'"不存在

时间:2017-11-01 12:45:40

标签: javascript android angular cordova

我尝试使用Cordova构建APK。 Web部件代码用Angular 4打字稿编写。在构建它时,它会给我以下错误

   AngularCordova / my-app / src / app / app.component.ts(19,26)中的错误:   物业相机'类型' Navigator'中不存在。错误   $ / AngularCordova / my-app / src / app / app.component.ts(23,13):提供   参数与呼叫目标的任何签名都不匹配。

    import { Component, OnInit } from '@angular/core';

declare let cordova: any;
declare let navigator: any;
let device;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  implements OnInit {
    title = 'angular app';
    ngOnInit(){
      this.deviceinfo();
    }
    deviceinfo(){

        document.addEventListener('deviceready', () => {
        alert("On device ready event");
            try{
                alert('Using Cordova plugins with Angular 4. Cordova version: ');
            }catch(e){
                alert("Exception occures"+ e)
            }
        }, false)
    }
  openCamera() {
      if( window.navigator != undefined){
        window.navigator.camera.getPicture(
          (imageUri) => {
            alert(imageUri);
          }, 
          (error) =>{
            alert("Unable to obtain picture: " + error, "app");

          }, {
              quality: 50,
              allowEdit: true,
              correctOrientation: true  //Corrects Android orientation quirks
          }
        );

    }
  }
}

1 个答案:

答案 0 :(得分:2)

if( navigator != undefined){
  navigator.camera.getPicture(
    (imageUri) => {
      alert(imageUri);
    },
    (error) =>{
      alert("Unable to obtain picture: "+error);

    }, {
      quality: 50,
      allowEdit: true,
      correctOrientation: true  
    }
  );

}

我在openCamera函数中替换了上面的代码。发生错误是因为我试图从窗口对象访问导航器对象。