简体   繁体   中英

Requirejs what is the use of deps in shim configuration

i read the requirejs document from here api

requirejs.config({
    baseUrl: 'js/',
    paths: {
        app: 'app/js',
        jquery: '',
    },
    shim: {
        jquery: {
            exports: '$',
        },
        knockout: {
            deps: ['jquery', 'bootstrap', 'underscore'],
            exports: 'KnockOut'
        },
        underscore: {
            exports: '_'
        },
        a:{
            deps: ['x','b'],
            exports: 'a'
        }
    }
});

but i am not getting deps part of shim . why should i use deps and does that mean these files( x and b ) will auto load if i load only a or what else

    require(['a'], function(a){
    });

I tried but couldn't getting auto load x and b . please can any one explain with example why and when should we use deps. thanks.

RequireJS loads modules that conform to the Asynchronous Module Definition (AMD). An AMD module must call a function named define provided by the AMD loader. The basic call is like this:

define(["a", "b", "c", ...], function (a, b, c, ...) {
});

The first argument is a list of the modules that the module being defined depends on. The 2nd argument is a factory function that builds the module being defined. RequireJS passes to it the modules that were listed in the dependencies. There are other ways to call define but the example above is typical.

That's all nice and well but what happens if you want to load a library produced by a third party but that is not an AMD module? The shim option allows telling RequireJS how to load those modules. For an AMD module, you specify dependencies in the array you pass to define , but for a non-AMD module you define a shim that contains a list of dependencies in the deps option. It contains the name of modules that must be loaded before the shimmed module is loaded. In your example, x and b must be loaded before a .

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