javascript RequireJS 不遵循设置 baseUrl 的 data-main 的相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19682610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RequireJS does not follow relative path for data-main with baseUrl set
提问by personne3000
Using requireJS, I am trying to specify a path for my data-main that is different from the baseUrl. It seems that requireJS is ignoring whatever I type before the file name, and always look for the file in the baseUrl folder.
使用 requireJS,我试图为我的 data-main 指定一个与 baseUrl 不同的路径。似乎 requireJS 忽略了我在文件名之前输入的任何内容,并始终在 baseUrl 文件夹中查找该文件。
I have the following folder structure :
我有以下文件夹结构:
index.html
scripts/
lib/
require.js
test/
main2.js
config.js
Contents of index.html :
index.html 的内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<script data-main="test/main2" src="scripts/lib/require.js"></script>
<script src="scripts/config.js"></script>
</head>
<body></body>
</html>
Contents of config.js :
config.js 的内容:
requirejs.config({
baseUrl: "scripts"
});
And I am getting a 404 error for : GET [...]/scripts/main2.js , even though it should be looking for [...]/scripts/test/main2.js. If I remove the config.js file and use data-main="scripts/test/main2" it works, but I would like to be able to specify a baseUrl for my project.
我收到了一个 404 错误: GET [...]/scripts/main2.js ,即使它应该寻找 [...]/scripts/test/main2.js。如果我删除 config.js 文件并使用 data-main="scripts/test/main2" 它可以工作,但我希望能够为我的项目指定 baseUrl。
Any ideas ?
有任何想法吗 ?
Edit : following the answer by Waxen :
编辑:按照 Waxen 的回答:
- Even if I use "scripts/test/main2", "/scripts/test/main2", or "whateverIWant/main2" in my data-main, it oddly always looks for "scripts/main2.js"
- 即使我在我的数据主中使用“scripts/test/main2”、“/scripts/test/main2”或“whateverIWant/main2”,它也奇怪地总是寻找“scripts/main2.js”
Note that I am using requirejs 2.1.8
请注意,我使用的是 requirejs 2.1.8
回答by Waxen
This isn't working how you want it to because you're calling require with a data-main before you're setting the baseURL. I'm not sure why it's trying to go to scripts/main2.js though; I would expect it to attempt to load test/main2.js rather than scripts/main2.js. However, that's beside the point.
这并不像您希望的那样工作,因为您在设置 baseURL 之前使用 data-main 调用 require。不过,我不确定它为什么要尝试转到 scripts/main2.js;我希望它尝试加载 test/main2.js 而不是 scripts/main2.js。然而,这不是重点。
What you need to do is make sure that your baseURL is available to require before it tries to load you data-main. This can be accomplished by including your config first and using the syntax from the second example here: http://requirejs.org/docs/api.html#config.
您需要做的是确保您的 baseURL 在它尝试加载您的 data-main 之前可用于要求。这可以通过首先包含您的配置并使用此处第二个示例中的语法来完成:http: //requirejs.org/docs/api.html#config。
Contents of index.html :
index.html 的内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
<script src="scripts/config.js"></script>
<script data-main="test/main2" src="scripts/lib/require.js"></script>
</head>
<body></body>
</html>
Contents of config.js :
config.js 的内容:
var require = {
baseUrl: "scripts"
};
回答by Jazimov
I read this thread and couldn't quite understand why using the data-main attribute to point to a configuration js file wouldn't be the same as specifying the configuration before loading Require, as the answer to this thread suggests.
我读了这个线程并且不太明白为什么使用 data-main 属性指向配置 js 文件与在加载 Require 之前指定配置不同,正如这个线程的答案所暗示的那样。
In my experiments, I learned that setting values using a data-main config js file mightwork (then, again, it might not). To those new to Require and AMDs and asynchronisity in general, the "might work" notion has to do with the fact that asynchronous operations run when then can--and not in any predictable order.
在我的实验中,我了解到使用 data-main config js 文件设置值可能会起作用(然后,它可能不会)。对于那些刚接触 Require 和 AMD 以及一般异步性的人来说,“可能会起作用”的概念与异步操作在可以运行时运行的事实有关 - 而不是以任何可预测的顺序运行。
Having established that, there is a very important point made in the current version of the RequireJS documentation that eluded me until now:
确定这一点后,在 RequireJS 文档的当前版本中提出了一个非常重要的观点,直到现在我都没有注意到:
You may also call require.config from your data-main Entry Point, but be aware that the data-main script is loaded asynchronously. Avoid other entry point scripts which wrongly assume that data-main and its require.config will always execute prior to their script loading.
您也可以从 data-main 入口点调用 require.config,但要注意 data-main 脚本是异步加载的。避免其他入口点脚本错误地假设 data-main 及其 require.config 将始终在脚本加载之前执行。
For further information, see: http://requirejs.org/docs/api.html#data-main
有关更多信息,请参阅:http: //requirejs.org/docs/api.html#data-main
Being new to RequireJS, I was mildly appalled to learn this--and I wasted a lot of time trying to debug path-access problems. At this stage it is unclear to me why anyone would use data-main (especially to define a baseUrl) since this reference will only randomly work. Instead the solution suggested by this thread (in which you set the baseUrl in a script tag that is NOT asynchronous, will work as expected and will reliably set the RequireJS configuration before kicking-off RequireJS.
作为 RequireJS 的新手,我对学习这一点感到有点震惊——而且我浪费了很多时间来调试路径访问问题。在这个阶段,我不清楚为什么有人会使用 data-main(尤其是定义 baseUrl),因为这个引用只会随机工作。相反,该线程建议的解决方案(您在非异步脚本标记中设置 baseUrl,将按预期工作,并在启动 RequireJS 之前可靠地设置 RequireJS 配置。