RequireJS - config;路径和垫片不起作用

时间:2014-12-06 00:05:49

标签: javascript requirejs

我有一个common.js来定义RequireJS的配置:

(function(requirejs) {
    "use strict";

    requirejs.config({
        baseUrl: "/js",
        paths: {
            "jsRoutes": "http://localhost:8080/app/jsroutes"
        },
        shim: {
            "jsRoutes": {
                exports: "jsRoutes"
            }
        }
    });

    requirejs.onError = function(err) {
        console.log(err);
    };
})(requirejs);

然后我有一个main.js文件,我尝试使用我创建的jsRoutes路径:

require(["./common", "jsRoutes"], function (common, routes) {
    // do something interesting
});

但是我没有在http://localhost:8080/app/jsroutes加载资源,而是在http://localhost:8080/js/jsRoutes.js执行时尝试加载main.js。但这个资源不存在,我得到了404。

如何让jsRoutes路径正常工作?我也需要垫片(我不是100%肯定)?

我可以调试common.js文件,因此应该设置路径,对吗?

更新1

我认为路径应该有效,因为我已经定义了它们不是吗?

摘自http://requirejs.org/docs/api.html

  

有时您可能希望直接引用脚本而不符合“baseUrl + paths”规则来查找它。如果模块ID具有以下特征之一,则ID将不会通过“baseUrl + paths”配置传递,只是被视为与文档相关的常规URL:

Ends in ".js".
Starts with a "/".
Contains an URL protocol, like "http:" or "https:".

更新2

我可能误解了文档,我可以通过定义main.js来解决问题:

require(["./common", "http://localhost:8080/app/jsroutes"], function (common, routes) {
    // do something interesting
});

我当时希望不要绕过这个相当笨拙的URL。

更新3

对文档的进一步调查揭示了以下片段:

requirejs.config({
    enforceDefine: true,
    paths: {
        jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min'
    }
});


//Later
require(['jquery'], function ($) {
    //Do something with $ here
}, function (err) {
    //The errback, error callback
    //The error has a list of modules that failed
    var failedId = err.requireModules && err.requireModules[0];
    if (failedId === 'jquery') {
        //undef is function only on the global requirejs object.
        //Use it to clear internal knowledge of jQuery. Any modules
        //that were dependent on jQuery and in the middle of loading
        //will not be loaded yet, they will wait until a valid jQuery
        //does load.
        requirejs.undef(failedId);

        //Set the path to jQuery to local path
        requirejs.config({
            paths: {
                jquery: 'local/jquery'
            }
        });

        //Try again. Note that the above require callback
        //with the "Do something with $ here" comment will
          //be called if this new attempt to load jQuery succeeds.
        require(['jquery'], function () {});
    } else {
        //Some other error. Maybe show message to the user.
    }
});

此处似乎jquery路径正在使用完整的网址

3 个答案:

答案 0 :(得分:1)

我相当肯定你的路径应该与你的baseUrl相关。所以给它域名&港口搞砸了。

编辑:我的标准需要js配置......它可能会有帮助吗?

require.config({
  baseUrl : "./",
    paths: {

    // Bower Components
    respond:      'assets/bower_components/respond/dest/respond.min',

    // Libraries & Polyfills
    polyfillGCS:  'assets/js/lib/polyfill-getComputedStyle', 
    polyfillRAF:  'assets/js/lib/polyfill-requestAnimationFrame',
    polyfillPro:  'assets/js/lib/polyfill-promise', 
    easing:       'assets/js/lib/easing',
    signalsui:    'assets/js/lib/Signals.ui',
    signalsjs:    'assets/js/lib/Signals',
    domReady:     'assets/js/lib/domReady', // TODO: Still needed?

    // Modules
    app:          'assets/js/es5/app'

    },
    shim: {
    app: {
      deps: ['signalsjs']
    },
    signalsjs: {
      deps: ['easing', 'polyfillGCS', 'polyfillRAF']
    },
    signalsui: {
      deps: ['signalsjs']
    }
    }
});

// Load the app
require(['app']);

答案 1 :(得分:1)

好的,我意识到我做错了什么。这很简单。

我将./commonjsRoutes的依赖项传递给同一模块,因此在配置定义之前需要jsRoutes

我将依赖项从main.js文件移动到实际需要的位置,并且按预期工作。

答案 2 :(得分:0)

我遇到了同样的问题,但是我通过更改代码来解决了该问题,例如原始代码:

require(["./common", "jsRoutes"], function (common, routes) {
    // do something interesting
});

对此:

require(["./common"], function (common) {
    require(["jsRoutes"], function (routes) {
        // do something interesting
    });
});

我猜想,在原始代码中,RequireJS尝试在应用"jsRoutes"中所做的配置更改之前,先加载"common"依赖项 。有效地嵌套require调用可确保仅在评估第一个依赖项之后才加载第二个依赖项。

相关问题