javascript - Angular 4 typescript Cordova相机插件错误 "Property '相机'在类型 'Navigator'上不存在”

标签 javascript android angular cordova

我尝试使用 Cordova 构建 APK。 Web 部件代码是用 Angular 4 typescript 编写的。当 ng build 时,它给了我以下错误

ERROR in $/AngularCordova/my-app/src/app/app.component.ts (19,26): Property 'camera' does not exist on type 'Navigator'. ERROR in $/AngularCordova/my-app/src/app/app.component.ts (23,13): Supplied parameters do not match any signature of call target.

    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
          }
        );

    }
  }
}

最佳答案

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

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

}

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

关于javascript - Angular 4 typescript Cordova相机插件错误 "Property '相机'在类型 'Navigator'上不存在”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47055198/

相关文章:

javascript - 如何一起使用 grunt-autoprefixer 和 grunt-sass?

javascript - 寻找线段和贝塞尔曲线交点的程序

android - 如何在标准滑动+选项卡模型下的选项卡中使用布局 xml 文件

Angular2 InputFormControl 和 ValueChange 连续触发

javascript - 如何比较数组以查看它是否包含特定值

javascript - 将 javascript 文件与 html 集成 - 简单示例

java - .set 背景颜色与十六进制颜色代码 Android Studio

android - SetView AlertDialogBu​​ilder 奇怪的行为

node.js - 无法运行图像,CMD ["npm", "start"]命令错误

angular - 如何在命名 socket 是未命名 socket 的子代时导航到路线