简体   繁体   中英

Bootstrapping RequireJS app with jQuery as a module without data-main attribute

In the app working with, I don't have a way of specyfing attributes on the tag for requirejs. I can specify the script tag in the form of

<script type="text/javascript" src="scripts/require.js"></script>

I can append another tag before or after this one and include code to bootstrap my app (main module). I am using jQuery 1.7.1 and would like to load it as dependency in other modules. I also want to do that without the requirejs-jquery bundle, so jQuery seperate of requirejs. I am looking for the right way to do that. I am fairly new to requirejs so an explanation of how the modules are loaded and a code sample would be great. Thanks!

include another javascript file after requirejs.

<script type="text/javascript" src="scripts/require.js"></script>
<script type="text/javascript" src="scripts/main.js"></script>

In this file you have to configure requirejs so it finds jquery.

main.js

require.config({
    paths: {
        "jquery": "js/jquery-1.7.1.min"//edit this path so it suits your needs.
  } 
});

From now on you'll be able to load jquery into your Modules by

require(['jquery'], function ($) {
    $('#foo').show();
});

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