【问题标题】:error TS2339: Property 'camera' does not exist on type 'Navigator'错误 TS2339:“导航器”类型上不存在属性“相机”
【发布时间】:2019-09-01 07:48:43
【问题描述】:

我正在尝试构建一个可以拍照的 Ionic 应用程序。 但是当我运行程序时,我得到“错误 TS2339:“导航器”类型上不存在属性“相机”

我安装了ionic cordova plugin add cordova-plugin-camera 还有npm install @ionic-native/camera

这是我的 app.module.ts:

import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { RouteReuseStrategy } from "@angular/router";

import { IonicModule, IonicRouteStrategy } from "@ionic/angular";
import { SplashScreen } from "@ionic-native/splash-screen/ngx";
import { StatusBar } from "@ionic-native/status-bar/ngx";

import { AppComponent } from "./app.component";
import { AppRoutingModule } from "./app-routing.module";
import { Camera } from "@ionic-native/camera/ngx"; 

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    Camera,
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

这是我的 home.page.ts

import { Component, OnInit } from "@angular/core";
import { Camera, CameraOptions } from "@ionic-native/camera/ngx";

@Component({
  selector: "app-home",
  templateUrl: "home.page.html",
  styleUrls: ["home.page.scss"]
})
export class HomePage implements OnInit {

  constructor(private camera: Camera) {}

  takePicture() {
    navigator.camera.getPicture(onSuccess, onFail, {
      quality: 50,
      destinationType: this.camera.DestinationType.FILE_URI
    });

    function onSuccess(imageURI) {
      var image = document.getElementById("myImage");
      this.image.src = imageURI;
    }

    function onFail(message) {
      alert("Failed because: " + message);
    }
  }
}

【问题讨论】:

    标签: angular typescript ionic-framework ionic4 navigator


    【解决方案1】:

    但是,你为什么使用navigator.camera.getPicture()

    您必须使用this 关键字来访问相机对象,然后您可以调用getPicture() 方法,如this.camera.getPicture()

    【讨论】:

    • 相机在 Navigator 上不存在,查看文档developer.mozilla.org/en-US/docs/Web/API/Navigator
    • 如果我按照你说的做,我会收到以下错误:error TS2554: Expected 0-1 arguments, but got 3。我想说。当我在手机上运行相机时,相机确实可以工作,但我想摆脱错误
    • 你是怎么调用这个方法的?你必须调用像this.camera.getPicture({option:yourOption}).then((result) => {//here is your result}}.catch((err) => {//error});这样的方法
    • 您已将成功和错误回调作为方法传递,因此它不起作用。使用thencatch 处理成功和错误回调。
    猜你喜欢
    • 2016-11-17
    • 2019-08-16
    • 2019-01-21
    • 2016-08-13
    • 2017-12-20
    • 2016-11-14
    • 2017-10-24
    • 2021-08-10
    • 2018-11-17
    相关资源
    最近更新 更多