热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

未定义的对象通过Requirejs传递-UndefinedobjectbeingpassedviaRequirejs

ImusingRequirejstoloadtheJavaScriptinourwebapp.TheissuesisthatImgettinganundefin

I'm using Requirejs to load the Javascript in our web app. The issues is that I'm getting an undefined object being passed to a module which, when used in other modules, is instantiated perfectly fine.

我正在使用Requirejs在我们的网络应用程序中加载Javascript。问题是我得到一个未定义的对象被传递给一个模块,当在其他模块中使用时,它被完美地实例化。

OK, here's the setup. My main.js file which requirejs runs on startup:

好的,这是设置。我的main.js文件,其中requirejs在启动时运行:

require.config({
    baseUrl: "/scripts",
    paths: {
        demographics: "Demographics/demographics",
        complaints: "Complaints/complaints",
    }
});

require(["templates", "demographics", "complaints", "crossDomain"], function (templates, demographics, complaints) {
    "use strict";

    console.log("0");
    console.log(demographics === undefined);

    demographics.View.display();
});

A lot of the config has been stripped to just the core files in this problem.

很多配置都被剥离到这个问题的核心文件中。

Here's Demographics.js:

这是Demographics.js:

define(["ko", "templates", "complaints", "globals", "underscore"], function (ko, templates, complaints, globals) {

    // Stuff removed.
    return {
        View: view
    };
});

and Complaints.js

和Complaints.js

define([
    "demographics",
    "ko",
    "templates",
    "complaints",
    "visualeffects",
    "globals",
    "webservice",
    "underscore",
    "typewatcher",
    "imagesloaded"],
    function (demographics, ko, templates, complaints, visualeffects, globals, webservice) {
        "use strict";


        console.log("1");
        console.log(demographics === undefined);
    return {
        View: view
    };
});

The problem is this - in Complaints.js the demographics parameter passed via the define config is undefined. The console log out tells me that "demographics === undefined" is true.

问题是这个 - 在Complaints.js中,通过define config传递的demographics参数是未定义的。控制台注销告诉我“人口统计数据=== undefined”是真的。

However, when the main.js file executes, the demographics parameter passed to it is not undefined, it is, as expected, an instantiated object.

但是,当main.js文件执行时,传递给它的demographics参数不是未定义的,正如预期的那样,它是一个实例化的对象。

Now I'm stuck since I can't see why in complaints.js that demographics variable is undefined. Can anyone spot what I'm missing please?

现在我陷入困境,因为我无法在投诉中看到为什么人口统计变量未定义。有人能发现我想念的东西吗?

6 个解决方案

#1


57  

You have a circular dependency. The demographics module depends on complaints and complaints depends on demographics. As per the documentation:

你有循环依赖。人口统计模块取决于投诉和投诉取决于人口统计。根据文件:

If you define a circular dependency (a needs b and b needs a), then in this case when b's module function is called, it will get an undefined value for a.

如果定义循环依赖(需要b和b需要a),那么在这种情况下调用b的模块函数时,它将获得a的未定义值。

The solution, if you can't remove the circular dependency, is to asynchronously require one of the two modules within the other on demand (say when the view is instantiated instead of when the module that defines the view is executed). Again, the docs cover this topic fairly well.

如果无法删除循环依赖关系,则解决方案是根据需要异步地要求另一个模块中的一个(例如,在实例化视图时,而不是在执行定义视图的模块时)。同样,文档很好地涵盖了这个主题。

#2


27  

Another case is when you accidentally type require instead of define when defining a module, took me some time to notice this.

另一种情况是,当您在定义模块时意外键入require而不是define时,花了一些时间注意这一点。

#3


11  

I had a similar problem. In my case, when defining a module, I'd written:

我有类似的问题。就我而言,在定义模块时,我写道:

define('some_dependency', ...

instead of

代替

define(['some_dependency'], ...

#4


4  

Another possible reason is implementing the module's interface (AMD, CommonJS), but forgetting to return anything. I just did that.

另一个可能的原因是实现模块的接口(AMD,CommonJS),但忘记返回任何内容。我就这样做了。

#5


0  

One other possible reason that may look obvious in hindsight is an error in your module's code. In my case, I was trying to get an attribute from an undefined variable. The error is logged in the console, but for some reason I was not seeing it/mistaking it for the undefined module error.

事后看来可能显而易见的另一个可能原因是模块代码中的错误。就我而言,我试图从未定义的变量中获取属性。该错误记录在控制台中,但出于某种原因,我没有看到/错误地将其视为未定义的模块错误。

#6


-1  

I just encountered another reason:

我刚刚遇到另一个原因:

define(function () {
    return {};
}()); // <-- notice the '()' typo.

This "typo" causes no JS errors for this one and can make it confusing to figure out especially in a complicated application with many potential circular dependancies.

这个“拼写错误”不会导致这个错误的JS错误,并且可能会让人困惑,尤其是在具有许多潜在循环依赖性的复杂应用程序中。

The reason, of course, is the "typo" is valid JS that simply calls the function you define thereby passing its result to define() rather then the function as intended.

当然,原因是“拼写错误”是有效的JS,只是简单地调用你定义的函数,从而将结果传递给define()而不是按预期传递函数。


推荐阅读
author-avatar
252575936_8ea84a
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有