本文介绍了require.js填充程序,导出myOwnGlobal名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定在shim config上使用"exports",按照requireJS API上的示例,我可以使用Backbone(大写字母B)将其导出到全局范围.这意味着它将是一个窗口对象属性.但是我意识到我被迫使用该名称,并且无法通过其他引用名称将其导出,即:"MyGlobalBackbone"

I'm not sure about the use of "exports" on shim config, following the example on the requireJS API, I can use Backbone (B in capital letter) to export it to a global scope.This means that it will be a window object property.But I realized that I'm forced to use that name, and I can't export it by other reference name, ie: "MyGlobalBackbone"

require.config({
  paths: {
    backboneAlias:'backbone'
  },
  shim : {
    backboneAlias : {
      deps : [ 'underscore', 'jquery-1.9.1' ],
      exports  : 'MyGlobalBackbone'
    }
  }
});

require(['backboneAlias'],function(backboneAsAliasDependency){
  console.log(backboneAsAliasDependency);//Loaded Ok
  console.log(MyGlobalBackbone); //Uncaught ReferenceError: MyGlobalBackbone is not     defined
});

仅当我使用"Backbone"而不是"MyGlobalBackbone"时,此代码才有效...

This code only works if I use "Backbone" instead of "MyGlobalBackbone"...

推荐答案

实际上,您可以用另一种方法解决:填充不会将变量导出到全局范围,而是导入范围.名称("Backbone")是由Backbone的作者设置的,这是您在shim config元素中向RequireJS解释的部分.

Actually you got it the other way around: shimming doesn't export a variable to global scope, it imports it FROM the global scope. The name ("Backbone") was set by Backbone's author, and this is the part you're explaining to RequireJS in shim config element.

这篇关于require.js填充程序,导出myOwnGlobal名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 05:29
查看更多