No provider for Camera! injectionError

拥有回忆 提交于 2019-11-29 19:54:18

问题


I am a beginner in Ionic 2. I would like to use the camera in Ionic. I followed https://ionicframework.com/docs/native/camera/ tutorial. I have already installed the cordova-plugin-camera plugin and installed ionic-native/camera using cli code.

While I serve the project it shows Runtime Error:

Uncaught(in promise):Error: No provider for Camera! injectionError

I am sending the app.module.ts, html page, and type script page. Please have a look.

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 { CameraExamplePage } from "../pages/camara-example/camara-example";

@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CameraExamplePage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CameraExamplePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

Camera Take Html Page

<ion-header>


  <ion-navbar>

   <ion-title>camaraExample</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>

**TypeScript File **

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera'

@IonicPage()
 @Component({
             selector: 'page-camara-example',
             templateUrl: 'camara-example.html',
            })
 export class CameraExamplePage {
 imageURL

 constructor(public navCtrl: NavController, public navParams: NavParams, public camera: Camera) {
 }
 ionViewDidLoad() {
    console.log('ionViewDidLoad CameraExamplePage');
 }

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

 }


}

回答1:


You need to set Camera as provider in app.module.ts

 import { Camera } from '@ionic-native/camera';//import in app.module.ts

 //...

 providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    Camera //here
  ]

While I Serve the project it shows Runtime Error

NOTE: Cordova plugins do not work in ionic serve.. You need to use emulator/device. Also, include your plugin code within this.platform.ready() and check if cordova is available using this.platform.is('cordova')

import { Platform } from 'ionic-angular'; //import Platform
//...
constructor(public platform:Platform){}
//...
takePhoto() {
    this.platform.ready().then(() => {
        if(this.platform.is('cordova')){
            this.camera.getPicture(this.options).then((imageData) => {
                // imageData is either a base64 encoded string or a file URI
                // If it's base64 (DATA_URL):
                let base64Image = 'data:image/jpeg;base64,' + imageData;
            }, (err) => {
                // Handle error
            });
        }
    })
}



回答2:


you need to install camera plugins first by using two commands

$ ionic cordova plugin add cordova-plugin-camera

$ npm install --save @ionic-native/camera

after that in your app.module.ts you need to import that plugins and change your provider

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";
import { Camera} from '@ionic-native/camera';
@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CamaraExampalePage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CamaraExampalePage
  ],
  providers: [
    Camera,
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}



回答3:


There's a problem with the auto generated import statement. You need: `import { Camera, CameraOptions } from '@ionic-native/camera/ngx';




回答4:


simply set Camera as provider in app.module.ts also import it in your component...works like magic!



来源:https://stackoverflow.com/questions/46048904/no-provider-for-camera-injectionerror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!