抛出警报时,甜蜜警报 2 未显示为模式

Tay*_*tin 3 javascript node.js reactjs sweetalert sweetalert2

我认为我一定以某种方式导入了这个错误,但我不确定。我按照github上的说明进行操作。它说我可以npm install,然后就可以了import swal from 'sweetalert2'。我还必须使用jscss文件吗?

发生的情况是,我在窗口左下角收到一个奇怪的警报,该警报不会消失。在此输入图像描述

这是我的代码:

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;
Run Code Online (Sandbox Code Playgroud)

ser*_*gei 6

您应该包含 CSS 文件:

import 'sweetalert2/dist/sweetalert2.min.css';
Run Code Online (Sandbox Code Playgroud)

或者将 CDN 版本包含到 HTML 文件中,如Github 上的“使用”部分所示。