RequireJS在依赖项加载/解析之前调用回调 [英] RequireJS calling callbacks before dependencies loaded/resolved

查看:78
本文介绍了RequireJS在依赖项加载/解析之前调用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了RequireJS的问题,其中我的main.js脚本引用了一个依赖项,当运行请求此依赖项的main.js中的回调时,该依赖项已加载但未解析。

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.

我的目录结构是:

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

index.htm:

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?

推荐答案

回答我自己的愚蠢问题:

To answer my own stupid question:

您正在使用 require 来定义模块,您应该使用 define

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

Facepalm。

这篇关于RequireJS在依赖项加载/解析之前调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆