Using Require.js without data-main

2019-01-31 07:46发布

问题:

Can I use Require.js in development without using the data-main attribute to load in my initial script? ie. <script data-main="scripts/main" src="scripts/require.js"></script> I'm finding it difficult for me to work with this attribute in my development environment.

回答1:

Yep, take a look at the documentation: http://requirejs.org/docs/api.html#config

You need to call require.config() and set baseUrl. Based on your example:

<script src="scripts/require.js"></script>
<script>
    require.config({
        baseUrl: "scripts"
    });

    require( [ /*...*/ ], function(  /*...*/ ) {
        /*...*/
    });
</script>