Require.js ignoring baseUrl

后端 未结 2 1427
孤独总比滥情好
孤独总比滥情好 2021-02-06 09:32

OK this is driving me crazy so maybe someone can point me in the right direction...

I\'m using the latest require.js combined with jquery as my module loader. I am using

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 10:23

    the trouble is that require.js load script asynchronously (that's the point behind requirejs), so when you add the require() into a script tag right after you load require, this script execute before js/main.js get loaded.

    The easy way would be to include all this in main.js, or create a new file to hold this piece and load it from js/main

    /js/main.js

    require.config({
        baseUrl: '/js/vendor/'
    });
    
    require(['jquery', 'bootstrap'], function($) {
        $(function() {
            console.log('hello world!');
        });
    });
    

    -- OR --

    /js/main.js

    require.config({
        baseUrl: '/js/vendor/',
        deps: ['../boostrap']
    });
    

    /js/boostrap.js

    define(['jquery', 'bootstrap'], function($) {
        $(function() {
            console.log('hello world!');
        });
    });
    

    note require() became a define() in the bootstrap file

提交回复
热议问题