简体   繁体   中英

Configuration of requirejs and shim

I have this kind of config:

require.config({

    paths: {
        jquery: '../libs/jquery/dist/jquery',
        uicore: '../libs/jquery-ui/ui/jquery.ui.core',
        uiwidget: '../libs/jquery-ui/ui/jquery.ui.widget',
        uidialog: '../libs/jquery-ui/ui/jquery.ui.dialog'
    },

    shim: {
        uicore: ['jquery'],
        uiwidget: ['uicore'],
        uidialog: ['uiwidget']
    },

    urlArgs: "bust=" +  (new Date()).getTime()

});

require(['uidialog'], function (d) {
    console.log(d); // undefined;
});

So, what is wrong with my code? Why does d is undefined? Every file has been downloaded, version of jquery is 2.1.

Please, help :(

Quote from Requirejs-getStarted :

Inside of main.js, you can use require() to load any other scripts you need to run. This ensures a single entry point, since the data-main script you specify is loaded asynchronously.

require(["helper/util"], function(util) { //This function is called when scripts/helper/util.js is loaded. //If util.js calls define(), then this function is not fired until //util's dependencies have loaded, and the util argument will hold //the module value for "helper/util". });

I suggest, for instance:

require.config({
    baseUrl: 'js/lib',
    paths: {
        jquery: 'jquery-1.9.0'
    }
});

Then:

require(['jquery'], function( jq ) {
    console.log( jq ) // OK
});

Probably you have wrong path in your configuration..

edit

Have you try:

define(['jquery'], function (jq) {
    return jq.noConflict( true );
});

The answer is simple: is to export $ . Like this:

'ui.core': {
    deps: ['jquery'],
    exports: '$'
},
'ui.widget': {
    deps: ['ui.core'],
    exports: '$'
},
'ui.button': {
    deps: ['ui.core'],
    exports: '$'
},
'ui.dialog': {
    deps: ['ui.widget', 'ui.button'],
    exports: '$'
}

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