【问题标题】:Phonegap plugin - imagePicker - Producing Error MessagePhonegap 插件 - imagePicker - 产生错误信息
【发布时间】:2015-08-01 01:58:28
【问题描述】:

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

"未捕获的类型错误:无法读取未定义的属性 'getPictures'", 来源: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();

});

【问题讨论】:

  • 根据错误,window.imagePickerundefined。您是否通过执行window.imagePicker = ...imagePicker 设置为任何地方的全局变量?
  • @Pushkin,var 定义在 imagepicker.js 文件中。我将编辑问题以包含它。
  • 如果你不是 window.plugin 而是跟随 this 的例子。如果您遵循它,则需要将window... 替换为module.exports 并在main.js 中添加require 语句。
  • 哦,除了这不是你的插件,对吗?所以最好不要改变它。但是,如果该示例有帮助,请告知。
  • 如果可以的话,尝试在浏览器中运行它。如果您打开 Chrome 开发工具 (F12) 并导航到“源”选项卡,您应该会看到 imagepicker.js。我只是想让你确保它没有被加载。如果没有,也许检查你的路径?

标签: javascript jquery cordova jquery-mobile phonegap-plugins


【解决方案1】:

打了几个小时之后,终于意识到外部 javascript 文件没有加载。菜鸟的错误,我把脚本的引用放在页眉而不是 data-role="page" 标记中。

在这里找到答案:

JQuery Mobile Change Page doesnt load JS files

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-08
    • 2012-01-15
    • 2022-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    相关资源
    最近更新 更多