将Cordova插件在没有离子本质的离子框架中使用



我找到了一个为codova制作的插件:
https://github.com/dff-solutions/dff-cordova-plugin-honeywell
该文档描述了如何直接使用对象霍尼韦尔,而不是如何将其导入到app.module中并进行适当的适应。我还没有发现有关离子天然开发或Cordova插件迁移到离子2的清晰文档。
上的任何指南>如何将任何Cordova插件进入现代离子2
Update :在我的情况下,有解决方法,因为我具有由插件创建的全局变量,因此我在@component Decorator之前声明该变量。但这诀窍打破了依赖注入系统。

import { Component, NgZone } from '@angular/core';
import { NavController } from 'ionic-angular';
declare var Honeywell;//declare the global created by the plugin
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  barcode: string;
  constructor(public navCtrl: NavController, public zone: NgZone) {
        Honeywell
          .onBarcodeEvent( ... );
  }
}

在您需要的任何提供商/页面的顶部使用'honeywell'应该足够,因此无需修改app.module.ts,它的依赖性角度可以处理。

declare let Honeywell: any;

但是,我创建了一个打字稿文件来存根,然后致电插件

import { Injectable } from '@angular/core';
import { IonicNativePlugin } from '@ionic-native/core';
declare let Honeywell: any;
@Injectable()
export class HoneywellBarcodeScanner extends IonicNativePlugin {    
    onLog(success, error, args?): Promise<any> { 
        return Honeywell.onLog(success, error, args);
    }
    onBarcodeEvent(success, error, args?): Promise<any> { 
        return Honeywell.onBarcodeEvent(success, error, args);
    }
    onFailureEvent(success, error, args?): Promise<any> { 
        return Honeywell.onFailureEvent(success, error, args); 
    }
    barcodeReaderPressSoftwareTrigger(success, error, args?): Promise<any> { 
        return Honeywell.barcodeReaderPressSoftwareTrigger(success, error, args); 
     }    
}

如果您有兴趣,然后嘲笑要与离子服务一起使用的插件(选择模拟的条形码和调用条形码扫描事件),这是一个完整的示例(使用3种类型的条形码扫描仪,Linea,Honeywell和Camera)https:https://github.com/domisginger/ionic-cordova-barcode-examples

Honeywell-Barcode-Scanner.mock.ts

import { HoneywellBarcodeScanner } from './../plugin-interfaces/honeywell-barcode-scanner';
export class HoneywellBarcodeScannerMock extends HoneywellBarcodeScanner {    
    onLog(): Promise<any> {
        return new Promise((resolve, reject) => {
            resolve();
        });
    }
    onBarcodeEvent(): Promise<any> {
        return new Promise((resolve, reject) => {
            resolve();
        });
    }
    onFailureEvent(): Promise<any> {
        return new Promise((resolve, reject) => {
            resolve();
        });
    }
    barcodeReaderPressSoftwareTrigger(): Promise<any> {
        return new Promise((resolve, reject) => {
            resolve();
        });
    }
}

插件 - factories.ts

import { Platform } from "ionic-angular/platform/platform";
import { AlertController } from "ionic-angular";
import { HoneywellBarcodeScanner } from "./honeywell-barcode-scanner";
import { HoneywellBarcodeScannerMock } from "./../mocks/honeywell-barcode-scanner.mock";
let isMobile = (platform: Platform): boolean => {
    return platform.is('ios') || platform.is('android');
}
export let PluginFactories = {
    honeywellBarcodeScannerFactory:  (platform: Platform) => { 
        return isMobile(platform) ? new HoneywellBarcodeScanner() : new HoneywellBarcodeScannerMock();
    }
}

然后在app.module.ts

providers: [
    {
      provide: HoneywellBarcodeScanner, 
      useFactory: PluginFactories.honeywellBarcodeScannerFactory,
      deps: [Platform]
    },
]

相关内容

  • 没有找到相关文章

最新更新