我有一個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