打字稿 Javascript for cordova-plugins

Typescript to Javascript for cordova-plugins

我正在尝试从我的 devExtreme 项目中调用一个 cordova 插件。不幸的是,该插件提供的一种方法在 TypeScript 中,我已经尝试了很多不同的角度来复制 JavaScript 中的调用,在翻译以下代码时非常感谢您的帮助: 插件代码:

function findNetworkPrinters(success: (printers: Printer[]) => void, failure: (reason: string) => void): void

我尝试过的:

cordova.plugins.brotherPrinter.findNetworkPrinters(function (Printer) {
     alert(printer);
}, onSuccess, onFail);

并且:

cordova.plugins.brotherPrinter.findNetworkPrinters(function (Printer) {
     alert(printer);
}, function()error{
     alert(error);
});

插件使用简单说明:

findNetworkPrinters

Upon success, findNetworkPrinters will provide a list of printers that were discovered on the network (likely using WiFi). It is not considered an error for no printers to be found, and in this case the list will just be empty.

如果插件尚未编译,您必须将其编译为JavaScript。但是通常情况下,如果您通过 npm 获取插件,该步骤应该已经完成​​。

考虑到你第二次尝试的函数签名是更正确的,但要注意错误函数中的语法错误,并且 Printerprinter 是不同的变量。除此之外,该函数实际上提供的不是一台打印机,而是一组打印机。更正确的版本是:

cordova.plugins.brotherPrinter.findNetworkPrinters(function(printers){
  printers.forEach(function(printer){
    alert(printer);
  });
}, function(error){
  alert(error);
});