我是Ionic 2的初學者。我想在Ionic中使用相機。我跟着https://ionicframework.com/docs/native/camera/頁面。 我已經安裝了cordova-plugin-camera插件 並使用cli代碼保存了離子本機/相機。沒有相機的供應商! injectionError
雖然我所服務的項目它顯示運行時錯誤:
Uncaught(in promise):Error: No provider for Camera! injectionError
我送了app.module.ts,HTML網頁和類型的腳本頁面。請看一看。
app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HttpModule } from '@angular/http';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { CamaraExampalePage } from "../pages/camara-exampale/camara-exampale";
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
CamaraExampalePage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
CamaraExampalePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
相機拍攝的HTML頁面
<ion-header>
<ion-navbar>
<ion-title>camaraExampale</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<button ion-button color="dark" (click)="takePhoto()" > Take Photo
</button>
<img [src]="imageURL" *ngIf="imageURL">
</ion-content>
**打字稿文件**
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera'
@IonicPage()
@Component({
selector: 'page-camara-exampale',
templateUrl: 'camara-exampale.html',
})
export class CamaraExampalePage {
imageURL
constructor(public navCtrl: NavController, public navParams: NavParams, public camera: Camera) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad CamaraExampalePage');
}
takePhoto()
{
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64:
//let base64Image = 'data:image/jpeg;base64,' + imageData;
this.imageURL = imageData
}, (err) => {
// Handle error
});
}
}
感謝您的建議。錯誤消失了。但點擊「拍照」按鈕後,什麼也沒有發生。這是唯一的iPhone/Android手機嗎?或者工作任何平臺。 – Jit
http://ionicframework.com/docs/native/camera/檢查支持的平臺鏈接.. –
嗨代碼不執行裏面的if(this.platform.is('cordova')){}。請解釋一下。 – Jit