我遇到一个问题,我似乎无法在我的打字稿文件中导入bootstrap
js。我需要这个来打开模式。
我的打字稿脚本:
import $ from 'jquery';
import 'bootstrap';
$('#btn').click(function () {
$('#test').text('sasasa');
});
我的看法
<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">×</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>
}
当我在打字稿脚本上运行时,在控制台中出现以下错误:
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
但是,当我删除
import 'bootstrap'
行时,btn
单击处理程序就可以正常工作。因此,看来jQuery已正确导入。手动添加脚本(即:)
<!-- 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>
模态工作正常,但我不想走那条路。有没有人有什么建议?我在网上找不到似乎有相同问题的任何资源。
我的(相关部分)
package.json
: "@types/bootstrap": "^3.3.36",
"bootstrap": "^3.3.7",
"jquery": "^3.2.1",
有没有人有什么建议?任何帮助将不胜感激。
最佳答案
使用ES6 / CommonJS导入导入jQuery时,不会创建bootstrap
包所依赖的全局变量。您需要显式创建全局jQuery
变量,以便引导程序可以使用它:
import $ from 'jquery';
window['jQuery'] = $;
import 'bootstrap';