离子原生打印机插件无法正常工作 [英] Ionic native Printer plugin not working
问题描述
(Ionic 2)
这里的插件似乎对android和ios不起作用: http://ionicframework.com/docs/native/printer/
我认为我遵循了本页的指导原则,代码是在两个平台上构建,但我在ios上有一个黑屏,在android上有一个空白的模拟... ...
I think that I followed the guidelines from this page, the code is building on both platforms but I got a black screen on ios and a blank one on android when emulate ...
首先我开始了一个新项目:离子启动PrinterApp --v2
First I started a new project : ionic start PrinterApp --v2
然后我安装了平台: android 6.2.1,ios 4.3.1
然后来自插件页面的两个命令行:
Then the two command lines from the plugin page :
ionic plugin add --save de.appplant.cordova.plugin.printer
npm install --save @ionic-native/printer
然后在home.html中我放了一条线来激活打印机:
Then in the home.html I put a line to active the printer :
<button class="button" (click)="print()">Print</button>
最后我的home.ts看起来像这样:
And finally my home.ts looks like this :
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);
}
}
有没有人有这种插件的麻烦?我做错什么了吗 ?我应该安装其他东西来解决问题吗?
并且有没有人有一个示例项目运作良好?
Did anyone have this kind of troubles with the plugin ? Did I do something wrong ? Should I install other things to fix the problem ? And did anyone have a exemple project working well ?
非常感谢!
推荐答案
//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 !");
}
}
这篇关于离子原生打印机插件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!