Can't load Bootstrap with RequireJS

后端 未结 4 1319
孤城傲影
孤城傲影 2020-12-14 08:09

I can\'t seem to figure out how to load Bootstrap via RequireJS. None of the examples that I found worked for me.

Here is my shim:

require.config({
         


        
4条回答
  •  猫巷女王i
    2020-12-14 08:53

    I found a working example here:

    https://github.com/sudo-cm/requirejs-bootstrap-demo

    I followed it to get my code to work.

    According to that demo, especially app.js, you simply make a shim to catch Bootstrap's dependency on jQuery,

    requirejs.config({
        // pathsオプションの設定。"module/name": "path"を指定します。拡張子(.js)は指定しません。
        paths: {
            "jquery": "lib/jquery-1.8.3.min",
            "jquery.bootstrap": "lib/bootstrap.min"
        },
        // shimオプションの設定。モジュール間の依存関係を定義します。
        shim: {
            "jquery.bootstrap": {
                // jQueryに依存するのでpathsで設定した"module/name"を指定します。
                deps: ["jquery"]
            }
        }
    });
    

    and then mark Bootstrap as a dependency of the app itself, so that it loads before app.js.

    // require(["module/name", ...], function(params){ ... });
    require(["jquery", "jquery.bootstrap"], function ($) {
        $('#myModalButton').show();
    });
    

    Finally, since app.js is the data-main,

    
    

    Bootstrap's JS is guaranteed to load before any application code.

提交回复
热议问题