3

我认为我必须以某种方式导入这个错误,但我不确定。我按照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;
4

2 回答 2

5

您应该包含 CSS 文件:

import 'sweetalert2/dist/sweetalert2.min.css';

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

于 2017-10-03T15:10:41.203 回答
3

更新(2017 年 1 月):从 v7 开始,您需要同时导入 JS 和 CSS:

import swal from 'sweetalert2'

我们最近发布了v6.10.0,其中包含 JS 和 CSS 的一体化版本。

您可以通过以下方式导入它:

import swal from 'sweetalert2/dist/sweetalert2.all.min.js'
于 2017-10-03T15:15:07.703 回答