简体   繁体   中英

RequireJS module not loading dependencies

I have an issue with loading module using requireJS. I've got two files:

File1: app1/js/utils/commons/commons.js

define("utils/commons/commons",
    [
        "../../../../app2/MenuModule"
    ], function (MenuModule) {
//MenuModule is undefined here
var app = angular.module('commons', ['MenuModule']);

});

File2: app2/MenuModule.js

define("../../../../app2/MenuModule",
    [
        "../../../../app2/MenuController",
        "../../../../app2/MenuRestProvider"

    ], function (MenuController,
    MenuRestProvider) {

var app = angular.module('MenuModule', []);

app.factory('MenuRestProvider', MenuRestProvider);
app.controller('MenuController', MenuController);

return app;

});

The point is: File1 loads File2, and 'define' function from File2 is run. Dependencies from File2 are not fetched, and function passed to 'define' function is not evaluated. Can you see what causes this problem?

I think you are confusing paths with module IDs. It's easy to confuse one with the other, since paths ultimately lead into defining the moduleID based on how it's resloved, but when you put relative symbols in a module reference, don't think about 'going up a path', but rather 'moving up the module hierarchy.

All this means is that you need to share your paths config with us so that we can see how the referenced module IDs could be translated into the paths for loading.

Also agree: putting the moduleID directly in the define() call has code smell. You should be letting the amd loader define the moduleIDs for you (by making them anonymous modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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