2

我正在使用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>

我不知道我错过了什么。

非常感谢任何帮助

4

2 回答 2

3

尝试这样做:

yarn install sweetalert2

import Swal from 'sweetalert2';
window.Swal = Swal;
于 2020-05-27T08:25:35.543 回答
2

对我有用的是 Rails 6.0.3

  1. yarn add sweetalert2
  2. 在中添加以下条目app/javascript/packs/application.js
import Swal from 'sweetalert2/dist/sweetalert2.js'
import 'sweetalert2/src/sweetalert2.scss'
window.Swal = Swal;
于 2020-06-15T14:44:25.790 回答