Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

angular - Ionic : Error: Invalid provider for the NgModule 'AppModule'

i installed Barcodescanner in my ionic application using below commands

ionic cordova plugin add phonegap-plugin-barcodescanner
npm install @ionic-native/barcode-scanner

After installations I imported it and then added to providers list as well in app.moule.ts as below

 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 {BarcodeScanner} from '@ionic-native/barcode-scanner/ngx';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    IonicModule.forRoot(),

    AppRoutingModule
  ],
  providers: [
    StatusBar,
    SplashScreen,
     { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    BarcodeScanner

  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

After that i injected it in the constructor as below :

import { Component, OnInit } from '@angular/core';
import {BarcodeScanner} from '@ionic-native/barcode-scanner/ngx';

@Component({
  selector: 'app-scanning',
  templateUrl: './scanning.page.html',
  styleUrls: ['./scanning.page.scss'],
})
export class ScanningPage implements OnInit {

  constructor( barcodeScanner: BarcodeScanner) {

  }

  ngOnInit() {
  }

}

which gives me following error :

Error: Invalid provider for the NgModule 'AppModule' - only instances of Provider and Type are allowed, got: [..., ..., ..., ?[object Object]?]
    at throwInvalidProviderError (core.js:5455)
    at providerToFactory (core.js:11347)
    at providerToRecord (core.js:11318)
    at R3Injector.processProvider (core.js:11216)
    at core.js:11202
    at core.js:1135
    at Array.forEach (<anonymous>)
    at deepForEach (core.js:1135)
    at R3Injector.processInjectorType (core.js:11202)
    at core.js:11009

i have tried lot to fix the issue but can't figure out the reason for this , below is my ionic version

6.12.3

Node Version

12.19.0

can someone help please to fix the issue

question from:https://stackoverflow.com/questions/65848406/ionic-error-invalid-provider-for-the-ngmodule-appmodule

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You dont need to import BarcodeScanner in your AppModule providers list


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...