理解和解决requireJS的报错:MODULE NAME HAS NOT BEEN LOADED YET FOR CONTEXT
使用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'); } });
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); });
相关文章
- Delphi 解决StrToDateTime()不是有效日期类型的问题
- 解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法
- Javaweb中解决跨越问题的拦截器代码
- git 相关流程和报错解决
- 【COCOS2DX-LUA 脚本开发之七】解决( ERROR IN FUNCTION ‘ADDCHILD’. ARGUMENT #2 IS ‘XX’; ‘CCNODE’ EXPECTED. )异常报错的方法
- 解决远程登录mysql数据库报1130错误
- 【解决】联想thinkpad E40 如何禁用触摸板
- live555 RTSP推送到Darwin出现404错误的解决
- 解决kata containers报错:ERRO[0000] host system doesn‘t support vsock
- 分布式系统一致性问题解决实战(阿里) 异步解耦+消息队列可作为分布式系统满足最终一致性的优秀方案
- 成功解决:Win系统下的Tensorflow使用CPU而不使用GPU运行加速
- 已解决vscode配置原因运行Python读取文件报错
- Mac环境下Jenkins部署Python报错 - ModuleNotFoundError: No module named ‘selenium‘ (已完美解决)
- java - Could not find tools.jar. Please check that E:xxxxx contains a valid JDK installation 报错解决
- vue3 报错解决:找不到模块‘xxx.vue’或其相应的类型声明。(Vue 3 can not find module)
- idea-debug启动会卡住不动,BeanPostProcessors (for example: not eligible for auto-proxying),报错解决
- K8s kubectl 报错 c-bash: _get_comp_words_by_ref: command not found解决过程
- C++使用技巧(一):解决C++类class C 库函数strcpy使用方法以及出现的报错问题: ‘strcpy’ was not declared in this scope
- Win11系统开机黑屏无法显示桌面怎么解决?
- Git知识总览(三) 分支的创建、删除、切换、合并以及冲突解决
- 海带软件分享——Office 2021全家桶安装教程(附报错解决方法)
- Unable to find RoutePredicateFactory with name path(报错解决)
- GStreamer1.0中no element “ffmpegcolorspace”报错解决(八)
- Gerrit代码提交: [remote rejected] prohibited by gerrit: ref update access denied报错解决