无法调用cordova中的功能 - visual studio

时间:2014-12-09 20:35:08

标签: cordova typescript cordova-3 visual-studio-cordova

我正在通过visual studio cordova工具创建第一个cordova应用程序 我正在使用Windows 7和VS社区(如果这很重要)和波纹模拟器 问题是我有这样的代码:<button onclick="showAlert(); return false;">Click Me</button>我的函数定义为in index.ts 但是点击按钮时我收到错误Exception: showAlert is not defined 我在这里想念的是什么? 这是强制性代码:
的index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Cordova01</title>

    <!-- Cordova01 references -->
    <link href="css/index.css" rel="stylesheet" />
</head>
<body>
    <p>Hello, your application is ready!</p>
    <p><strong>Hello, from Cordova :)</strong></p>
    <button onclick="showAlert(); return false;">Click Me</button>
    <!-- Cordova reference, this is added to your app when it's built. -->
    <script src="cordova.js"></script>    
    <script src="scripts/platformOverrides.js"></script>
    <script src="scripts/index.js"></script>
</body>
</html>

index.ts

module Cordova01 {
    "use strict";

    export module Application {
        export function initialize() {
            document.addEventListener('deviceready', onDeviceReady, false);
        }

        function onDeviceReady() {
            // Handle the Cordova pause and resume events
            document.addEventListener('pause', onPause, false);
            document.addEventListener('resume', onResume, false);

            // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.

        }

        function showAlert() {
            navigator.notification.alert(
                "This is simple message!",  // message
                null,       // callback
                "Alert View Title", // title
                'OK'        // buttonName
                );    
        }

        function onPause() {
            // TODO: This application has been suspended. Save application state here.
        }

        function onResume() {
            // TODO: This application has been reactivated. Restore application state here.
        }

    }

    window.onload = function () {
        Application.initialize();
    }


}

1 个答案:

答案 0 :(得分:1)

您几乎就在那里,您应该将showAlert功能更改为export function showAlert() 并且在html中也改变它<button onclick="Cordova01.Application.showAlert(); return false;">Click Me</button>

在你的情况下,它找不到showAlert函数,因为它在一个模块中。另一种方法是将showAlert函数移出模块进行测试,这也应该有效!

请参阅小提琴示例:fiddle