RequireJs依赖项始终未定义 [英] RequireJs dependency always undefined

查看:70
本文介绍了RequireJs依赖项始终未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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

这是我的主要文件示例

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!!!");
    }

});

我的数据服务类做了一个非常冗长的jquery get但是作为一个简单的例子我的bootstrapper正在做的旁边没有

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

//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.

推荐答案

你没有从<$ c $返回任何东西c> bootstrapper 或 dataservice 模块,因此模块的public部分是 undefined 。 RequireJS只是执行 define 函数的主体,并隐式返回 undefined 。我认为一个很好的类比是将模块视为一个隐藏所有内部的黑盒子,只允许通过公共接口访问(这是从模块的最终返回返回的任何内容)声明)

You're not returning anything from your bootstrapper or dataservice modules, so the "public" part of the module is undefined. RequireJS simply executes body of the define function and returns undefined implicitly. 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 return statement)

你应该重写一下你的模块,例如:

You should rewrite your modules a bit, for example:

bootstrapper.js

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

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

dataservice.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 docs covers the topic of defining modules in detail, I'd recommend reading as much documentation as possible before diving in.

这篇关于RequireJs依赖项始终未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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