【问题标题】:Sweet alert 2 not showing up as a modal when alert thrown发出警报时,甜蜜警报 2 未显示为模态
【发布时间】:2017-10-03 14:58:56
【问题描述】:

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

【问题讨论】:

    标签: javascript node.js reactjs sweetalert sweetalert2


    【解决方案1】:

    您应该包含 CSS 文件:

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

    或者在HTML文件中包含CDN版本,如Usage section on Github所示。

    【讨论】:

      【解决方案2】:

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

      import swal from 'sweetalert2'
      

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

      你可以这样导入:

      import swal from 'sweetalert2/dist/sweetalert2.all.min.js'
      

      【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      相关资源
      最近更新 更多