首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ionic 2/3中调用cordova brother标签打印机插件?

如何在ionic 2/3中调用cordova brother标签打印机插件?
EN

Stack Overflow用户
提问于 2018-07-07 17:07:36
回答 2查看 1.1K关注 0票数 1

我使用的是插件https://github.com/gordol/cordova-brother-label-printer

代码语言:javascript
复制
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

declare let BrotherPrinter:any;

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

  code:any={text:''};

  constructor(
    public navCtrl: NavController,
    public barcodeScanner:BarcodeScanner,

  ) {

  }

  print(){
    BrotherPrinter.findBluetoothPrinters(function(data){
        console.log("Success");
        console.log(data)
    },function(err){
        console.log("Error");
        console.log(err)
    });
  }
  
  scan(){
    this.barcodeScanner.scan().then(barcodeData => {
      console.log('Barcode data', barcodeData);
      this.code = barcodeData;
     }).catch(err => {
         console.log('Error', err);
     });
  }
}

则在运行/构建之后:错误ReferenceError:未定义BrotherPrinter

请帮助,如何定义/调用这个插件,因为它不能正常工作。

EN

回答 2

Stack Overflow用户

发布于 2018-07-07 18:50:35

根据plugin.xml file的说法,全局对象似乎是:

代码语言:javascript
复制
cordova.plugins.brotherPrinter

尝试:

代码语言:javascript
复制
declare var cordova;

在你的print函数中,

代码语言:javascript
复制
cordova.plugins.brotherPrinter.findBluetoothPrinters((data)=>{
    console.log("Success");
    console.log(data)
},(err)=>{
    console.log("Error");
    console.log(err)
});
票数 1
EN

Stack Overflow用户

发布于 2018-07-12 22:07:18

我只是在插件中添加了一个打印数量的参数。这是为QL-810W兄弟打印机工作。

代码语言:javascript
复制
printFromBrotherPrinter(base64String, noOfPrintLocal) {
let that = this;

return new Promise((resolve, reject) => {

  cordova.plugins.brotherPrinter.findPrinters((success) => {
    that.printerResponse = success;
    if (that.printerResponse.length > 0) {

      var printer = {
        "model": that.printerResponse[0].model,
        "port": that.printerResponse[0].port,
        "modelName": that.printerResponse[0].modelName,
        "ipAddress": that.printerResponse[0].ipAddress,
        "macAddress": that.printerResponse[0].macAddress,
        "nodeName": that.printerResponse[0].nodeName,
        "location": ""
      };

      cordova.plugins.brotherPrinter.setPrinter(printer, function (success) {
        var dataArray = {
          "base64String": base64String,
          "numberOfCopies": noOfPrintLocal
        };

        cordova.plugins.brotherPrinter.printViaSDK(dataArray, function (printResult) {
          //var printResult = callback;
          if (printResult == null) {
            // iOS result
            resolve(1);
          }
          else {
            if (printResult.result != null && printResult.result !== undefined) {
              if (printResult.result == 'Succeeded') {
                // Print Success
                resolve(1);
              }
              else {
                resolve(2);
              }
            }
            else {
              resolve(2);
            }
          }
        });
      }, function (error) {
        reject(2);
      });
    } else {
      console.log("No printer found");
      reject(2);
    }
  }, (error) => {
    console.log("Find Printer Error: " + JSON.stringify(error));
    reject(2);
  });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51221612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档