cordova.js导致参考错误“未定义要求" [英] cordova.js causes reference error 'require is not defined'

查看:258
本文介绍了cordova.js导致参考错误“未定义要求"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用 ngCordova 检测设备的网络状态.但是,一旦包含 cordova.js ,我就会收到一个错误-

I'm wish to use ngCordova to detect the network state of a device. However, as soon as I include cordova.js I get an error -

未捕获的ReferenceError:未定义require(cordova.js:23)

Uncaught ReferenceError: require is not defined (cordova.js:23)

我已经安装并成功运行 Node.js Cordova ,并且我已经下载并安装了 ngCordova 按照此处的说明进行操作.

I've installed and am successfully running Node.js and Cordova, and I've downloaded and installed ngCordova following the instructions here.

我还通过 Cordova CLI -

cordova插件添加cordova-plugin-network-information

cordova plugin add cordova-plugin-network-information

根据文档,这是我正在使用的代码示例(并且要澄清的是,一旦包含cordova.js,就会发生错误,因此即使没有此代码片段,我仍然会收到错误;这表明我最终目标)-

As per the documentation this is the code sample that I'm using (and to clarify, the error occurs as soon as I include cordova.js, so even without this code snippit I still get the error; this is show my end goal) -

var app = angular.module('myApp', ['ngCordova']);

app.controller('myCtrl', ['$rootScope', '$scope', '$cordovaNetwork', function($rootScope, $scope, $cordovaNetwork){

    document.addEventListener("deviceready", function () {

        var type = $cordovaNetwork.getNetwork()

        var isOnline = $cordovaNetwork.isOnline()

        var isOffline = $cordovaNetwork.isOffline()


        // listen for Online event
        $rootScope.$on('$cordovaNetwork:online', function(event, networkState){
            var onlineState = networkState;
        })

        // listen for Offline event
        $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
            var offlineState = networkState;
        })

    }, false);

}]);

在我的 index.html 文件中,我已确保已声明文件按指定顺序是必需的-

In my index.html file I've made sure that I have declared the files are required in the order specified -

  1. angular.min.js
  2. ngCordova.min.js
  3. cordova.js
  1. angular.min.js
  2. ngCordova.min.js
  3. cordova.js

如何解决此错误?

推荐答案

此处的答案是, Cordova CLI在构建应用程序时会自动包含cordova.js和任何必要的依赖项.

The answer here is that the Cordova CLI automatically includes cordova.js and any necessary dependancies when you build the application.

cordova.js文件位于www目录中,因此您要做的就是包括该文件-

The cordova.js file is placed in the www directory, so all you have to do is include that file -

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

引用您没有插入的文件似乎是不合逻辑的,但是一旦您了解了CLI的工作原理,这是很有意义的.

It seems illogical to reference a file that you are not inculding, but once you get your head around how the CLI works it makes sense.

对于初学者来说,主要的问题是它的文档非常少.实际上,整个 Cordova 似乎文献不多,在数量上不是那么重要,而在相关性上却是如此-对于旧版本而言,太多了.不过,这是一个很棒的项目,希望这会在不久的将来得到解决.

The main problem for beginners is that it's quite poorly documented. In fact, Cordova as a whole seems to be poorly documented, not so much in terms of quantity but rather the relevance - so much is for old versions. It's a fantastic project though, and hopefully this is something that will be addressed in the near future.

这篇关于cordova.js导致参考错误“未定义要求"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆