RequireJS calling callbacks before dependencies loaded/resolved

我的未来我决定 提交于 2019-12-23 13:09:00

问题


I'm having an issue with RequireJS where my main.js script has a reference to a dependency, which is loaded but not resolved when the callback in main.js requesting this dependency is run.

My directory structure is:

index.htm
scripts/
    require.js
    main.js
    feeds/
        feed.js

index.htm:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Blah</title>
        <script data-main="scripts/main" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>Hello, World!</h1>
    </body>
</html>

main.js:

require(["feeds/feed"], function(feed) {
    console.log("A");
    require.ready(function() {
        console.log("B");
        console.log(feed.val);
    });
});

feed.js:

console.log("C");
require(function() {
    console.log("D");
    return {
        val: "E"
    }
})

And the console output, suggesting that the dependency files are being loaded, but not resolved correctly:

C
A
B
Uncaught TypeError: Cannot read property 'val' of null

I must be missing something really obvious here, but whatever documentation I read up on, the problem doesn't seem to be revealing itself. Any ideas?


回答1:


To answer my own stupid question:

You're using require to define your modules, where you should be using define.

Facepalm.



来源:https://stackoverflow.com/questions/5590618/requirejs-calling-callbacks-before-dependencies-loaded-resolved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!