javascript 如何访问 Require.js 的配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13997001/
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
How can I access configuration of Require.js
提问by Simon Boudrias
I try to get the baseUrl
configuration of Require.js inside a module, but I can't find where it is stored.
我尝试baseUrl
在模块中获取Require.js的配置,但找不到它的存储位置。
define([], function() {
// Here I'd like to access the `baseUrl` require.js is using
var baseUrl = requirejs.config().baseUrl;
});
In my case, the baseUrl
is set up by Require.js using the data-main
attribute of the script file.
就我而言,baseUrl
是由 Require.js 使用data-main
脚本文件的属性设置的。
I know I can request module
to access the config
attributes (e.g. define(['module'])
), but I can't find how to access the higher level of configuration option.
我知道我可以请求module
访问config
属性(例如define(['module'])
),但我找不到如何访问更高级别的配置选项。
采纳答案by epascarello
Do you want to use toUrl?
你想使用toUrl吗?
define({
load: function (name, parentRequire, load, config) {
var fullUrl = parentRequire.toUrl("foo/bar.css");
}
});
edit:
Starting in require.js 2.1.3, calling toURL
return the path without extension. As so, to get the baseUrl:
编辑:
从 require.js 2.1.3 开始,调用toURL
返回不带扩展名的路径。因此,要获取 baseUrl:
var baseURL = require.toUrl();
回答by xdemocle
You can also reach the config into:
您还可以访问配置:
requirejs.s.contexts._.config
to inspect the config object directly.
直接检查配置对象。
https://groups.google.com/forum/#!topic/requirejs/Hf-qNmM0ceI
https://groups.google.com/forum/#!topic/requirejs/Hf-qNmM0ceI
回答by Steven
In RequireJS 2.1.5, you can get the base URL just like epascarello says, except you'll need to pass the empty string.
在 RequireJS 2.1.5 中,您可以像 epascarello 所说的那样获取基本 URL,但您需要传递空字符串。
var baseURL = require.toUrl('');