理解和解决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');
}
});
方式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中模块的正确载入和定义。
理解和解决requireJS的报错:MODULE NAME HAS NOT BEEN LOADED YET FOR CONTEXT的更多相关文章
- 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7
最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...
- 解决:MySQL 报错:1045 - Access denied for user 'root'@'localhost'(using password YES)
一.前言 今年疯狂迷上了开源,只要看到好的开源项目,就会不顾一切一股脑扎进去研究,五一期间发现一个很好的关于众筹的开源项目,但不巧,这个项目竟然是 PHP 写的,没学过 PHP,自然对这个开源项目毫无 ...
- Python报错module 'scipy.misc' has no attribute 'xxx'
Python报错module 'scipy.misc' has no attribute 'imresize' 解决办法: 安装Pillow包,命令如下: pip install Pillow 然后重 ...
- 解决kylin查询报错:org.apache.kylin.rest.exception.InternalErrorException
报错信息: -- ::, ERROR [Query 12e9c054-760c---b1f06724c9b6-] service.QueryService: : Exception when exec ...
- homestead虚拟机,通过npm下载依赖包和解决运行gulp报错问题 yarn出错问题
homestead虚拟机,通过npm下载依赖包和解决运行gulp报错问题 yarn出错问题 1. 在虚拟器运行 npm 下载依赖组件时报错: npm ERR! EPROTO: protocol err ...
- 完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径。)
完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径.) 错误原因 读出文件的路径需要有被拷贝的文件名,否则无法解析地址 源代码(用 ...
- PHP报错: Can't use method return value in write context
$dp_id = $this->getParam('dpId'); if(!empty($this->getParam('dpId'))) { $this->smarty->a ...
- jacob 多个web项目报错 jacob-1.14.3-x64.dll already loaded in another classloader jacob
多个web项目报错 jacob-1.14.3-x64.dll already loaded in another classloader jacob 这个问题困扰了很久,网上很多解决方案,很多都不成功 ...
- 解决Python模块报错:ModuleNotFoundError: No module name 'StringIO'
下面是我在学习中遇到的问题,给大家分享一下: ''' 这里是测试代码 '''# coding = utf-8from selenium import webdriverfrom selenium. ...
随机推荐
- (转)淘淘商城系列——使用Jedis操作集群
http://blog.csdn.net/yerenyuan_pku/article/details/72862084 通过上文的学习,我相信大家应该已经知道如何搭建Redis集群了,本文我将为大家介 ...
- RocketMQ学习笔记(15)----RocketMQ的消息模式
在前面学习ActiveMQ时,看到ActiveMQ可以是队列消息模式,也可以是订阅发布模式. 同样,在RocketMQ中,也存在两种消息模式,即是集群消费模式和广播消费模式. 1. 集群消费模式 跟A ...
- CAD与用户互在图面上得到一个矩形框(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::ExApp_CutDwg 与用户互在图面上得到一个矩形框,详细说明如下: 参数 说明 IN DOUBLE dX1 保存范围的左下角位置 ...
- CAD向控件注册一个命令
_DMxDrawX::RegistUserCustomCommand 向控件注册一个命令,用户在命令行输入命令名这个字符串,就会触发执行命令事件 命令事件的id就是该注册时的id值,成功返回true. ...
- Spring框架系列(九)--MyBatis面试题(转载)
1.什么是Mybatis? 1.Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动.创建 连接.创建statement ...
- 爬虫之Requests库
官方文档:http://cn.python-requests.org/zh_CN/latest/ 一.引子 import requests resp = requests.get("http ...
- react----父子组件之间的参数传递
1.父组件向子组件传递参数 //父组件 import React from 'react'; import './header.css' import ComponentChild from './h ...
- 关于JavaScript的一些笔试题
1.原题: function Foo() { getName = function () { alert (); }; return this; } Foo.getName = function () ...
- Apache Maven Cookbook(八)学习笔记-Handling Typical Build Requirements
Including and excluding additional resources Using the Maven Help Plugin: mvn help:effective-pom mvn ...
- 【Codeforces 1037D】Valid BFS?
[链接] 我是链接,点我呀:) [题意] 让你判断一个序列是否可能为一个bfs的序列 [题解] 先dfs出来每一层有多少个点,以及每个点是属于哪一层的. 每一层的bfs如果有先后顺序的话,下一层的节点 ...