Using the Requirejs load module, the module is defined to comply with the AMD specification, which defines the module using the following function when defining the module:
1 define (function () { 2 var private = function { 3 Console.log (' Private party Fa ... ' ); 4 }; 5 return function () {private ();} 7 }; 8 });
What if you wanted to embed some of your previous code in Requirejs, even if you included the module code with define, but that code didn't comply with the AMD specification? Don't worry, require config of Shim can help you solve the pain! For example, my file directory structure is as follows:
I want to use the object provided by Framewokr.js in the Quiktip.js file, but Framework.js is not compliant with the AMD specification, in Quiktip.js file require The framework module must first process the following framework.js to conform to the AMD specification, as follows:
1 Require.config ({2BASEURL: ' js/utily ',//Specifies the base path of the JS file3 shim:{4' Framework ': {//This key name is the file name of the target file to be loaded, it cannot be arbitrarily named, otherwise the Framework.js file is not available after the file is not changed to the external interface. Because it's been a hole in my brother's afternoon!!!5Exports: ' Pxjsframe '//The exports value is the name of the external interface provided by Framework.js6 }7 }8 });9Require ([' framework '],function(frame) {Ten varPxjsframe =frame; OneConsole.log (frame);//This will print the interface objects provided in the framework.js. Ha ha. A}
Requirejs (shim) handles JavaScript libraries that load non-AMD specifications