【问题标题】:Ionic native Printer plugin not working离子本机打印机插件不起作用
【发布时间】:2017-09-26 08:46:03
【问题描述】:

(离子 2) 位于此处的插件似乎不适用于我在 android 和 ios 上:http://ionicframework.com/docs/native/printer/

我认为我遵循了这个页面的指导方针,代码在两个平台上都构建,但是我在 ios 上得到一个黑屏,在 android 上模拟时一个空白......

首先我开始了一个新项目:ionic start PrinterApp --v2

然后我安装了平台:android 6.2.1, ios 4.3.1

然后是插件页面的两条命令行:

ionic plugin add --save de.appplant.cordova.plugin.printer
npm install --save @ionic-native/printer

然后在 home.html 中添加一行来激活打印机:

<button class="button" (click)="print()">Print</button>

最后我的 home.ts 看起来像这样:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Printer, PrintOptions } from '@ionic-native/printer';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, private printer: Printer) {
  }

  print() {
     this.printer.isAvailable();
    let options: PrintOptions = {
         name: 'MyDocument',
         duplex: true,
         landscape: true,
         grayscale: true
       };
    this.printer.print("http://google.com", options);
  }
}

有没有人在使用插件时遇到过这种问题?我做错什么了吗 ?我应该安装其他东西来解决问题吗? 有没有人有一个运行良好的示例项目?

非常感谢!

【问题讨论】:

  • 不工作是什么意思?有什么错误吗?
  • 感谢您的回复,应用程序构建正常,设备上的应用程序模拟也正常,但我在设备上出现黑屏和空白屏幕,网络检查器上没有错误.. 但我会尝试检查我的代码,也许我做错了什么......
  • 只是一个猜测..你做了npm install @ionic-native/core --save吗?
  • 是的,我几分钟前就这样做了,但没有改变,但有人在 ionic 论坛上告诉我(我发布了相同的消息)我的行:this.printer.isAvailable();可能不正确你猜我可以如何纠正我的错误吗?再次感谢
  • 检查您链接的文档。 this.printer.isAvailable().then(onSuccess, onError);

标签: cordova ionic-framework ionic2 ionic-native


【解决方案1】:

ionic cordova 插件添加--save isiigo-cordova-plugin-printer

npm install --save @ionic-native/printer

【讨论】:

  • 虽然这段代码 sn-p 可以回答这个问题,包括解释它为什么以及如何帮助解决问题,但可以提高您的答案的质量和寿命。见How do I write a good answer?
【解决方案2】:
//Note: First into your App.Module.ts adding printer provider 
//Then into Printer Page
   import { Component } from '@angular/core';
   import { NavController, NavParams } from 'ionic-angular';
   import { Printer, PrintOptions } from '@ionic-native/printer';


   @Component({
     selector: 'page-printer-view',
     templateUrl: 'printer-view.html'
    })
   export class PrinterViewPage {

   constructor(public navCtrl: NavController, public navParams: 
   NavParams, 
   private printer: Printer) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad PrinterViewPage');
  }

  print(){
    this.printer.isAvailable().then(this.onSuccessLoad, this.onErrorLoad);

  }

  onSuccessLoad(){
    let options: PrintOptions = {
        name: 'MyDocument',
        printerId: 'My Printer XYZ',
        duplex: true,
        landscape: true,
        grayscale: true
      };


    this.printer.print("http://google.com",options).then(this.onSuccessPrint, 
    this.onErrorPrint); 
  }

  onErrorLoad(){
    alert('Error : printing is unavailable on your device ');
  }

  onSuccessPrint(){
    alert("printing done successfully !");
  }

  onErrorPrint(){
    alert("Error while printing !");
  }
}

【讨论】:

    【解决方案3】:

    在 Suraj 和 Gabriel 的帮助下,我设法解决了这个问题, 我需要去这个页面获取信息:http://ionicframework.com/docs/native/#Add_Plugins_to_Your_App_Module

    然后输入这一行:npm install @ionic-native/core --save

    然后在我的 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 { 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 { Printer, PrintOptions } from '@ionic-native/printer';
    
    @NgModule({
      declarations: [
        MyApp,
        AboutPage,
        ContactPage,
        HomePage,
        TabsPage
      ],
      imports: [
        BrowserModule,
        IonicModule.forRoot(MyApp)
      ],
      bootstrap: [IonicApp],
      entryComponents: [
        MyApp,
        AboutPage,
        ContactPage,
        HomePage,
        TabsPage
      ],
      providers: [
        StatusBar,
        SplashScreen,
        Printer,
        {provide: ErrorHandler, useClass: IonicErrorHandler}
      ]
    })
    export class AppModule {}
    

    再次感谢! 祝你有美好的一天

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 2021-03-29
      相关资源
      最近更新 更多