Cordova对话框插件无法正常工作

时间:2017-07-05 02:04:53

标签: javascript android html cordova

我已经被困在这个问题上超过10个小时了,我自己也没有运气来解决这个问题。问题是我试图在我的Cordova应用程序上使用此对话框插件,但它无法正常工作。我甚至不认为deviceready脚本正在运行。

任何问题随时可以提出

WWW / index.html中:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content=".." />
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <link href="css/layout-styles.css" rel="stylesheet" type="text/css">
        <link href="css/bootstrap-3.3.7.css" rel="stylesheet" type="text/css">
        <script src="cordova_plugins.js"></script>
        <script src="cordova.js"></script>
        <script src="js/index.js"></script>
        <script src="js/jquery-3.1.1.min.js"></script>
        <script src="js/bootstrap.js"></script>
        <title>Plugins Not Working</title>

        <script src="cordova.js">

            document.addEventListener("deviceready", onDeviceReady(), false)

            function onDeviceReady() {     
                navigator.notification.alert(
                    'Script has loaded',  // message
                    alertDismissed,       // callback
                    'Loaded',             // title
                    'Done'                // buttonName
                );
            };

            function alertDismissd() {
                // do something
            };

         </script>
    </head>
</html>

<plugin name="cordova-plugin-dialogs" spec="^1.3.3" />位于config.xml

2 个答案:

答案 0 :(得分:1)

<script>代码中的代码似乎没问题。如果您使用 -

添加了dialogs插件
cordova plugin add cordova-plugin-dialogs

应该可以正常工作。

不要为包含您代码的脚本添加src="cordova.js"。就这样做 -

<script>
document.addEventListener("deviceready", onDeviceReady(), false)

function onDeviceReady() {     
navigator.notification.alert(
   'Script has loaded',  // message
    alertDismissed,       // callback
    'Loaded',             // title
    'Done'                // buttonName
    );
};

function alertDismissd() {
     // do something
};

</script>

如果它仍然不起作用,您可以inspect in Google Chrome查看并列出Console标签下的错误消息吗?如果<script>未被触发,则onDeviceReady()之前可能会出现一些错误。

看到来自here -

的错误消息后,

更新

  1. 删除<script src="cordova_plugins.js"></script>不需要。插件直接从js
  2. 调用
  3. Cannot read property 'alert' of undefined =&gt;该插件未正确安装。首先使用cmd中的cordova plugin add cordova-plugin-dialogs安装它。
  4. 删除<script src="js/index.js"></script>。当您在<script>标记内执行所有操作时,您不需要index.js或其js内容(有错误)。

答案 1 :(得分:0)

我重新安装了Windows 10并重新安装了cordova。现在所有的插件都可以工作:)对不起,如果这不是你想要的答案,但它对我有用!祝你有个美好的一天!