理解和解决requireJS的报错:MODULE NAME HAS NOT BEEN LOADED YET FOR CONTEXT
2014-11-02 17:49
2667 查看
使用requireJS加载模块的时候,有时候会碰到如下的错误:
Uncaught Error: Module name "module1" has not been loaded yet for context: _. Use require([])
比如下面的代码就会报这个错误:
这个错误在requireJS官网上写的很明白:
This occurs when there is a require('name') call, but the 'name' module has not been loaded yet.
我们先来看下,requireJS中定义模块和加载模块的标准方式:
如果我们需要加载的或者定义的模块比较少,这种标准的写法是很清晰的。
但是如果我们需要加载的模块很多,那么这种一一对应的写法很繁琐。
为了解决这个问题,我们可以使用以下2种方式来定义模块:
方式1:If you are using the simplified define wrapper, make sure you have require as
the first argument to the definition function
方式2:If you are listing dependencies in the dependency array, make sure that require and name are
in the dependency array
但是下面的这种写法就不行,会报错HAS NOT BEEN LOADED YET FOR CONTEXT
官网上的解释是:
This fails because requirejs needs to be sure to load and execute all dependencies before calling the factory function above. If
a dependency array is given to define(), then requirejs assumes that all dependencies are listed in that array, and it will not scan the factory function
for other dependencies. So, either do not pass in the dependency array, or if using the dependency array, list all the dependencies in it.
最后官网上特别强调:require('name')这种写法,只应该出现在define()或者require()的回调函数中。
Be sure that require('name') only
occurs inside a define() definition function or a require() callback function, never in the global space by its own.
可以看到使用define()定义模块的时候,如果依赖的模块比较少,那么可以使用标准方式;如果依赖的模块很多,那么可以使用方式1或者方式2来解决。很显然,使用require()加载模块的时候,也存在和define()一样的问题。经过我的试验:使用方式2也是可以的。
方式3:使用require加载多个模块的时候
总结:使用define()定义模块,使用require()加载模块,可以使用标准方式,或者是方式1,方式2,方式3,这样就能够实现requireJS中模块的正确加载和定义。
Uncaught Error: Module name "module1" has not been loaded yet for context: _. Use require([])
比如下面的代码就会报这个错误:
require([], function() { var module = require("module1"); alert(module.name); });
这个错误在requireJS官网上写的很明白:
This occurs when there is a require('name') call, but the 'name' module has not been loaded yet.
我们先来看下,requireJS中定义模块和加载模块的标准方式:
// 加载模块的标准方式 require(['foo','jquery'], function (foo,$) { //foo is now loaded. }); // 定义模块的标准方式 define(['module1', 'module2'], function(m1, m2) { return { method: function() { m1.methodA(); m2.methodB(); } }; });
如果我们需要加载的或者定义的模块比较少,这种标准的写法是很清晰的。
但是如果我们需要加载的模块很多,那么这种一一对应的写法很繁琐。
define( ['dep1', 'dep2', 'dep3', 'dep4', 'dep5', 'dep6', 'dep7', 'dep8'], function(dep1, dep2, dep3, dep4, dep5, dep6, dep7, dep8){ ... } );
为了解决这个问题,我们可以使用以下2种方式来定义模块:
方式1:If you are using the simplified define wrapper, make sure you have require as
the first argument to the definition function
define( function (require) { var dep1 = require('dep1'), dep2 = require('dep2'), dep3 = require('dep3'), dep4 = require('dep4'), dep5 = require('dep5'), dep6 = require('dep6'), dep7 = require('dep7'), dep8 = require('dep8'); } });
方式2:If you are listing dependencies in the dependency array, make sure that require and name are
in the dependency array
define(['require', 'dep1', 'dep2', 'dep3', 'dep4', 'dep5'], function (require) { var dep1 = require('dep1'); var dep2 = require('dep2'); });
但是下面的这种写法就不行,会报错HAS NOT BEEN LOADED YET FOR CONTEXT
//THIS WILL FAIL define(['require'], function (require) { var namedModule = require('name'); });
官网上的解释是:
This fails because requirejs needs to be sure to load and execute all dependencies before calling the factory function above. If
a dependency array is given to define(), then requirejs assumes that all dependencies are listed in that array, and it will not scan the factory function
for other dependencies. So, either do not pass in the dependency array, or if using the dependency array, list all the dependencies in it.
最后官网上特别强调:require('name')这种写法,只应该出现在define()或者require()的回调函数中。
Be sure that require('name') only
occurs inside a define() definition function or a require() callback function, never in the global space by its own.
可以看到使用define()定义模块的时候,如果依赖的模块比较少,那么可以使用标准方式;如果依赖的模块很多,那么可以使用方式1或者方式2来解决。很显然,使用require()加载模块的时候,也存在和define()一样的问题。经过我的试验:使用方式2也是可以的。
方式3:使用require加载多个模块的时候
//异步加载module1模块,加载完成后调用回调函数 require(["module3","module1","module2"], function() { var m1 = require("module1"); alert(m1.name); });
总结:使用define()定义模块,使用require()加载模块,可以使用标准方式,或者是方式1,方式2,方式3,这样就能够实现requireJS中模块的正确加载和定义。
相关文章推荐
- has not been declared和does not name a type解决办法
- webpack---粗暴解决Module build failed: Error: The node API for 'babel' has been moved to babel-core
- `QtValidLicenseForCoreModule' does not name a type 错误的解决
- 解决 Code::Blocks之It seems that this file has not been built yet.
- 解决"The breakpoint will not currently be hit. No symbols have been loaded for this document" 错误
- CLR has been unable to transition from COM context … for 60 seconds’ error. 此错误的解决方法
- Hadoop出现 native snappy library not available: SnappyCompressor has not been loaded的解决办法
- Virtualbox报错------>make sure the kernel module has been loaded successfully
- Entity Framework 中 使用Include 解决ObjectContext instance has been disposed and can no longer be used for operations that require a connection错误 -原创
- code:block运行遇到It seems that this project has not been built yet.问题解决
- Workbench has not been created yet" 尚未解决【问题】
- redis异常解决:attempted to load session[xxx] which has been created but not yet serialized.
- getOutputStream() has already been called for this response问题的解决
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- getOutputStream() has already been called for this response的解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法
- java.lang.IllegalStateException:getOutputStream() has already been called for this response的解决方法
- tomcat5下jsp出现getOutputStream() has already been called for this response异常的原因和解决方法