简体   繁体   English

require.js没有加载任何模块

[英]require.js is not loading any modules

I am trying to load a module using require.js, and I have the following in my app.js: 我正在尝试使用require.js加载模块,我的app.js中有以下内容:

require.config({
baseUrl: "js"
});


alert("hello world"); // for debugging

require(['eh2'], function(eh2) {
    alert("nothing here"); // for debugging
});

When I run my application, though, despite the app.js being loaded, the module I'm requiring is never loaded - the "hello world" executes, but the "nothing here" doesn't! 但是,当我运行我的应用程序时,尽管app.js正在加载,但我要求的模块从未加载 - “hello world”执行,但“此处没有”不会!

My script tag in the HTML page looks like this: 我在HTML页面中的脚本标记如下所示:

<script type="text/javascript" src="js/lib/require.js" data-main="app"></script>

And eh2.js is located in the js folder, and it is wrapped in a define statement: 并且eh2.js位于js文件夹中,它包含在define语句中:

define(["./screens/Screens"], function(screens) {
    return {
        // code here
    };
});

What am I doing wrong? 我究竟做错了什么? Is require.js silently failing on loading some submodule under screens.js, perhaps? require.js是否默默无法在screens.js下加载一些子模块,也许?

Here is the code from the Screens module: 以下是Screens模块的代码:

    define([ "screens/TitleScreen", "screens/GameScreen" ], function(titleScreen, gameScreen) {
    return {

        screenFuncs: {
            "TitleScreen" : titleScreen.TitleScreen,
            "GameScreen" : gameScreen.GameScreen,
        },

        buildScreen: function(data) {
            var func = screenFuncs[data.type];
            var screen = new func(data.params);
            return screen;
        },
    };
});

Do the paths in the define call need to be relative to the current location of the js file I'm in, or to the root defined in the app.js, anyway? 定义调用中的路径是否需要相对于我所在的js文件的当前位置,或者相对于app.js中定义的根?

replace this: 替换这个:

define(["./screens/Screens"], function(screens) {
    ....
});

either with an absolut path variant: 要么是absolut路径变体:

define(["screens/Screens"], function(screens) {
    ....
});

or use: 或使用:

define(function(require) {
    var screens = require("./screens/Screens");
    ....
});

From the docs : 来自文档

Relative module names inside define(): define()中的相对模块名称:

For require("./relative/name") calls that can happen inside a define() function call, be sure to ask for "require" as a dependency, so that the relative name is resolved correctly: 对于可能在define()函数调用中发生的require(“./ relative / name”)调用,请务必要求“require”作为依赖项,以便正确解析相对名称:

define(["require", "./relative/name"], function(require) {
    var mod = require("./relative/name");
});

Or better yet, use the shortened syntax that is available for use with translating CommonJS modules: 或者更好的是,使用可用于翻译CommonJS模块的缩短语法:

define(function(require) {
    var mod = require("./relative/name");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM