简体   繁体   English

Laravel Asset Management,如何使用已安装的NPM库(参考错误)

[英]Laravel Asset Management, how to use installed NPM library (reference error)

I'm quite new to managing assets in any other way than a direct download, and copying the required files to a designated folder and simply referencing this. 除了通过直接下载以外的任何其他方式来管理资产,将所需文件复制到指定文件夹并简单地引用它之外,我还很陌生。 However, I wish to keep my assets "close to the framework" and therefore hope to get some clarity regarding how it is done in Laravel. 但是,我希望使自己的资产“接近框架”,因此希望对在Laravel中如何完成工作有所了解。

I am using Laravel v5.4 and NPM v5.3.0 我正在使用Laravel v5.4和NPM v5.3.0

I want to use the Sweet Alert library and so did 我想使用Sweet Alert库,所以

npm install sweetalert

which placed the files in the node_modules directory and package.json as expected 将文件按预期放置在node_modules目录和package.json中

This is where the confusion begins. 这就是混乱的开始。 I then did 然后我做了

npm install --no-bin-links

(the no-bin-links flag recommended for Windows hosts by the docs) (文档建议Windows主机使用no-bin-links标志)

and

npm run dev

thinking this would compile/minify the library to my app.js or vendor.js (which does not exist), or at least do some magic to let me use the library. 认为这会将库编译/缩小到我的app.js或vendor.js(不存在),或者至少做了一些魔术让我使用该库。

The output states: 输出状态:

DONE  Compiled successfully in 8551ms  

which suggests to me that I have simply failed to include the Sweet Alert library in this process. 这向我表明,我只是没有在此过程中包含Sweet Alert库。

PHPStorm does suggest the library as an auto-complete option, but the application fails to load the library, stating in the JS Console on load: PHPStorm确实建议将该库作为自动完成选项,但是应用程序无法加载该库,并在加载时在JS控制台中声明:

jQuery.Deferred exception: swal is not defined ReferenceError: swal is not defined

I have also tried "require"-ing the library in bootstrap.js, stating: 我也尝试过“ require”-在bootstrap.js中声明该库:

window.swal = require('sweetalert');

or simply 或简单地

swal = require('sweetalert');

Where 'sweetalert' again is suggested by the IDE autocomplete. IDE自动完成功能建议再次使用“ sweetalert”。

Here is how I attempted to use it: 这是我尝试使用它的方式:

$( document ).ready(function () {
        alert("Hello!"); //works
        swal({
            title: "Hello!",
            text: "Hellooo",
            type: "error",
            confirmButtonText: "OK THEN"
        });
    });

Which throws the error mentioned above. 这引发了上面提到的错误。

I also tried initializing using 我也尝试使用初始化

window.swal({...
sweetAlert({...

which fail. 失败了。

What am I missing? 我想念什么? And how are you supposed to use NPM packages in a Laravel project/what are the best practices? 您应该如何在Laravel项目中使用NPM软件包/最佳实践是什么?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You need to add a reference to your sweetalert vendor file in your webpack.js config file. 您需要在webpack.js配置文件中添加对sweetalert供应商文件的引用。

mix.scripts('/vendor/..../sweetalert2.min.js', '/public/js/sweetalert.min.js');

That will copy it from your vendor folder to your public folder when you run 这将在您运行时将其从供应商文件夹复制到公用文件夹

npm run dev

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

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