sweetalert2 rails 6

sweetalert2 with rails 6

我正在使用 sweetify gem 将 sweetalert2 添加到我的 rails 6 应用程序,但出现此错误

Uncaught ReferenceError: Swal is not defined

我在 environment.js 文件中添加了以下代码

environment.plugins.append('Provide', new webpack.ProvidePlugin({
  sweetalert2: 'sweetalert2/dist/sweetalert2.all',
  Swal: 'sweetalert2/dist/sweetalert2.all'
}));

我确实在 application.js 文件中要求它如下

require("sweetalert2")

sweetify gem 在标签结束前添加以下代码

<script>
      Swal.fire({"showConfirmButton":true,"timer":null,"allowOutsideClick":false,"confirmButtonText":"Try again","type":"error","text":"there was a problem saving your Information, please try again","title":"Oops..."})
    </script>

我不知道我错过了什么。

非常感谢任何帮助

尝试这样做:

yarn install sweetalert2

import Swal from 'sweetalert2';
window.Swal = Swal;

对我有用的是 Rails 6.0.3

  1. yarn add sweetalert2
  2. app/javascript/packs/application.js
  3. 中添加以下条目
import Swal from 'sweetalert2/dist/sweetalert2.js'
import 'sweetalert2/src/sweetalert2.scss'
window.Swal = Swal;