RequireJS模块的依赖没有被评价(RequireJS module's depende

2019-09-21 05:04发布

我有以下(非常简单)模块定义,在CoffeeScript的:

# backbone/routers/appointments_router.js.coffee
define ["app", "underscore", "backbone"], (App, _, Backbone) ->
  console.log(Backbone)

下面是我的配置和东西:

# application.js.coffee
requirejs.config
  paths:
    underscore: "lodash.min"
    backbone: "backbone"
    appointmentsRouter: "backbone/routers/appointments_router"
    "backbone-relational": "backbone-relational"

requirejs ["app", "underscore", "backbone", "appointmentsRouter"], (App, _, Backbone, AppointmentsRouter) ->

这里发生的事情:当我加载我的网页,我得到undefined的控制台,即使骨干被列为依赖关系。 什么是更令人费解的是,如果我输入Backbone到控制台,骨干定义

这怎么可能是骨干最终得到评估,但我appointments_router.js.coffee不知道骨干?

Answer 1:

下划线或骨干不兼容AMD,所以定义路径是不够的。 幸运的是Require.js提供shim -Functionality作为一个答案。

所以你必须添加这样的事情

requirejs.config( // shouldn't this be just require?
  paths: ..., // don't change these
  shim: {
    "underscore": {
      exports: "_" // define the export
    },
    "backbone": {
      deps: ["underscore"], // define dependencies for backbone
      exports: "Backbone"
    }
  }
);

希望这可以帮助!



文章来源: RequireJS module's dependencies not being evaluated