使用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的更多相关文章

  1. 【.NET调用Python脚本】C#调用python requests类库报错 'module' object has no attribute '_getframe' - IronPython 2.7

    最近在开发微信公众号,有一个自定义消息回复的需求 比如用户:麻烦帮我查询一下北京的天气? 系统回复:北京天气,晴,-℃... 这时候需要根据关键字[北京][天气],分词匹配需要执行的操作,然后去调用天 ...

  2. 解决:MySQL 报错:1045 - Access denied for user 'root'@'localhost'(using password YES)

    一.前言 今年疯狂迷上了开源,只要看到好的开源项目,就会不顾一切一股脑扎进去研究,五一期间发现一个很好的关于众筹的开源项目,但不巧,这个项目竟然是 PHP 写的,没学过 PHP,自然对这个开源项目毫无 ...

  3. Python报错module 'scipy.misc' has no attribute 'xxx'

    Python报错module 'scipy.misc' has no attribute 'imresize' 解决办法: 安装Pillow包,命令如下: pip install Pillow 然后重 ...

  4. 解决kylin查询报错:org.apache.kylin.rest.exception.InternalErrorException

    报错信息: -- ::, ERROR [Query 12e9c054-760c---b1f06724c9b6-] service.QueryService: : Exception when exec ...

  5. homestead虚拟机,通过npm下载依赖包和解决运行gulp报错问题 yarn出错问题

    homestead虚拟机,通过npm下载依赖包和解决运行gulp报错问题 yarn出错问题 1. 在虚拟器运行 npm 下载依赖组件时报错: npm ERR! EPROTO: protocol err ...

  6. 完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径。)

    完美解决JavaIO流报错 java.io.FileNotFoundException: F:\ (系统找不到指定的路径.) 错误原因 读出文件的路径需要有被拷贝的文件名,否则无法解析地址 源代码(用 ...

  7. PHP报错: Can't use method return value in write context

    $dp_id = $this->getParam('dpId'); if(!empty($this->getParam('dpId'))) { $this->smarty->a ...

  8. 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 这个问题困扰了很久,网上很多解决方案,很多都不成功 ...

  9. 解决Python模块报错:ModuleNotFoundError: No module name 'StringIO'

    下面是我在学习中遇到的问题,给大家分享一下:   ''' 这里是测试代码 '''# coding = utf-8from selenium import webdriverfrom selenium. ...

随机推荐

  1. struts2特殊符号替换

    今天用struts2做了一个小例子,结果发现个问题 action代码如下 private String table; public String execute(){ setName("pe ...

  2. STL容器的排序

    STL容器的排序,支持随机访问的容器vector,deque,string没有sort成员,可调用std::sort排序:list排序调用自带的list::sort. 下面是std::sort函数,有 ...

  3. 浅谈IFC

    IFC布局规则: 在一个行内格式化上下文中,盒是一个接一个水平放置的,从包含块的顶部开始 这些盒之间的水平margin,border和padding都有效 盒可能以不同的方式竖直对齐:以它们的底部或者 ...

  4. 03Microsoft SQL Server 数据类型

    Microsoft SQL Server 数据类型 数据类型 Number 类型: 数据类型 描述 存储 bit  允许0,1或NULL    tinyint 允许从 0 到 255 的所有数字. 1 ...

  5. oracle 安装准备

    1.选择数据库 (官网查询支持的操作系统) 2.选择系统 (官网查询支持的硬件)(更新补丁) 3.选择硬件 (io性能测试--oracle 大量小文件读写) 4.oracle 升级(和打补丁) 5.o ...

  6. 一个简单的java年龄计算器

    制作一个如下图年龄计算器 根据题目,我做了一个由Calendar类以及年月日各相减得到的年龄,当然正确的方法不止一个,以下为我的源代码和结果截图: package com.Date; import j ...

  7. TWaver3D特效系列之环境映射

    随着TWaver3D的快速发展,越来越多的各种功能都在不断加强,包括性能的极大提升(可以参考这里),3D编辑器的易用性和功能持续增强(欢迎大家申请试用),各种特效的增加,特效是本文的主角. 对于UI技 ...

  8. 前端安全 xss

    整体的 XSS 防范是非常复杂和繁琐的,不仅需要在全部需要转义的位置,对数据进行对应的转义.而且要防止多余和错误的转义,避免正常的用户输入出现乱码. 虽然很难通过技术手段完全避免 XSS,但可以总结以 ...

  9. 运用循环求和( sum operation in python)

    1.for loop example 1: sum of 1+2+...+10 ********** >>> sum=0 >>> for x in [1,2,3,4 ...

  10. centos7安装:license information(license not accepted)

    安装centos7的时候明明已经选择了默认的许可证信息,不知道哪里出错了,安装到最后,就会显示license information(license not accepted)的信息.解决方法如下: ...