HTML:
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script type="module" src="sweetalert2.js"></script>
<link rel="stylesheet" href="sweetalert2.min.css">
<script type="module" src="test.js"></script>
</head>
<body>
</body>
</html>
JS:
$( document ).ready(function() {
import Swal from 'sweetalert2.js'
Swal.fire({
title: 'Error!',
text: 'Do you want to continue',
type: 'error',
confirmButtonText: 'Cool'
})
});
两个文件(test.html
和test.js
)都在同一目录中!
错误:
意外的标识符在test.js
-第3行
TypeError:无法设置未定义的属性'Sweetalert2'
-sweetalert2.js-第8行
答案 0 :(得分:1)
在这里您不需要import Swal from 'sweetalert2.js'
工作示例
<!DOCTYPE html>
<html>
<head>
<title>sweetalert2</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script>
$(document).ready(function() {
Swal.fire({
title: 'Error!',
text: 'Do you want to continue',
type: 'error',
confirmButtonText: 'Cool'
})
});
</script>
</body>
</html>