nbokmans
nbokmans

Reputation: 5757

Can't import bootstrap lib in TS script

I'm running into an issue where I can't seem to import bootstrapjs in my typescript file. I need this to open a modal.

My typescript script:

import $ from 'jquery';
import 'bootstrap';

$('#btn').click(function () {
    $('#test').text('sasasa');
});

My view

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                <p>Some text in the modal.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">
                test
            </h3>
        </div>
        <div class="panel-body">
            <span id="test">Test</span>

            <button id="btn">Klik</button>
        </div>
    </div>
</div>

@section scripts {
    <environment names="Development">
        <script src="~/js/quotation-test.js"></script>
    </environment>
    <environment names="Staging,Production">
        <script src="~/js/quotation-test.js" asp-append-version="true"></script>
    </environment>
}

When I run above typescript script I get the following error in my console:

Uncaught ReferenceError: jQuery is not defined
    at Object.562 (quotation-test.js:103)
    at __webpack_require__ (common.js:55)
    at Object.561 (quotation-test.js:27)
    at __webpack_require__ (common.js:55)
    at Object.560 (quotation-test.js:13)
    at __webpack_require__ (common.js:55)
    at webpackJsonpCallback (common.js:26)
    at quotation-test.js:1

However, when I remove the import 'bootstrap' line, the btn click handler works just fine. So it seems like jQuery is being imported correctly.

Adding the scripts manually (i.e:)

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

The modal works fine, but I'd rather not go that route. Does anyone have any suggestions? I can't find any resources online that seem to have the same problem.

My (relevant parts of) package.json:

    "@types/bootstrap": "^3.3.36",
    "bootstrap": "^3.3.7",
    "jquery": "^3.2.1",

Does anyone have any suggestions? Any help would be greatly appreciated.

Upvotes: 2

Views: 764

Answers (2)

Stubb0rn
Stubb0rn

Reputation: 1402

When jQuery is imported using ES6/CommonJS imports it doesn't create global variable that bootstrap package relies on. You need to explicitly create global jQuery variable so that bootstrap can use it:

import $ from 'jquery';
window['jQuery'] = $;
import 'bootstrap';

Upvotes: 1

alexander
alexander

Reputation: 29

mb bootstrap use "JQuery", not "$". Try "import 'jquery';" or "import { jQuery} 'jquery';"

Upvotes: 0

Related Questions