javascript RequireJs 依赖项总是未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18069305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:38:25  来源:igfitidea点击:

RequireJs dependency always undefined

javascriptrequirejs

提问by Infolord101

I am using require Js and am getting rather confused as to why my modules are loading but dependencies are always undefined and even upon using require.defined() function to check if my module has been loaded it indeed has been but when I use the require([deps], function(deps){}); all of my dependencies are undefined (except for jquery, underscore and knockout)

我正在使用 require Js 并且对于为什么我的模块正在加载但依赖项始终未定义甚至在使用 require.defined() 函数来检查我的模块是否已加载它确实已经加载了但当我使用 require 时感到困惑([deps], 函数(deps){}); 我的所有依赖项都未定义(jquery、下划线和淘汰赛除外)

my file structure is as follows my file structure is as follows

我的文件结构如下 我的文件结构如下

scripts 
    |
    |        
    main.js
    |
    |_________BusinessScripts
    |           |
    |           |
    jquery.js   |
    |           |
    |           boostrapper.js-undefined
    ko.js       |
                |
                dataservice.js-undefined

here is an example of my main file that kicks off require

这是我的主文件的一个例子,它启动了要求

requirejs.config(
    {
        paths: {
            'jquery': 'jquery-1.7.1',
            'underscore': 'underscore',
            'ko': 'knockout-2.2.1'
        },
        shim: {
            underscore: { exports: '_' },
        }
    }
);


requirejs(['require', 'BusinessScripts/bootstrapper', 'BusinessScripts/dataservice'],
function (require,bootstrapper, dataservice) {

    var def = require.defined('BusinessScripts/bootstrapper'); //this returns true

    if (dataservice !== undefined) { // always undefined

        alert("Loaded properly");
    } else {
        alert("not loaded!!!");
    }

    if (bootstrapper !== undefined) { // always undefined


        alert("Loaded properly");
    } else {
        alert("not loaded!!!");
    }

});

my data service class does a quite lengthy jquery get but as a simple example my bootstrapper is doing next to nothing

我的数据服务类做了一个相当长的 jquery get 但作为一个简单的例子,我的引导程序几乎什么都不做

//bootstrapper
define(function () { var one = 1; 
var run = function () {
}
});

//dataservice

define(['jquery', 'underscore'],function ($, _) {
    $.ajax({lengthy work...});

});

as I said both modules are being loaded but are never resolving

正如我所说的,两个模块都在加载但从未解决

any help would be much appreciated.

任何帮助将非常感激。

回答by kryger

You're not returning anything from your bootstrapperor dataservicemodules, so the "public" part of the module is undefined. RequireJS simply executes body of the definefunction and returns undefinedimplicitly. I think a good analogy is to think of a module as a black box with all internals hidden, only allowing access through the public interface (which is whatever you return from module's final returnstatement)

你没有从你的bootstrapperdataservice模块返回任何东西,所以模块的“公共”部分是undefined. RequireJS 只是执行define函数体并undefined隐式返回。我认为一个很好的类比是将模块视为隐藏所有内部结构的黑匣子,仅允许通过公共接口访问(这是您从模块的最终return语句返回的任何内容)

You should rewrite your modules a bit, for example:

您应该稍微重写一下模块,例如:

bootstrapper.js

引导程序.js

define(function () {
  var one = 1;
  var run = function () {
    console.log('Running...');
  }

  return {
    publicRun: run,
    publicOne: one
  }
});

dataservice.js

数据服务.js

define(['jquery', 'underscore'],function ($, _) {
  $.ajax({
    // lengthy work...
  });

  return 'Lengthy work started!';
});

Then you could use these modules like this:

然后你可以像这样使用这些模块:

requirejs(['require', 'BusinessScripts/bootstrapper', 'BusinessScripts/dataservice'],
  function (require, bootstrapper, dataservice) {
    // prints "1"
    console.log(dataservice.publicOne);

    // prints "Running..."
    dataservice.publicRun();

    // prints "Lengthy work started!"
    console.log(bootstrapper);
  });


The official docscovers the topic of defining modules in detail, I'd recommend reading as much documentation as possible before diving in.

官方的文档详细地介绍定义模块的话题,我建议你阅读如在跳水之前多文档成为可能。

回答by daCoda

This answer doesn't fully 'match' the question's (specific) circumstance, but it may help someone for the (general) question - Why is my dependency undefined?

这个答案并不完全“匹配”问题的(特定)情况,但它可能有助于解决(一般)问题 - 为什么我的依赖项未定义?

Tip: Double check that your dependency isn't dependent on the thing you're trying to get dependent on...

提示:仔细检查你的依赖是否依赖于你试图依赖的东西......

i.e. If you want A to depend on B, double check that B isn't depending on A.

即如果您希望 A 依赖于 B,请仔细检查 B 是否不依赖于 A。