javascript Cordova/phonegap 项目中的“需要”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19818282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Require" exception in Cordova/phonegap project
提问by Purus
I am trying to build an hybrid mobile app using Phonegap/Cordova in Android platform. And I have succeeded in that too :) The app works as desired in Ripple emulator in my Chrome browser. I am not using Eclipe ADT or Android Studio as the emulator does not load quick.
我正在尝试在 Android 平台上使用 Phonegap/Cordova 构建一个混合移动应用程序。我也成功了 :) 该应用程序在我的 Chrome 浏览器中的 Ripple 模拟器中按需要工作。我没有使用 Eclipe ADT 或 Android Studio,因为模拟器不能快速加载。
I have used onDeviceReady event and Navigator plugin for vibrate and alerts. All these works fine as expected.
我使用 onDeviceReady 事件和 Navigator 插件进行振动和警报。所有这些都按预期工作正常。
When the Developer Console is active in Chrome, the JavaScript breaks in cordova.js with the following error
当开发者控制台在 Chrome 中处于活动状态时,JavaScript 在 cordova.js 中中断并出现以下错误
Uncaught ReferenceError: require is not defined
in the following line. The same happens for notification.js file too. When I press continue in debugger, everything works again as expected apart from that error.
在下一行。通知.js 文件也会发生同样的情况。当我在调试器中按继续时,除了该错误外,一切都按预期工作。
var cordova_events = require('./src/events'),
Order of the java script files are as given below.
java 脚本文件的顺序如下所示。
<script src="js/cordova.js"></script>
<script src="js/notification.js"></script>
<script src="js/vibration.js"></script>
I believe I am not referencing the correct cordova.js file and all other dependencies. But I also wonder how come does the app works fine in Ripple when the developer console is not active.
我相信我没有引用正确的 cordova.js 文件和所有其他依赖项。但我也想知道当开发者控制台未处于活动状态时,该应用程序如何在 Ripple 中正常工作。
I even tried to remove cordova.js file as Ripple automatically includes it as per phonegap deviceready event - ripple emulator
我什至试图删除cordova.js文件,因为Ripple会根据phonegap deviceready事件自动包含它- 波纹模拟器
I have the download copies of phonegap and cordova from their respective sites. I have installed Node.js and have installed the packages too.
我从他们各自的网站上下载了 phonegap 和 cordova 的下载副本。我已经安装了 Node.js 并且也安装了这些包。
My questions here are:
我的问题是:
- Where to find the correct cordova.js file? Which is the correct version to be included in my html files project?
- What's the user of Node.js over here?
- Does it designed to automatically include the scripts based on Node.js? If so what I am missing?
- 在哪里可以找到正确的cordova.js 文件?要包含在我的 html 文件项目中的正确版本是哪个?
- 这里的 Node.js 用户是什么?
- 它是否旨在自动包含基于 Node.js 的脚本?如果是这样,我缺少什么?
I confess that the concept and usage of node.js looks like a rocket science for me.
我承认 node.js 的概念和用法对我来说就像一门火箭科学。
采纳答案by MBillau
Cordova and Phonegap are pretty much the same thing, you shouldn't need to download both. Cordova is the open source project that helps you publish your HTML5 app to multiple different mobile OSes. Phonegap uses Cordova to do that, but also adds some extra features, mostly just being able to build in the cloud instead of on your workstation.
Cordova 和 Phonegap 几乎是一样的东西,您不需要同时下载两者。Cordova 是一个开源项目,可帮助您将 HTML5 应用程序发布到多个不同的移动操作系统。Phonegap 使用 Cordova 来做到这一点,但也增加了一些额外的功能,主要是能够在云中而不是在你的工作站上构建。
Node.js is used by Cordova for a lot of the building steps. Since Cordova works on OSX and Windows machines, we needed a way to write build and package scripts that would work on both operating systems - node.js provides this. When you build an application with Cordova you shouldn't really use node.js at all, unless you are also building a complementary backend system.
Cordova 使用 Node.js 进行许多构建步骤。由于 Cordova 可在 OSX 和 Windows 机器上运行,我们需要一种方法来编写可在两种操作系统上运行的构建和打包脚本——node.js 提供了这一点。当您使用 Cordova 构建应用程序时,您根本不应该真正使用 node.js,除非您也在构建一个互补的后端系统。
Starting with Cordova 3.x, there is a cordova
command line tool that can greatly help you create the application. It takes care of copying the right cordova.js and cordova-android.jar files. You can read about it here http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface
从 Cordova 3.x 开始,有一个cordova
命令行工具可以极大地帮助您创建应用程序。它负责复制正确的cordova.js 和cordova-android.jar 文件。你可以在这里阅读它http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface
Once you get set up you might want to read my other answer that clarifies some of the use cases of cordova
tool: Should a phonegap plugin be declared in the config.xml file?
设置完成后,您可能想阅读我的其他答案,其中阐明了cordova
工具的一些用例:应在 config.xml 文件中声明 phonegap 插件吗?
回答by Ostkontentitan
Dont use the plugins *.js files out of the plugin source.
不要使用插件源中的插件 *.js 文件。
Dont add plugins *.js files as tags into your html
(Cordova loads them on its own based on cordova_plugins.js )
不要将插件 *.js 文件作为标签添加到您的 html 中
(Cordova 基于 cordova_plugins.js 自行加载它们)
The specific error 'require is not defined' comes from missing cordova define in the plugins.js
特定错误 'require is not defined' 来自 plugins.js 中缺少 cordova 定义
cordova.define("org.apache.cordova.file.DirectoryEntry", function(require, exports, module) {
});
To avoid all of this trouble:
为了避免所有这些麻烦:
Use cordova command line interfaceto setup platforms and plugins. It manages all the native and javascript source files and puts them together in the proper way.
使用cordova 命令行界面设置平台和插件。它管理所有本机和 javascript 源文件,并以适当的方式将它们组合在一起。