Phonegap 中本地文件的引用错误

Reference Error With Local Files In Phonegap

我的 phonegap 应用程序使用本地文件时似乎遇到了问题。我正在尝试启动一个简单的应用程序并 运行 扫描二维码并仅显示扫描的文本。但是,由于某种原因它不起作用。我可以 运行 任何我想要的 JavaScript 只要它在 index.html 文件中。出于某种原因,每当我尝试从包含的 js 文件调用函数时,我都会收到引用错误。在这段代码中,我得到 'ReferenceError: scan_qr_code is not defined' 和 'ReferenceError: onDeviceReady is not defined'。这两个函数都来自 main.js 文件,该文件应该包含在 index.html 文件中。我下载并检查了 apk 文件,这些文件包含在构建中,但由于某种原因无法正常工作。有人有什么建议或建议吗?这让我抓狂。看起来这应该是一件相当简单的事情。另外,我正在构建 PhoneGap 版本 3.7.0

我正在使用 PhoneGap build 进行编译,我的文件夹结构也是这样设置的。

/www
  /scripts
    main.js
    jquery-1.11.3.min.js
  /styles
    main.css
  index.html
  config.xml

index.html

<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">

<script type="text/javascript" src="scripts/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="scripts/main.js"></script>

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="childbrowser.js"></script>
<script type="text/javascript" src="scripts/barcode.js"></script>

<link href="styles/main.css" rel="stylesheet" />

<title> </title>

<script>
$(function(){
    alert('here');
     try{
        $("#scan_button").click(function(){
            alert('Button Clicked 2');
            try{
                onDeviceReady();
                scan_qr_code();
            }catch(e){
                alert(e);
            }
        });
    }catch(e){
        alert(e);
    }
});

document.addEventListener("deviceready", function(){
     try{
        $("#scan_button").click(function(){
            alert('Button Clicked');
            try{
                scan_qr_code();
            }catch(e){
                alert(e);
            }
        });
        scan_qr_code();
    }catch(e){
        alert(e);
    }
}, false);
</script>

</head>
<body>

<button id="scan_button">Scan QR Code</button>

</body>
</html>

main.js

$(function(){
});

function onDeviceReady(){
   alert('Device Ready Fired');
}

function scan_qr_code(){
    try{    
        alert('Scanning');
        // Scan the QR code
        barcode_app.scan(function(result){
            code = result.text;
            // Just grab the text from the scanned code
            alert(code);
            scan_qr_code(){
        });
    }catch(e){
        alert(e);
    }
}

config.xml

<preference name="phonegap-version"            value="3.7.0" />
<access origin="*"/>

这将是一个非常大胆的猜测,但看起来您的 main.js 文件有一个不应该存在的额外 {

scan_qr_code(){

这可能会破坏您的代码,这就是为什么 main.js 中的函数未定义的原因。

这里有一个 plunker,表明如果您删除多余的 {,代码 运行 就可以了。嗯... barcode_app 现在显示为未定义,我假设它是 scripts/barcode.js 文件的一部分,顺便说一句,它也不包含在您的项目结构中。