Requirejs reference examples

I have an old web application that already uses ASP.Net MVC binding and minimization.

Now I'm starting to use requirejs and I have a problem! How can I reference package files from requirejs?

I know that there is another similar question, but it does not answer my question!

How to link related js files (asp.net mvc4) in Require.js?

+3
source share
1 answer

I hope I understand your question correctly.

If your package contains requireJS modules, and you upload it to a page, you can still name those modulesas long as you define them, as shown below:

Say your existing package is displayed as follows:

@Scripts.Render("~/bundles/aCustomBundle")

jQuery, React a aLibrary . aLibrary .

// Define custom modules
define('aLibrary', [], function() {
   return {
      aFunction: function() {
           console.log('a function within a library');
      }
   }
});

, .

<script>
    // Third party libraries
    define('jquery', [], function () { return jQuery; });
    define('react', [], function () { return React; });

    // Use the libs from the bundle
    require(['aLibrary', 'jquery', 'react'], function(a, j, r) {

       // Execute the function from the aLibrary
       a.aFunction();
    });
</script>
0

All Articles