在没有ionic-native的情况下将cordova插件用于ionicframework

jgp*_*s2w 0 ionic-framework cordova-plugins ionic2

我找到了一个为codova制作的插件:
https : //github.com/dff-solutions/dff-cordova-plugin-honeywell
该文档描述了如何直接使用对象Honeywell,但没有介绍如何导入到app.module中。并做出适当的调整。我还没有发现关于离子本地开发或清晰的文档科尔多瓦插件移植到离子2.
请任何指导线如何得到任何科尔多瓦插件融入现代离子2
更新:在我的情况下,有一些解决方法,因为我有一个由插件创建的全局变量,所以我在@Component装饰器之前声明该变量。但这招破坏了依赖注入系统。

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( ... );
  }

}
Run Code Online (Sandbox Code Playgroud)

小智 5

在任何提供程序/页面的顶部使用“ Honeywell”就足够了,不需要修改app.module.ts,它不是Angular可以处理的依赖项。

declare let Honeywell: any;
Run Code Online (Sandbox Code Playgroud)

但是我创建了一个TypeScript文件来对方法进行存根并调用插件

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); 
     }    
}
Run Code Online (Sandbox Code Playgroud)

如果您感兴趣,那么我会模拟出要与Ionic Serve一起使用的插件(选择模拟的条形码并调用条形码扫描的事件),这是一个完整的示例(使用3种类型的条形码扫描仪,linea,honeywell和camera)https:// github .com / domisginger / ionic-cordova-barcode-examples

霍尼韦尔条形码扫描仪.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();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

plugin-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();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在app.module.ts中

providers: [
    {
      provide: HoneywellBarcodeScanner, 
      useFactory: PluginFactories.honeywellBarcodeScannerFactory,
      deps: [Platform]
    },
]
Run Code Online (Sandbox Code Playgroud)