requirejs插件在新的上下文中加载模块

问题描述

| 我想让一个模块在页面上多次加载,每个实例具有唯一的上下文。有两种正常的方法可以做到这一点。 模块可以返回构造函数 我可以显式运行require方法,提供一个上下文ID。 在我的情况下,这些方法都可以使用,但是我想要第三个选择。 :) 我想要一个requirejs插件,它将在一个新的上下文中返回一个模块给我。即
require([\"new!some/module\"],function(SomeModule) {
    // SomeModule,here,is in its own context.  If i were to run this
    // method call again,SomeModule would be in a new context.
});
我已经开始研究构建插件来实现此目的...
load: function (name,req,load,config) {
    var index = get_unique_index();
    req({context:index},[name],function(value) {
        if(!config.isBuild){
            load(value);
        }
    });
}
但是{context:index}字典在这里不起作用...想法?     

解决方法

        与其使用传递给本地的req来加载,不如使用全局require函数,即所谓的require()。传递给本地的已绑定到上下文。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...