RequireJS 模块加载

3
我可以帮助您翻译这段内容。这是关于使用requireJS仅在需要时加载模块的概念方面的帮助请求。
这是我的main.js文件。
require(['jquery', 'path/somemodule'],
function($, somemodule) {
$(document).ready(function() {
    somemodule.init()
})

})

并且在 somemodule.js 中

 define(['jquery', 'path/someothermodule'], function ($, someothermodule) {
 "use strict";
var somemodule;

somemodule = {
init: function () {
    someothermodule.init()
}
}
return somemodule;
)}

现在,somemodule.js和someothermodule.js被加载到所有页面上。我该如何只在需要时加载它们?


它在所有页面上加载,因为...目前所有页面都需要它。main.js需要path/somemodule,而somemodule.js需要path/someothermodule。因此,每次包含main.js时,它也会包含somemodule.jssomeothermodule.js - Kevin B
只有在需要该模块时才使用require,因此除非您在那里加载它,否则它不需要出现在所有页面上。 - webduvet
2个回答

7
当您使用标准的define()语法从module1中要求module2时,只有在module2完全加载/运行之后,module1才会加载/运行。代码如下:
// inside module1
define(['module2'], function(mod2) {
   // we don't get here until AFTER module2 has already been loaded
});

一种替代lazy-load模块2的方法如下:

一种替代lazy-load模块2的方法如下:

// inside module1
define([], function() {
   require(['module2'], function(mod2) {
       // we don't get here until AFTER module2 has already been loaded
   });
   // but we DO get here without having loaded module2
});

现在你需要仔细编写程序,以确保不会遇到异步问题。在您的情况下,您可以修改main.js文件。
require(['jquery'],
function($) {
    // jquery is loaded, but somemodule has not

    if(thisPageNeedsSomeModule) {
        require(['path/somemodule'],
        function(somemodule) {
            // now somemodule has loaded
            $(document).ready(function() {
                somemodule.init()
            })
        });
    }
})

我现在明白了。你帮了我很多。 - Newcoma

0

你的 main.js 文件将会加载任何提供给它的文件路径,只要你的应用程序的其他元素指定它们为依赖项。请看我的示例 main.js 文件:

require.config({

    paths: {
        'app': 'app',
        'underscore':'bower_components/underscore/underscore-min',
        'backbone':'bower_components/backbone/backbone-min',
        'marionette':'bower_components/backbone.marionette/lib/backbone.marionette.min',
        'jquery': 'bower_components/jquery/jquery.min',
        'tpl':'bower_components/requirejs-tpl/tpl',
        'bootstrap':'bower_components/bootstrap/dist/js/bootstrap.min',
        'leaflet':'bower_components/leaflet/leaflet',
        'leaflet.markercluster':'bower_components/leaflet/leaflet.markercluster',
    },
    shim: {
        'underscore': {
            exports: '_'
        }, 
        'leaflet': {
            exports: 'L'
        }, 
        'leaflet.markercluster': {
            deps: ['leaflet']
        },
        'backbone': {
            deps: ['underscore']
        },
        'marionette': {
            deps: ['backbone']
        },
        'jquery': {
            exports: '$'
        },  
        'bootstrap': {
            deps: ['jquery']
        },
        'app': {
            deps: ['jquery', 'leaflet','bootstrap', 'leaflet.markercluster', 'marionette', 'tpl']
        },
        'app.elem': {
            deps:['app']
        },
        'app.api': {
            deps:['app']
        }
    }
})

require(['app','app.api','app.elem'], function() {
    App.start();
})

我的初始应用程序文件:

define(['router', 'collections/moments'], function(router, momentCollection) {

    // Boot the app!

    App = new Marionette.Application();

    App.LocResolve = false; // Have we resolved the user's location?
    App.Locating = true; // Are we actively tracking the user's location?

    App.FileReader = window.FileReader ? new FileReader : null;

    App.Position = null; // Instant access to Lat & Lng of user.

    App.MomentsRaw = null; // Keep cached copy of returned data for comparison.

    App.Moments = new momentCollection; // Current collection of moments.
    App.Markers = new L.MarkerClusterGroup(); // Create Marker Cluster Group

    App.View = null; // Current view.

    // Marionette Regions

    App.addRegions({
        header: '#header',
        map: '#map',
        list: '#list',
        modal: '#modal',
    });

    return App
})

我注意到你没有传递配置对象 - 这是有意为之吗?如果你使用 R.js,构建优化器,它会自动为你删除未使用的供应商文件。

简而言之,在 require.js 配置中设置供应商文件的路径,然后通过 define() 调用它们,每当你需要特定的资产时。这将确保只使用你需要的文件。希望这可以帮助你!


请问您能否解释一下,如果我只想在特定元素存在时加载 somemodule.js 并使用 somemodule.init(),这将有助于我更好地理解。 - Newcoma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接