2014-09-28 91 views
0

我在基于RequireJs的应用程序中将baseUrl设置为/angular有时候我想设置相对于底座的路径,有时候我想将它设置为相对于当前目录。从`RequireJs`指定相对路径

我对RequireJs很新,很困惑。

这是我现在有:

require([ 
    'require', 
    'angular', 
    'module/draft/draftDisplay'// module that I want relative to baseUrl 
], function(requireLocal, angular) { 

requireLocal('./autoSave'); // Modules that I want relative to current url 
requireLocal('./module'); 

它创建这个错误:Error: Module name "autoSave" has not been loaded yet for context: _

正如我以前说过,我不能得到RequireJs是怎样工作的手柄。有些事情我不明白是:

1)什么时候RequireJs使用的baseUrl,当它使用当前目录

2)什么是模块ID和它的路径之间的区别?

3)是否以前模块的require([...],..数组中指定的方式影响后续的人怎么都解决了(这似乎是这样的,当我修修补补)

如果你可以包括在你的答案的那些东西,那会非常有帮助。

回答

1

1) When does RequireJs use the baseUrl and when does it use the current directory

baseUrl总是,当模块名通过./../,在这种情况下,它使用了需要其它模块的目录前缀除外。

2) What's the difference between a module ID and its path?

该路径包括baseUrl。例如。可能是类似scripts/vendor/angular/angular.js。模块ID不包括baseUrl或后缀,例如对于baseUrl: 'scripts',上面的模块ID将为vendor/angular/angular

3) Does the way previous modules are specified in the require([...],.. array affect how subsequent ones are resolved (which seemed to be the case when I tinkered)


你的错误是从使用的require为未装载的模块的同步版本号引起的。改用异步版本;如:

requireLocal(['./autoSave', './module'], function(autosave, module) { 
    // here autosave and module are loaded 
}); 
// BEWARE: Here neither autosave nor module are loaded 

但是你确定你想要内在的要求吗?从您提供的信息,拉动需求外需可能就够了,即

require(['require','angular','module/draft/draftDisplay','./autoSave','./module'],...) 

而且,所谓'module'进口在需要特殊的意义;它包含有关模块当前模块的信息;如果您字面上使用的是'./module',它将与'module'(第一个在该文件所在的目录中查找module.js文件,第二个文件提供有关此模块的信息)完全不同。