动态需要RequireJS,越来越"模块名称尚未上下文和QUOT没有加载;错误? [英] Dynamic require in RequireJS, getting "Module name has not been loaded yet for context" error?

查看:523
本文介绍了动态需要RequireJS,越来越"模块名称尚未上下文和QUOT没有加载;错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来定义模块动态加载其他的RequireJS模块?如果是,则优化器(r.js)如何理解如何/当一个模块被列入?

例如,让 dynModules 定义名称/路径对一个模块:

 定义([],函数(){
    返回['moduleA','moduleB']; //模块名称的数组
});

另一个模块是要基于在阵列上动态加载的模块。这将无法正常工作

 定义(['dyn_modules'],功能(dynModules){
    对(在dynModules名){
        VAR模块=要求(路径); //调用RequireJS要求
    }    // ...
});

...给我:


  

未捕获的错误:模块名称moduleA尚未加载
  背景:_。使用要求([])
  <一href=\"http://requirejs.org/docs/errors.html#notloaded\">http://requirejs.org/docs/errors.html#notloaded


我的解决错误,但它不是动了:

 定义(['dyn_modules','moduleA','moduleB'],功能(dynModules){
    对(在dynModules名){
        VAR模块=要求(路径); //调用RequireJS要求
    }    // ...
});


解决方案

的限制涉及简化的语法CommonJS的主场迎战正常回调语法:

加载一个模块本质上是一个异步过程中因下载它的未知的时机。然而,在服务器端CommonJS的规格仿真RequireJS试图给你一个简单的语法。当你做这样的事情:

  VAR foomodule =要求('富');
//做一些fooModule

这是怎么回事幕后是RequireJS是看你的函数code的身体和解析出来,你需要'富'和你的函数执行前加载它。然而,当一个变量或不是一个简单的字符串,任何其他比如你的例子...

  VAR模块=要求(路径); //调用RequireJS要求

...然后要求无法解析了这一点,并自动将其转换。解决的办法是要转换为回调语法;

  VAR MODULENAME ='富';
需要([MODULENAME]功能(fooModule){
    //做一些fooModule
})

鉴于上述情况,这里是你的第二个示例使用标准语法的一种可能重写:

 定义(['dyn_modules'],功能(dynModules){
    需要(dynModules,函数(){
        //由于您使用参数不知道你有多少模块回调获得
        对于(VAR I = 0; I&LT;与arguments.length;我++){
            VAR MyModule的参数= [I]
            //做MyModule的东西...
        }
    });});

编辑:从你自己的答案,我看你使用下划线/ lodash,因此,使用 _值 _对象可以简化通过参数数组的循环如上。

Is there a way to define a module that "dynamically" load other modules in RequireJS? If yes, how the optimizer (r.js) understands how/when a module has to be included?

For example, let dynModules a module which defines name/path pairs:

define([], function () {
    return ['moduleA', 'moduleB']; // Array of module names
});

Another module is going to load modules dynamically, based on the array. This will not work:

define(['dyn_modules'], function (dynModules) {
    for(name in dynModules) {   
        var module = require(path); // Call RequireJS require
    }

    // ...
});

... gives me:

Uncaught Error: Module name "moduleA" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded

I can solve the error, but it's not "dynamic" anymore:

define(['dyn_modules', 'moduleA', 'moduleB'], function (dynModules) {
    for(name in dynModules) {   
        var module = require(path); // Call RequireJS require
    }

    // ...
});

解决方案

The limitation relates to the simplified CommonJS syntax vs. the normal callback syntax:

Loading a module is inherently an asynchronous process due to the unknown timing of downloading it. However, RequireJS in emulation of the server-side CommonJS spec tries to give you a simplified syntax. When you do something like this:

var foomodule = require('foo');
// do something with fooModule

What's happening behind the scenes is that RequireJS is looking at the body of your function code and parsing out that you need 'foo' and loading it prior to your function execution. However, when a variable or anything other than a simple string, such as your example...

var module = require(path); // Call RequireJS require

...then Require is unable to parse this out and automatically convert it. The solution is to convert to the callback syntax;

var moduleName = 'foo';
require([moduleName], function(fooModule){
    // do something with fooModule
})

Given the above, here is one possible rewrite of your 2nd example to use the standard syntax:

define(['dyn_modules'], function (dynModules) {
    require(dynModules, function(){
        // use arguments since you don't know how many modules you're getting in the callback
        for (var i = 0; i < arguments.length; i++){
            var mymodule = arguments[i];
            // do something with mymodule...
        }
    });

});

EDIT: From your own answer, I see you're using underscore/lodash, so using _.values and _.object can simplify the looping through arguments array as above.

这篇关于动态需要RequireJS,越来越&QUOT;模块名称尚未上下文和QUOT没有加载;错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆