Requirejs Require.JS shim config全局范围?

Requirejs Require.JS shim config全局范围?,requirejs,shim,Requirejs,Shim,我对requireJS有以下设置 requirejs.config({ paths: { 'resources' : '/Scripts/resources' }, shim: { 'resources': { exports: 'LocalizedStrings' } } }); 我的resources.JS如下所示: Loca

我对requireJS有以下设置

requirejs.config({
     paths: {
            'resources' : '/Scripts/resources'
     },
     shim: {
             'resources': {
                           exports: 'LocalizedStrings'
           }
     }
});
我的resources.JS如下所示:

LocalizedStrings = {
                    title: "Demo",
                    save: "Save"
}
现在,当我将资源作为依赖项加载到main.JS文件中时,我可以访问LocalizedStrings,并且它可以正常工作

//main.js
define(function(require){
    var LocalizedStrings = require('resources');
    console.log(LocalizedStrings); //works as expected
});
然而,在其他模块上,我并不需要将资源作为依赖项加载以访问“LocalizedStrings”

//othermodule.js
define(function(require){
    console.log(LocalizedStrings); //works as expected even though resources dependency is not loaded
});

这里我不明白的是,如果我使用shim加载一个JS文件并加载一次,它是否会变得全局可用,并且我不必在其他模块中再次加载相同的依赖项。

主干线和下划线都会修改全局范围,因此如果浏览器已经运行了它们的代码,那么全局范围就会存在

如果在RequireJS中作为填隙片加载,或者直接在src中包含脚本标记,就会发生这种情况

一旦全局变量存在,它们就存在了(除非我想明确地
delete
d)

,并查看值(对于某些库)设置为全局值

该示例的目的是显示globals的值仅在
require()
调用中得到保证。
(如果使用AMD加载程序,而不是简单地在
脚本
标记中导入库)。全局值将在未来某个不确定的时间存在

源代码
require.config({
垫片:{
下划线:{
导出:'''
},
主干:{
deps:[“下划线”,“jquery”],
出口:“骨干”
}
},
路径:{
jquery:“http://code.jquery.com/jquery-1.9.1",
主干:“http://backbonejs.org/backbone",
下划线:“http://underscorejs.org/underscore"
}
});
函数附件版本(msg,$,_,主干){
var pre=document.getElementById(“内容”);
var s=”“+msg+”;
s+=“jquery=”;
试试{s+=$.fn.jquery;}catch(e){s+=e.msg;}
s+=“
”; s+=“quo;”; 试试{s+=\.VERSION;}catch(e){s+=e.msg;} s+=“
”; s+=“主干=”; 试试{s+=Backbone.VERSION;}catch(e){s+=e.msg;} pre.innerHTML+=s; } appendVersions(“需要之前(未定义)”、窗口[“$”]、窗口[“”]、窗口[“主干”); require([“jquery”,“下划线”,“主干线]),function($,_,主干线){ appendVersions(“Inside Require(应该*始终*正常,除非脚本不在那里)”,$,\ux,Backbone); }); appendVersions(“在require之后(可能是未定义的,因为require可能还没有加载脚本)”、window[“$”]、window[“”]、window[“主干”); setTimeout(函数(){ appendVersions(“超时后(应该可以,但取决于脚本加载的速度。尝试更改超时持续时间)”、窗口[“$”]、窗口[“”]、窗口[“主干”); }, 2000);
样本输出

通过AMD加载的模块(如RequireJS)通常不会成为全局模块。但是您没有在
resources.js
中使用“real”模块。您在
resources.js
中的代码改变了全局范围,因此在您的例子中,您正在改变全局范围。感谢您回答这个问题。我真的不想要模块中的resources.js。我只是想了解垫片配置是如何工作的。现在,假设我在requireJS中填充主干线和下划线,并在main.js中将它们作为依赖项加载一次,那么requireJS是否使主干线和下划线在全局范围内可用,而无需我在其他模块中加载它们。主干线和下划线都会修改全局范围,因此如果浏览器已经运行了它们的代码,然后,全局将存在。如果在RequireJS中作为填隙片加载,或者直接在src中包含脚本标记,就会发生这种情况。一旦全局存在,它们就存在了(除非我想明确地
delete
d)。
require.config({
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ["underscore", "jquery"],
            exports: "Backbone"
        }
    },
    paths: {
        jquery: "http://code.jquery.com/jquery-1.9.1",
        backbone: "http://backbonejs.org/backbone",
        underscore: "http://underscorejs.org/underscore"
    }
});

function appendVersions(msg, $, _, Backbone) {
    var pre = document.getElementById("content");
    var s = "<h2>" + msg + "</h2>";
    s += "jquery=";
    try { s += $.fn.jquery; } catch (e) { s += e.msg; }
    s += "<br>";
    s += "_=";
    try { s += _.VERSION; } catch (e) {  s += e.msg; }
    s += "<br>";
    s += "Backbone=";
    try { s += Backbone.VERSION; } catch (e) {  s += e.msg; }
    pre.innerHTML += s;
}

appendVersions("Before require (will be undefined)", window["$"], window["_"], window["Backbone"]);

require(["jquery", "underscore", "backbone"], function ($, _, Backbone) {
    appendVersions("Inside Require (should *always* be ok, unless the scripts aren't there)", $, _, Backbone);
});

appendVersions("After require (Probably be undefined, as the require probably won't have loaded the scripts yet)", window["$"], window["_"], window["Backbone"]);

setTimeout(function () {
    appendVersions("After Timeout (should be ok, but depends on how quickly the scripts load. Try changing the timeout duration)", window["$"], window["_"], window["Backbone"]);
}, 2000);