javascript Requirejs 配置在不同的文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18653492/
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 configuration in different file
提问by barteloma
I am using requirejs. My main.js content is like following.
我正在使用 requirejs。我的 main.js 内容如下。
requirejs.config({
async: true,
parseOnLoad: true,
packages: [],
paths: {
jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min'
}
});
require(["login"], function (loginService) {
loginService.login('validUser');
});
Now, my config elements are little. But later, I will add packages, paths and others, so the require.configlines will increase.
现在,我的配置元素很少。但是稍后,我会添加包、路径等,所以require.config行会增加。
- I wanna separate require.configas a different file and use it?
- If jquery load delays, does the error occurs? My other javascript files are using it.
- 我想将require.config作为不同的文件分开并使用它?
- 如果jquery加载延迟,是否会出现错误?我的其他 javascript 文件正在使用它。
回答by Marius
Yes you can, require your config before you require anything else, like this:
是的,你可以,在你需要其他任何东西之前需要你的配置,像这样:
config example:
配置示例:
require.config({
baseUrl: '/Public/js',
paths: {
jquery: '../../Scripts/jquery-1.10.2.min',
jqueryui: '../../Scripts/jquery-ui-1.10.2.min',
},
shim: {
jqueryui: {
deps: ['jquery']
},
}
waitSeconds: 3
});
and, then I load it:
然后我加载它:
require(['/Public/js/config.js'], function() {
require(['home/index'], function() {
});
});
Just remember that you reference the config.js by path in the first require-statement because require.js can not resolve by baseUrl since it has not been loaded. When you get to the inner require()-statement, its loaded and you can reference dependencies relative to baseUrl.
请记住,您在第一个 require 语句中通过路径引用了 config.js,因为 require.js 无法通过 baseUrl 解析,因为它尚未加载。当您到达内部 require() 语句时,它已加载,您可以引用相对于 baseUrl 的依赖项。
回答by Zathrus Writer
You can put the config into a separate JS file, that's not a problem. Just make sure that file is loaded prior to the require()call in your main code.
If you're using jQuery for other scripts that are not loaded via requireJS, you will get errorsif they happen to load sooner than jQuery. What you need to do is convert all those static files into requireJS modulesand load them all via requireJS. By using a define()function in each of the modules, you can set up dependencies, so all modules will wait for jQuery to load prior to executing their own code.
您可以将配置放入单独的 JS 文件中,这不是问题。只需确保在主代码中的require()调用之前加载该文件。
如果您将 jQuery 用于未通过 requireJS 加载的其他脚本,如果它们碰巧比 jQuery 加载得早,您将收到错误。您需要做的是将所有这些静态文件转换为 requireJS 模块并通过 requireJS 加载它们。通过在每个模块中使用define()函数,您可以设置依赖项,因此所有模块将在执行自己的代码之前等待 jQuery 加载。
回答by enriquecastl
This is an example of a multipage requirejs based project where the requirejs.config
call is in a separate file
这是一个基于多页 requirejs 的项目的示例,其中requirejs.config
调用位于单独的文件中
https://github.com/requirejs/example-multipage/tree/master/www
https://github.com/requirejs/example-multipage/tree/master/www