javascript - 发出警报时,Sweet alert 2 未显示为模态

标签 javascript node.js reactjs sweetalert sweetalert2

我假设我一定是在某种程度上错误地导入了这个错误,但我不确定。我按照 github 上的说明进行操作.它说我可以 npm install 它,然后只需 import swal from 'sweetalert2'。我还必须使用 jscss 文件吗?

发生的事情是,我在窗口的左下方发出了一个奇怪的警报,该警报不会消失。 enter image description here

这是我的代码:

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/

相关文章:

javascript - 无法从 React 中的待办事项列表中删除项目

javascript - Angular.js 相当于 `rails generate scaffold` ?

javascript - jQuery 中意外的回调行为

php - 嵌套 else if 语法

javascript - 什么是 JavaScript 运行时..?

javascript - 无法将 "mysql2/promise"导入 Node.js 13/14 上的 ES 模块(MJS)

javascript - 使用 axios 获取数据并作为 props 导出到组件

javascript - 如何从输入文件设置背景图像?

node.js - 调用堆栈的深度

javascript - 对期望为 'number | string | undefined' 的属性使用泛型类型值