javascript - Phonegap 插件 - imagePicker - 生成错误消息

标签 javascript jquery cordova jquery-mobile phonegap-plugins

我正在尝试将phonegap imagePicker 插件添加到我的应用程序中,但当我尝试调用该函数时收到以下错误:

"Uncaught TypeError: Cannot read property 'getPictures' of undefined", source: file:///android_asset/www/js/main.js (85)

我按照说明使用 CLI 安装了该插件。我已将 .js 复制到适当的文件夹并引用它。

以下是不同页面的相关部分:

HTML:

<!DOCTYPE html>

<html>
  <head>
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height" />

    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <link href="jquerymobile/jquery.mobile-1.4.5.css" rel="stylesheet" type="text/css" />

    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="jquerymobile/jquery.mobile-1.4.5.min.js" type="text/javascript"></script>
    <script src="js/imagepicker.js" type="text/javascript"></script>
    <script src="js/main.js"></script>
  </head>
  <body>
  <!-- a bunch of irrelevant stuff -->
    <a href="#" data-role="button" data-inline="true" data-icon="arrow-u" data-iconpos="bottom" id="btn_upload">Upload</a> 
  </body>
</html>

main.js:

$(document).on("pageshow", "#thepageinquestion", function() {
    $("#btn_upload").click(function() {
        window.imagePicker.getPictures( // <--- this is line 85
            function(results) {
                for (var i = 0; i < results.length; i++) {
                    console.log('Image URI: ' + results[i]);
                }
            }, function (error) {
                console.log('Error: ' + error);
            }
        );
    }); 
});

imagepicker.js:

cordova.define("com.synconset.imagepicker.ImagePicker", function(require, exports, module) { /*global cordova,window,console*/
/**
 * An Image Picker plugin for Cordova
 * 
 * Developed by Wymsee for Sync OnSet
 */

var ImagePicker = function() {

};

/*
*   success - success callback
*   fail - error callback
*   options
*       .maximumImagesCount - max images to be selected, defaults to 15. If this is set to 1, 
*                             upon selection of a single image, the plugin will return it.
*       .width - width to resize image to (if one of height/width is 0, will resize to fit the
*                other while keeping aspect ratio, if both height and width are 0, the full size
*                image will be returned)
*       .height - height to resize image to
*       .quality - quality of resized image, defaults to 100
*/
ImagePicker.prototype.getPictures = function(success, fail, options) {
    if (!options) {
        options = {};
    }

    var params = {
        maximumImagesCount: options.maximumImagesCount ? options.maximumImagesCount : 15,
        width: options.width ? options.width : 0,
        height: options.height ? options.height : 0,
        quality: options.quality ? options.quality : 100
    };

    return cordova.exec(success, fail, "ImagePicker", "getPictures", [params]);
};

window.imagePicker = new ImagePicker();

});

最佳答案

经过几个小时的努力,终于意识到外部 JavaScript 文件没有加载。菜鸟错误,我将对脚本的引用放在页面标题中,而不是放在 data-role="page"标记中。

在这里找到答案:

JQuery Mobile Change Page doesnt load JS files

关于javascript - Phonegap 插件 - imagePicker - 生成错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757457/

相关文章:

javascript - 替换 Wakanda 2 中的 setLoginListener

javascript - 如何编写这个以在 onclick 上工作

javascript - jquery-ui 对话框显示无内容(请参阅更新 3)

javascript - iOs 构建失败 - UnhandledPromiseRejectionWarning : TypeError: Cannot read property 'toLowerCase' of undefined

node.js - 可以在离线的内网环境下使用Cordova吗?

android - 无法找到 android.bat

javascript - 谷歌浏览器打开 window.open(someurl) 就好了...但是点击链接的页面/窗口也会打开 someurl.com

javascript - React 组件数组 - 单击处理程序问题

javascript - Google 图表 API 和 jQuery

javascript - 使用 jquery 中的 fadein 和 fadeout 一张一张地显示图像