简体   繁体   English

如何在 laravel (sweetalert2) 中包含来自 node_modules 的文件

[英]How to include file from node_modules in laravel (sweetalert2)

I am trying to include sweetalert but it's seems like I misunderstood the instructions from the documentations.我正在尝试包含 sweetalert,但似乎我误解了文档中的说明。 Currently struggling how to include files from node_modules folder.目前正在努力如何从 node_modules 文件夹中包含文件。

In my blade I put script like these:在我的刀片中,我放置了这样的脚本:

$(document).ready(function() {
    Swal.fire(
        'The Internet?',
        'That thing is still around?',
        'question'
    )
});


I tried all these methods but none works: 我尝试了所有这些方法,但没有一个有效:

Method (1) - adding these codes in my app.js file (base from sweetalert doc )方法 (1) - 在我的 app.js 文件中添加这些代码(来自sweetalert doc 的基础

mix.copy('node_modules\sweetalert2\dist\sweetalert2.js', 'public/js/sweetalert2.js');
mix.copy('node_modules\sweetalert2\dist\sweetalert2.css', 'public/js/sweetalert2.css');

Method (2) - adding these codes in my app.js file (based from laravel doc )方法 (2) - 在我的 app.js 文件中添加这些代码(基于laravel doc

<link href="{{ asset('js/sweetalert2.js') }}">
<script src="{{ asset('js/sweetalert2.css') }}"></script>

and then,进而,

 <link href="{{ asset('js/sweetalert2.js') }}"> <script src="{{ asset('js/sweetalert2.css') }}"></script>


It always returns an error from my console 它总是从我的控制台返回错误

ReferenceError: mix is not defined参考错误:未定义混合

see full error logs: image查看完整的错误日志:图像


Sorry for my ignorance. 抱歉我的无知。 Can someone knows how / what is really a proper way of including a file(s) from node_modules? 有人可以知道如何/什么是从 node_modules 中包含文件的真正正确方法吗?

you don't need use mix to copy the sweatalert js file to public path你不需要使用 mix 来复制 steamalert js 文件到公共路径

  1. Install sweatalert with npm使用 npm 安装汗水
npm install sweetalert2
  1. You have two different choices in your app.js file use您的app.js文件使用中有两种不同的选择
import Swal from 'sweatalert2';

OR或者

var Swal = require('sweatalert2');

after that run the below command to compile the js file之后运行下面的命令来编译js文件

npm run watch

after that append js file in the app layout after that in each file you can use Swal modulethe app layout中追加js文件之后,在每个文件中您可以使用 Swal 模块

$(document).ready(function() {
    Swal.fire(
        'The Internet?',
        'That thing is still around?',
        'question'
    )
});

but you must do this after the section you import the app.js file because before that Swal not loaded in your project但是您必须在导入app.js文件的部分之后执行此操作,因为在此之前Swal未加载到您的项目中

  1. Install sweet Alert 2 with npm使用 npm 安装 Sweet Alert 2

     npm install sweetalert2
  2. In your app.js file在你的 app.js 文件中

    import swal from 'sweetalert2'; window.swal = swal; const toast = swal.mixin({ toast: true, position: 'top-end', showConfirmButton: false, timer: 3000 }); window.toast = toast;
  3. in your component在您的组件中

    swal.fire({ title: 'Are you sure?', text: "You won't be able to revert this!" + id, type: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Yes, delete it!' }).then((result) => { // Send request to the server if (result.value) { axios.delete("api/category/" + id).then(() => { toast.fire({ type: 'success', title: 'Deleted successfully' }); }).catch(() => { toast.fire({ type: 'error', title: 'Error Occurred..!' }) }); } })
  4. run npm run dev运行 npm run dev

     npm run dev

Put const mix = require('laravel-mix');const mix = require('laravel-mix'); in webpack at first line.在第一行的 webpack 中。 at try to re run webpack.在尝试重新运行 webpack。

Maybe put of time but try in bootstrap.js:也许花点时间但在 bootstrap.js 中尝试:

import Swal from 'sweetalert2';

then in the same file inside the try section:然后在 try 部分内的同一个文件中:

window.Swal = require('sweetalert2');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM