【问题标题】:App works fine in browser but not with cordova应用程序在浏览器中运行良好,但不适用于cordova
【发布时间】:2016-02-29 12:13:46
【问题描述】:

我正在为我的应用程序使用 Cordova 6 和 Requirejs。当我在 iPhone 模拟器中启动我的应用程序时,cordova deviceready 事件被触发,但我通过 require js 加载的代码永远不会被调用:(

我在自己的代码之前加载cordova,等待设备就绪事件触发:

document.addEventListener('deviceready', function() {
    alert('this gets called with cordova so we know this works')
    define('app', ['router'], function(Router) {
        alert('in regular browser i get called, but not in cordova')
    });
}, false);

有没有办法解决这个问题?

【问题讨论】:

  • 也许这就是问题所在? adamauckland.com/posts/… 但是肯定还有其他人同时使用requirejs和cordova吧?
  • 在 Google Chrome 或 Safari 等普通浏览器窗口中,应用程序运行良好,仅当我启用 cordova.js 加载时才会发生。

标签: cordova requirejs


【解决方案1】:

对我来说,结果是从 require 中删除 data-main 属性并直接加载我的主 javascript 文件,然后在 deviceReady 事件触发后正常使用 require

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <script type="text/javascript" src="js/libs/require/require.js"></script>
</head>

<body>

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

</body>

</html>
var app = {

  initialize: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
  },

  onDeviceReady: function() {
    require(['app/requireModuleConfig'], function() {
      require([
          'core/shell',
          'domReady!'
        ],
        function(shell) {
          ko.applyBindings(shell);
          shell.Init();
        });
    });
  }
};

app.initialize();

requireModuleConfig.js

requirejs.config({

    baseUrl: window.location.pathname.replace('shell.html', '') + 'app/',
    //baseUrl: 'http://localhost:3001/app/',
    paths: {
        lib: '../js/libs',
        styles: '../content',
        text: '../js/libs/require/text',
        css: '../js/libs/require/css',
        domReady: '../js/libs/require/domReady',
        baseProxy: 'core/proxies/baseProxy',
        logger: '../js/libs/toastr/logger/logger',
        appContext: 'core/appContext',
        sync: 'core/sync/syncEngine',
        constants: 'core/constants/constants'
    },
    config: {
        text: {
            useXhr: function (url, protocol, hostname, port) {
                return true;
            }
        }
    }
});

【讨论】:

  • 你有同样的问题是我吗?感谢您的回答。
  • 不完全相同的问题,但相似,我的问题是我在 android 设备上丢失了结果意图,只是因为我使用 require 加载了所有内容。不同的是我使用 require 而不是 define,我做的第一件事是加载 require config。
  • 你的配置是什么样的?我试过了,但现在我得到了不匹配的匿名定义()错误。
【解决方案2】:

抱歉,还不能发表评论。但我做了同样的事情,它解决了一个非常相似的问题。

<script type="text/javascript" src="libs/require.js"></script>
<script>
    document.addEventListener('deviceready', function() {
        require.config({
            baseUrl: 'js',
            paths: {
                handlebars: "../lib/handlebars/handlebars",
                text: "../lib/text/text",
                i18n: "../lib/i18n/i18n",
                hbs: "../lib/requirejs-hbs/hbs",
                moment: "../lib/moment/moment",
                moment_fr: "../lib/moment/locale/fr",
                moment_duration: "../lib/moment-duration-format/lib/moment-duration-format"
            },
            shim: {
                handlebars: {
                    exports: "Handlebars"
                }
            },
            config: {
                moment: {
                    noGlobal: true
                },
                i18n: {
                    locale: localStorage.getItem('locale') || 'en'
                }
            }
        });
        require(['init'], function(init) {
            init.coreApp();
        });
    }, false);

</script>

【讨论】:

    猜你喜欢
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2016-02-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多