首页 > 解决方案 > How Can I Install sweetalert2 via Laravel-Mix?

问题描述

STEP1: In console/terminal.

npm install --save sweetalert2

STEP2: In app.scss add this line...

@import '~sweetalert2/src/sweetalert2.scss';

STEP3: In app.js add this line...

const swal = require('sweetalert2');

STEP4: In webpack.min.js...

mix.setPublicPath('public');
mix.js('resources/js/app.js', 'js');
mix.sass('resources/sass/app.scss', 'css');

STEP5: npm run dev

STEP6: Add app.js and app.css to HTML document

I got this error:

Uncaught ReferenceError: swal is not defined

What's the missing step?

标签: laravelnpmlaravel-mixsweetalert2

解决方案


If you want to make it available anywhere you would have to tie it to the window:

const swal = window.swal = require('sweetalert2');

But a better way would be to include it in any file you use it in, the same way you did in app.js


推荐阅读