未捕获的ReferenceError:未定义require

时间:2017-04-07 06:14:51

标签: javascript cordova

我正在尝试实施cordova-plugin-email-composer。我使用cli

安装了该插件

cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git

我收到了错误Uncaught ReferenceError: require is not defined at email_composer.js:22.

link你可以找到插件。我在index.js文件中添加了下面附带的代码。谁能帮忙解决这个问题?三江源。

index.js:

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, function () {
        cordova.plugins.email.isAvailable(
            function (isAvailable) {
                alert("is email mobile available? " + (isAvailable ? "Yes" : "No"));
                if(isAvailable){
                 window.plugin.email.open({
                     to:      'anu.barbie143@gmail.com',
                     subject: 'Greetings',
                     body:    'How are you? Nice greetings from Leipzig'
                 }, callback, scope);
               }
            }
        );
    }, false);

    function callback(){
        console.log("callback function");
    }

    function scope(){
        console.log("scope function");
    }

},

email_composer.js:

var exec      = require('cordova/exec'),
isAndroid = navigator.userAgent.toLowerCase().indexOf('android') > -1,
mailto    = 'mailto:';

在上面的代码中我得到了一个错误要求没有定义。任何人都可以帮我解决这个问题? 三江源。

2 个答案:

答案 0 :(得分:1)

我通过以下方式使其成功

cordova plugin rm cordova-plugin-email-composer

然后通过以下命令添加版本0.8.2的插件,因为在插件版本0.8.3中为loolipop打开了一个错误

cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git#0.8.2

index.js

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.receivedEvent('deviceready');

                cordova.plugins.email.open({
                    to:      'test@gmail.com',
                    cc:      'test@gmail.com',
                    bcc:     [],
                    subject: 'Greetings',
                    body:    'How are you? Nice greetings from Naresh'
                });



    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

app.initialize();

希望它可以帮助你..

答案 1 :(得分:0)

我修改了你的代码并立即开始工作。 所以请检查一次。

cordova.plugins.email.isAvailable(function (isAvailable) {
                // alert('Service is not available') unless isAvailable;
                alert("is email mobile available? " + (isAvailable ? "Yes" : "No"));
                if(isAvailable){
                    window.plugin.email.open({
                        to:      'test@test.com',
                        subject: 'Greetings',
                        body:    'How are you? Nice greetings from Leipzig',
                    }, function(){
                        console.log('email view dismissed');
                    },
                    this);
                }
            });

如果警告为“否”,则表示您没有任何电子邮件应用程序或配置。