javascript - RequireJS - 配置;路径和垫片不起作用

标签 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

There may be times when you do want to reference a script directly and not conform to the "baseUrl + paths" rules for finding it. If a module ID has one of the following characteristics, the ID will not be passed through the "baseUrl + paths" configuration, and just be treated like a regular URL that is relative to the document:

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 路径正在使用完整的 URL

最佳答案

我相当确定您的路径应该与您的 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']);

关于javascript - RequireJS - 配置;路径和垫片不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326672/

相关文章:

javascript - 调整列表宽度时消失的列表元素

javascript - Fuelux 数据网格并需要 js

javascript - 创建扩展时出现 Qlik Sense 错误

javascript - Angular 和链接下拉列表不保存数据

javascript - 我想在 jquery 对话框中从数据库获取 id

javascript - 运行Requirejs Optimizer后,JQuery插件无法使用

asynchronous - 脚本错误(:0) when trying to run async test in mocha-phantomjs

javascript - 将 javascript 变量传递给 AJAX 成功函数

javascript - backbone、requirejs、grunt requirejs 使用外部模板

javascript - 使用 requirejs 和 typescript 的 Node 模块