【问题标题】:Socket.io variable not defined in Phonegap mobile applicationPhonegap 移动应用程序中未定义 Socket.io 变量
【发布时间】:2016-11-07 09:54:42
【问题描述】:

我正在尝试使用带有 RequireJs、Backbone、jQuery 的 Phonegap 开发移动应用程序,每次尝试包含 requirejs 脚本标签时都会遇到问题:

<script data-main="js/app" src="node_modules/requirejs/require.js</script>

包含此内容后,我收到以下错误:

ReferenceError: io is not defined.

我没有使用socket.io,但我认为 Phonegap 正在使用它来刷新浏览器中的页面。

这是我的index.html 文件:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <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" />

    <!-- This is a wide open CSP declaration. To lock this down for production, see below. -->
    <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *" />

    <title>Hello World</title>
</head>
<body>

    something nice

    <!-- Content -->

    <script type="text/javascript" src="cordova.js"></script>
    <script data-main="js/app" src="node_modules/requirejs/require.js"></script>
</body>
</html>

这是我的js/app.js 文件:

requirejs.config({

    baseUrl: 'js',

    shim: {
        'socket.io': {
            exports: 'io'
        },
        'underscore': {
            exports: '_'
        },
        'backbone': {
            deps: [
                'underscore',
                'jquery'
            ],
            exports: 'Backbone'
        }
    },

    paths: {

        jquery: 'jquery.min',
        underscore: 'lodash.min',
        backbone: 'backbone',
        socketio: '../socket.io/socket.io'

        // package: 'node_modules'
    }

    // map: {
    //
    //     '*': {
    //         'jquery': 'private/jquery'
    //     },
    //
    //     'private/jquery': {
    //         'jquery': 'jquery'
    //     }
    // }
});

我使用的是 Phonegap 6.3.4 版。

你能告诉我,我应该怎么做才能摆脱这个错误?

谢谢!

【问题讨论】:

    标签: javascript cordova socket.io requirejs phonegap-cli


    【解决方案1】:

    我通过阅读这些文章设法解决了这个问题:

    这是我的解决方法:

    <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript">
    
            // Fixes "Uncaught ReferenceError: io is not defined".
            // We need to load RequireJs after socket.io has been loaded.
    
            function injectRequireJs() {
                var h = document.getElementsByTagName('body')[0];
                var s = document.createElement('script');
                s.type = 'text/javascript';
                s.src = 'node_modules/requirejs/require.js';
                s.setAttribute('data-main', 'js/bootstrap');
                h.appendChild(s);
            }
    
            setTimeout(function(){
                injectRequireJs();
            }, 1);
    
        </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-12
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2021-01-24
      相关资源
      最近更新 更多