我假设我一定是在某种程度上错误地导入了这个错误,但我不确定。我按照 github 上的说明进行操作.它说我可以 npm install
它,然后只需 import swal from 'sweetalert2'
。我还必须使用 js
和 css
文件吗?
发生的事情是,我在窗口的左下方发出了一个奇怪的警报,该警报不会消失。
这是我的代码:
import React, { Component } from 'react';
import swal from 'sweetalert2';
class TestSwal extends Component {
componentDidMount() {
swal({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, keep it'
}).then(
function() {
swal('Deleted!', 'Your imaginary file has been deleted.', 'success');
},
function(dismiss) {
// dismiss can be 'overlay', 'cancel', 'close', 'esc', 'timer'
if (dismiss === 'cancel') {
swal('Cancelled', 'Your imaginary file is safe :)', 'error');
}
}
);
}
render() {
return <div>Test Swal</div>;
}
}
export default TestSwal;
最佳答案
你应该包含 CSS 文件:
import 'sweetalert2/dist/sweetalert2.min.css';
或者在HTML文件中包含CDN版本,如Usage section on Github所示.
关于javascript - 发出警报时,Sweet alert 2 未显示为模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46547648/