简体   繁体   中英

Barcodescanner plugin is not being initialized in window.plugins

I've already added the barcodescanner plugin to my phonegap project. It seems it's not being initialized in window.plugins, it appears like 'undefined'.

I am testing it in eclipse - android sdk.

Here's my code where I suppose it writes in window.plugins

ScannerLoader(require, exports, module);
    cordova.define("cordova/plugin/BarcodeScanner", ScannerLoader);

    if (! window.plugins) {
        window.plugins = {};
    }

    if (! window.plugins.LocalNotification) {
        //window.plugins.LocalNotification = cordova.require("cordova/plugin/LocalNotification");
        window.plugins.barcodeScanner = cordova.require("cordova/plugin/BarcodeScanner");
    }

I am using phonegap 2.9.0 and my OS is Mac OSX Mavericks

EDIT: I also noticed the plugin calls require() javascript function which browser cannot recognize. Do I have to add another js file so require() function works?

The solution is:

  1. Make sure your plugin is initialized in cordova project:

cordova.define( "cordova/plugin/barcodescanner", function( require, exports, module ){ .. });

  1. Make sure window.plugins exists like object, else create it.

    if (!window.plugins) { window.plugins = {}; }

  2. Assign the plugin to window.plugins

    if (!window.plugins.barcodeScanner) { window.plugins.barcodeScanner = cordova.require("cordova/plugin/barcodescanner"); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM