我正在尝试构建一个可以拍照的Ionic应用程序。但是当我运行该程序时,我收到"错误TS2339:类型"导航器"上不存在属性"相机">
我安装了ionic cordova plugin add cordova-plugin-camera
还npm install @ionic-native/camera
这是我的应用程序.模块.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 {}
这是我的主页
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);
}
}
}
但是,你为什么要使用navigator.camera.getPicture()
?
您必须使用 this
关键字来访问相机对象,然后您可以调用getPicture()
方法,例如 this.camera.getPicture()
.