如何将sweetalert2安装到Laravel 10的Vite中?

huangapple go评论104阅读模式
英文:

How to install sweetalert2 to laravel 10 vite

问题

I can't have problem w sweetalert2.

I have error in console:

Uncaught ReferenceError: Swal is not defined
at HTMLButtonElement. (list:121:13)
at HTMLButtonElement.dispatch (jquery-3.7.0.js:5135:27)
at elemData.handle (jquery-3.7.0.js:4939:28)

It concerns this part of code (index.blade.php):

<script type="text/javascript">
$(function() {
    $('.delete').click(function() {
        Swal.fire("hello");
    });
});
</script>

In others files I have:
app.blade.php:

<script type="text/javascript">
    @yield('javascript')
</script>

bootstrap.js

import 'bootstrap';
try {
    window.Popper = require('popper.js').default;
    window.$ = window.jQuery = require('jquery');
    window.Swal = require('sweetalert2');

    require('bootstrap');
} catch (e) {}

package.json

{
    "private": true,
    "type": "module",
    "scripts": {
        "dev": "vite",
        "build": "vite build",
        "watch": "vite build --watch",
        "prod": "vite production"
    },
    "devDependencies": {
        "@popperjs/core": "^2.11.6",
        "axios": "^1.1.2",
        "bootstrap": "^5.2.3",
        "laravel-vite-plugin": "^0.7.5",
        "sass": "^1.56.1",
        "vite": "^4.0.0"
    },
    "dependencies": {
        "jquery": "^3.7.0",
        "popper.js": "^1.16.1",
        "sweetalert2": "^11.7.16"
    }
}

app.scss

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

I install sweetalert2 using command:

npm install sweetalert2

And this add new folder sweetalert in node_modules

When I use npm audit I have:

npm audit report
sweetalert2 >=11.6.14
sweetalert2 v11.6.14 and above contains potentially undesirable behavior - https://github.com/advisories/GHSA-mrr8-v49w-3333
fix available via npm audit fix --force
Will install sweetalert2@11.6.13, which is a breaking change
node_modules/sweetalert2

1 low severity vulnerability

To address all issues (including breaking changes), run:
npm audit fix --force

but npm audit fix --force doesn't help

I try to use npm run dev and npm run watch too.

Edit:
I add this to app.blade.php

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

And it's help.

But, Can it be made to work without it?

英文:

I can't have problem w sweetalert2.

I have error in console:

&gt; Uncaught ReferenceError: Swal is not defined
    at HTMLButtonElement.&lt;anonymous&gt; (list:121:13)
    at HTMLButtonElement.dispatch (jquery-3.7.0.js:5135:27)
    at elemData.handle (jquery-3.7.0.js:4939:28)

It concerns this part of code (index.blade.php):


    &lt;script type=&quot;text/javascript&quot;&gt;
                $(function() {
            $(&#39;.delete&#39;).click(function() {
                Swal.fire(&quot;hello&quot;);
            });
        });
    &lt;/script&gt;

In others files I have:
app.blade.php:


    &lt;script type=&quot;text/javascript&quot;&gt;
            @yield(&#39;javascript&#39;)
    &lt;/script&gt;

bootstap.js


    import &#39;bootstrap&#39;;
    try {
        window.Popper = require(&#39;popper.js&#39;).default;
        window.$ = window.jQuery = require(&#39;jquery&#39;);
        window.Swal = require(&#39;sweetalert2&#39;);
    
        require(&#39;bootstrap&#39;);
    } catch (e) {}

package.json


    {
        &quot;private&quot;: true,
        &quot;type&quot;: &quot;module&quot;,
        &quot;scripts&quot;: {
            &quot;dev&quot;: &quot;vite&quot;,
            &quot;build&quot;: &quot;vite build&quot;,
            &quot;watch&quot;: &quot;vite build --watch&quot;,
            &quot;prod&quot;: &quot;vite production&quot;
        },
        &quot;devDependencies&quot;: {
            &quot;@popperjs/core&quot;: &quot;^2.11.6&quot;,
            &quot;axios&quot;: &quot;^1.1.2&quot;,
            &quot;bootstrap&quot;: &quot;^5.2.3&quot;,
            &quot;laravel-vite-plugin&quot;: &quot;^0.7.5&quot;,
            &quot;sass&quot;: &quot;^1.56.1&quot;,
            &quot;vite&quot;: &quot;^4.0.0&quot;
        },
        &quot;dependencies&quot;: {
            &quot;jquery&quot;: &quot;^3.7.0&quot;,
            &quot;popper.js&quot;: &quot;^1.16.1&quot;,
            &quot;sweetalert2&quot;: &quot;^11.7.16&quot;
        }
    }

app.scss


    @import &#39;sweetalert2/src/sweetalert2.scss&#39;;

I install sweetalert2 using command:

> npm install sweetalert2

And this add new folder sweetalert in node_modules

When I use npm audit I have:

&gt; npm audit report                                                                                                          
                                                                                                                         sweetalert2  &gt;=11.6.14                                                                                                      
sweetalert2 v11.6.14 and above contains potentially undesirable behavior - https://github.com/advisories/GHSA-mrr8-v49w-3333
fix available via `npm audit fix --force`                                                                                   
Will install sweetalert2@11.6.13, which is a breaking change                                                                
node_modules/sweetalert2                                                                                                    
                                                                                                                            
1 low severity vulnerability                                                                                                

To address all issues (including breaking changes), run:
  npm audit fix --force

but npm audit fix --force doesn't help

I try to use npm run dev and npm run watch too.

Edit:
I add this to app.blade.php


    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/sweetalert2@11&quot;&gt;&lt;/script&gt;

And it's help.

But, Can it be made to work without it?

答案1

得分: 1

使用导入

**bootstrap.js** 

    import jQuery from &quot;jquery&quot;;
    window.$ = jQuery;
    
    import {popper} from &quot;@popperjs/core&quot;;
    window.popper = popper;
    
    import {swal} from &quot;sweetalert2&quot;;
    window.swal = swal;
英文:

Use imports.

bootstrap.js

import jQuery from &quot;jquery&quot;;
window.$ = jQuery;

import {popper} from &quot;@popperjs/core&quot;;
window.popper = popper;

import {swal} from &quot;sweetalert2&quot;;
window.swal = swal;

huangapple
  • 本文由 发表于 2023年7月13日 19:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定