[英]How to require npm packages after installing it in Laravel?
在Laravel中安裝后如何要求npm包?
例如,我需要包sweetalert2
,首先安裝它:
npm install --save sweetalert2
現在,我需要在laravel中的\\resources\\assets\\js\\bootstrap.js
文件中要求它嗎?
它的默認內容是這樣的:
window._ = require('lodash');
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
} catch (e) {}
window.axios = require('axios');
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
它似乎需要lodash
, jquery
, bootstrap
, axios
進入laravel,但是4個句子是不同的,它們是這樣的:
window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.axios = require('axios');
問題:
1,為什么4個句子有差異?
2,我想要包sweetalert2
,我怎么寫呢?
你應該只能在bootstrap.js
文件中require
它:
require('sweetalert2');
然后(假設你正在使用scss
)在你的條目.scss文件中:
@import "node_modules/sweetalert2/src/sweetalert2";
此外,您不必手動將bootstrap
附加到window
的原因是因為它將在加載時自動執行。 這是因為假設window
實例將始終存在,因為它是樣式框架。 其他庫可能不這樣做的原因之一是因為它們可以在未定義window
環境中使用。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.