Before the Requirejs framework, if we wanted to use the jquery framework, it would be loaded in the HTML page via the <script> tag, when the jquery framework generated global variables such as global variables $ and jquery. If the REQUIREJS framework is referenced in the project and the jquery is loaded in a modular way, jquery will no longer add global variables $ and jquery. Now that's the problem, although the jquery framework has started to support AMD specifications, many of jquery's plugins still don't support AMD and still need to use the global variable $ as before. jquery plugins are mostly structured as follows:
(function ($, undefined) {}) (JQuery);
If the jquery plugin is used in our project, but the jquery framework is loaded via REQUIREJS (no global variable is added), how do I complete the loading of the jquery plugin? Using a traditional party, loading the jquery plugin through <script> in an HTML page is definitely not going to work. This time we need to use
Requirejs the shim parameter to complete the loading of the jquery plugin. Let's take the slider plugin for loading jquery-ui as an example:
Requirejs.config ({shim: {' jquery.ui.core ': [' jquery '], ' jquery.ui.widget ': [' jquery '], ' Jquery.ui.mouse ': [' jquery '], ' jquery.ui.slider ': [' jquery '] },paths: {jquery: ' Jquery-2.1.1/jquery ', Domready: ' Require-2.1.11/domready ', ' jquery.ui.core ': ' Jquery-ui-1.10.4/development-bundle/ui/jquery.ui.core ', ' Jquery.ui.widget ': ' Jquery-ui-1.10.4/development-bundle/ui/jquery.ui.widget ', ' jquery.ui.mouse ': ' Jquery-ui-1.10.4/development-bundle/ui/jquery.ui.mouse ', ' jquery.ui.slider ': ' jquery-ui-1.10.4/ Development-bundle/ui/jquery.ui.slider '}); require ([' jquery ', ' domready ', ' jquery.ui.core ', ' jquery.ui.widget ', ' Jquery.ui.mouse ', ' Jquery.ui.slider '],function ($) {$ ("#slider"). Slider ({ value:0, min:0, max:4, Step:1, slide:function (event, UI) {} );});
In the path parameter, we set the module name (optionally specified) and the JS file path mapping, and then in the shim parameter, specify the module name and its dependent array, above our jquery plug-in only rely on the jquery framework. In this way, you can use Requirejs to complete the loading of jquery and its plug-ins without the problem of global variable contamination.
Use Requirejs's shim parameter to complete the loading of the jquery plugin