【问题标题】:Ionic : Error: Invalid provider for the NgModule 'AppModule'Ionic:错误:NgModule 'AppModule' 的提供程序无效
【发布时间】:2021-01-23 01:12:47
【问题描述】:

我使用以下命令在我的 ionic 应用程序中安装了 Barcodescanner

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

安装后,我将其导入,然后添加到提供程序列表以及app.moule.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 {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 {}

之后我将它注入到构造函数中,如下所示:

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() {
  }

}

这给了我以下错误:

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

我已经尝试了很多来解决这个问题,但无法找出原因,下面是我的离子版本

6.12.3

节点版本

12.19.0

有人可以帮忙解决这个问题

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    您不需要在您的 AppModule 提供者列表中导入 BarcodeScanner

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2017-03-08
      • 2017-06-18
      • 2019-10-11
      相关资源
      最近更新 更多