cordova-plugin-printer @ ionic-native / printer错误错误:未捕获(承诺):NullInjectorError

时间:2019-10-18 15:23:22

标签: cordova printing ionic4 ionic-native

我需要使用ionic框架(v4)从android应用程序打印html内容。我在官方文档中找到了cordova-plugin-printer,但我无法使其正常工作。 根据{{​​3}},您只需要稍后安装插件和包装器即可。

ionic cordova plugin add cordova-plugin-printer
npm install @ionic-native/printer

然后在注入打印机对象时出现NullInjector错误

ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[TabComprobantePage -> Printer]: 
  StaticInjectorError(Platform: core)[TabComprobantePage -> Printer]: 
    NullInjectorError: No provider for Printer!
NullInjectorError: StaticInjectorError(AppModule)[TabComprobantePage -> Printer]: 
  StaticInjectorError(Platform: core)[TabComprobantePage -> Printer]: 
    NullInjectorError: No provider for Printer!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:725)
    at resolveToken (core.js:11917)
    at tryResolveToken (core.js:11861)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:11763)
    at resolveToken (core.js:11917)
    at tryResolveToken (core.js:11861)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:11763)
    at resolveNgModuleDep (core.js:20233)
    at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:20904)
    at resolveNgModuleDep (core.js:20233)
    at resolvePromise (zone.js:852)
    at resolvePromise (zone.js:809)
    at zone.js:913
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:26246)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
    at drainMicroTaskQueue (zone.js:601)

我尝试将打印机添加到提供者/导入中,但没有成功。有想法吗?

谢谢

1 个答案:

答案 0 :(得分:3)

我现在尝试过,如果将打印机插件作为提供程序添加到我的App.Module.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 { Printer } from '@ionic-native/printer/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    Printer,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}