RequireJS模块的依赖性不被评估 [英] RequireJS module's dependencies not being evaluated
问题描述
我有以下(非常简单)模块定义,在CoffeeScript的:
I have the following (extremely simple) module definition, in CoffeeScript:
# backbone/routers/appointments_router.js.coffee
define ["app", "underscore", "backbone"], (App, _, Backbone) ->
console.log(Backbone)
和这里是我的配置和东西:
And here's my config and stuff:
# 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) ->
下面是发生了什么:当我加载我的网页,我得到未定义
在控制台中,即使骨干被列为依赖关系。什么是更令人费解的是,如果我键入骨干
进入控制台,骨干的是的定义。
Here's what's happening: when I load my page, I get undefined
in the console, even though Backbone is listed as a dependency. What's even more puzzling is that if I type Backbone
into the console, Backbone is defined.
怎么可能是骨干最终得到评估,但我的 appointments_router.js.coffee
不知道骨干?
How could it be that Backbone is ultimately getting evaluated, but my appointments_router.js.coffee
doesn't know about Backbone?
推荐答案
下划线或骨干不兼容AMD,所以定义路径是不够的。幸运的是Require.js提供 垫片
- 功能作为一个答案。
Underscore or Backbone aren't AMD compliant, so defining the path isn't enough. Luckily Require.js offers the shim
-functionality as an answer to this.
所以你必须添加这样的事情
So you'll have to add something like this
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模块的依赖性不被评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!