理解和解决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. ...
随机推荐
- dutacm.club_1085_Water Problem_(矩阵快速幂)
1085: Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Othe ...
- @objc and dynamic
@objc and dynamic Objective-C runtime visibility and the depths of dynamic dispatch in the modern ...
- Shell输入/输出重定向
输出重定向 重定向一般通过在命令间插入特定的符号来实现.特别的,这些符号的语法如下所示 command1 >file1 上面这个命令执行command1然后将输出的内容存入file1. 注意任何 ...
- 百度地图API获取数据
目前,大厂的服务范围越来越广,提供的数据信息也是比较全的,在生活服务,办公领域,人工智能等方面都全面覆盖,相对来说,他们的用户基数大,通过用户获取的信息也是巨大的.除了百度提供api,国内提供免费AP ...
- 使用Sophus练习李群SO3、SE3以及对应的李代数so3、se3
这是高博<视觉SLAM14讲,从理论到实践>第4章的练习.加了一些注释和理解: #include <iostream>#include <cmath>using n ...
- 面向对象程序设计--Java语言第一周编程题:分数
分数 题目内容: 设计一个表示分数的类Fraction.这个类用两个int类型的变量分别表示分子和分母. 这个类的构造函数是: Fraction(int a, int b) 构造一个a/b的分数. 这 ...
- jdbcTemplate传参使用Map或List
List传参方式 举个例子 sql = "select * from table where id=? and param=?": sql中的参数要用?形式,然后使用list.ad ...
- LeetCode_16 3SumCloest
3Sum Closest Given an array nums of n integers and an integer target, find three integers in nums su ...
- 设置Python解析器
如果同时安装了多个Python,如 Python2.7 和 Python3.7 .如果某些特殊原因(比如有些框架只能在Python2.7中使用),需要修改程序在 Python2.7 下运行,即可设置P ...
- Python随笔day03
温故知新: 注释: 单行注释 # 多行注释 ‘’’ ‘’’ 或者 “”” “”” 注意:三个单引号或双引号可以用于表示多行字符串. 判断输入的字符串是否是数字 salary = inpu ...