简体   繁体   English

require.js没有加载我的模块

[英]require.js is not loading my module

I am using require.js and a library. 我正在使用require.js和一个库。 Unfortunately it looks like when the library is loaded the result comes back as null. 不幸的是,加载库时,结果返回为空。 Sorry I may be a noob when it comes to using require.js 抱歉,在使用require.js时我可能是菜鸟

postmonger.js postmonger.js

    (function(root, factory){
       if(typeof define === 'function' && define.amd) {
        define('postmonger', [], function(){ return factory(root); });
        }else {
            root.Postmonger = factory(root);
        }
    }(this, function(root){
        root = root || window;
var Postmonger;
if (typeof(exports) !== 'undefined') {
        Postmonger = exports;
    } else {
        Postmonger = {};
    }
Postmonger.noConflict = function(){
        root.Postmonger = previous;
        return this;
    }
        console.log(Postmonger);
        return Postmonger;
    }));

Code that loads the postmonger library "customActivity" 加载后置监视库“ customActivity”的代码

define([
    'js/postmonger'
], function(Postmonger){
    'use strict';
   // console.log(postmonger);
    console.log(Postmonger);
});

My runtime script 我的运行时脚本

<script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/require.js"></script>
    <script type="text/javascript">
        (function() {
            var config = {
                baseUrl: ''
            };

            var dependencies = [
                'customActivity'
            ];

            require(config, dependencies);
        })();
    </script>

When I load the page. 当我加载页面时。 The first console.log returns an object and the second returns Null. 第一个console.log返回一个对象,第二个返回Null。 I must be missing something. 我肯定错过了什么。

Try using postmonger instead of js/postmonger . 尝试使用postmonger而不是js/postmonger

define([
    'postmonger'
], function(Postmonger){
    'use strict';
   // console.log(postmonger);
    console.log(Postmonger);
});

Edit - Why does javscript load but AMD module is null/undefined. 编辑-为什么要加载javscript,但AMD模块为空/未定义。

When requires sees path js/postmonger it tries to download the file at js/postmonger.js which it finds and loads successfully. js/postmonger看到js/postmonger路径时,它尝试将文件下载到js/postmonger.js并成功找到并加载。 But in this file we have a named module with name postmonger with following code 但是在这个文件中,我们有一个命名模块,名称为postmonger ,代码如下

define('postmonger', [], function(){ return factory(root); });

So module with name js/postmonger is never defined and hence you get undefined. 因此,从未定义名称为js/postmonger模块,因此您未定义。 (You should be getting undefined as per my understanding, but you have mentioned it as null) (根据我的理解,您应该变得不确定,但您已将其提到为null)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM