我有一个common.js
定义为RequireJS的配置:RequireJS - config;路径和垫片不工作
(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
加载资源,而是在执行main.js
时尝试加载http://localhost:8080/js/jsRoutes.js
。但是这个资源不存在,我得到了一个404.
如何让jsRoutes
路径正常工作?我也需要垫片(我不是100%确定)?
我可以调试到common.js
文件,所以路径应该被设置,对吧?
更新1
我相信路径应工作,我让他们定义应该不是吗?从http://requirejs.org/docs/api.html
摘录有可能是当你想而不是直接引用脚本符合“的baseUrl +路径”规则找到它。如果模块ID具有以下特征中的一个,该ID将不通过“的baseUrl +路径”配置过去了,只是被像普通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
路径正在与一个完整的URL
我想我可以做到这一点,看我的更新1.我错了吗? – Neilos 2014-12-06 00:13:09
请参阅页面http://www.webjars.org/documentation上的底部代码窗格,看起来他们正在做类似于我想实现的内容。 – Neilos 2014-12-06 00:25:34
请参阅我的更新3,使用完整网址的文档示例 – Neilos 2014-12-06 00:36:26