简体   繁体   English

RequireJS-在加载其他模块之前先初始化一个模块

[英]RequireJS - initialize one dependend module first, before loading the others

I am doing my first project with requireJS and instantly ran into a problem :) 我正在使用requireJS做我的第一个项目,并立即遇到问题:)

I have created a module "main" which is the basic node where my app should initialize from. 我创建了一个模块“ main”,这是我的应用程序应从其初始化的基本节点。 The APP uses a REST API to communicate with the server, so I created an API module, too. APP使用REST API与服务器进行通信,因此我也创建了一个API模块。

The problem is: I have to initialize the API module first, before any other modules that are dependend of it will be loaded and initialized. 问题是:我必须先初始化API模块,然后再加载和初始化该模块的任何其他依赖模块。

I have found it impossible to do that, because if my main module requires both the API module, plus some module dependend on the API module, the whole thing will failed, because they both get loaded and initialized before my main module gets initialized (and does the basic configuration of the API module). 我发现这样做是不可能的,因为如果我的主模块同时需要API模块和依赖于API模块的模块,那么整个事情都会失败,因为它们都在我的主模块初始化之前(并且执行API模块的基本配置)。

Does someone know a trick how to load one module first, initialize it by passing some parameters and THEN go on and continue with requiring the other modules? 有人知道一个技巧,如何首先加载一个模块,通过传递一些参数对其进行初始化,然后继续并继续要求其他模块?

never tried it with Node.js but this works for me in both Dojo AMD and RequireJS assuming you need to run a module but before running the module B module A has to run i simply do this 从未使用Node.js尝试过,但这在Dojo AMD和RequireJS中都对我有用,假设您需要运行模块,但是在运行模块B之前必须运行模块A,我只是这样做

require(["path/ModuleA","require"],function(modA,req){

    req(["path/ModuleB"],function(modB){
        //do anything with ModuleB
    });

});

i hope this solves your answer, if that's the issue you are referring to, take care 我希望这可以解决您的问题,如果这是您要解决的问题,请当心

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

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