未调用RequireJS数据主模块回调 [英] RequireJS data-main module callback not invoked
问题描述
我的应用程序:
define(function bringTheAction() { console.log('Loaded!'); });
使用data-main进行了优化和加载:
is optimised and loaded using data-main:
<script data-main="src/app" src="lib/require.js"></script>
并且RequireJS确实加载了文件:
and RequireJS does load the file:
<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="app" src="src/app.js"></script>
但是,永远不会调用bringTheAction
回调,而我的应用只是闲着.
however, the bringTheAction
callback is never invoked and my app just sits idly.
我可以通过在控制台中执行require(['src/app']);
或在未构建的代码中将define
更改为require
来开始事情,但是:
I can kick things off by doing require(['src/app']);
in the console or by changing define
to require
in the unbuilt code, but:
-
根据我的理解,它应该按原样工作;和
- 我需要使用
define
,以便可以使用 Squire 来模拟该模块.
- from my understanding, it should work as it is; and
- I need to use
define
so the module can be mocked with Squire.
在开发中,未构建的代码以不同的方式加载:
In development, the unbuilt code is loaded differently:
<script src="lib/require.js"></script>
<script src="src/require.config.js"></script>
<script>require({ /* config applicable to dev */ }, ['src/app'], function () {});</script>
它就像梦一样.
推荐答案
好吧,经过一番困惑,我有点明白了.
Okay, I sort of figured this out, after much confusion.
我注意到了data-requiremodule="app"
属性(在RequireJS注入的脚本标签上),并假设我需要的是data-requiremodule="src/app"
,以便RequireJS在文件加载后就知道要require
做什么.
I noticed the attribute data-requiremodule="app"
(on the script tag injected by RequireJS) and assume what I need is data-requiremodule="src/app"
, so that RequireJS knows what to require
once the file loads.
我不确定为什么要在此属性上省略src/
,因为我的配置已调整为可以从src/
上的目录进行操作(因此,我通过在src/
,lib/
之前加上前缀来引用模块) .但是,作为修复,我在构建配置中添加了一个别名,以满足RequireJS:
I'm not sure why it is omitting src/
on this attribute, since my config is tuned to operate from the directory above src/
(hence I reference modules by prefixing src/
, lib/
, etc). However, as a fix, I added an alias to the build config, to satisfy RequireJS:
paths: { 'app': 'src/app'; }
现在,该属性实际上指向要加载的模块.
Now that attribute actually points to a module to load.
这篇关于未调用RequireJS数据主模块回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!